2012-06-26 27 views
1

我試圖做一些我認爲很簡單的事情。我有2個工具條項目。在C#visual studio 2010中不會改變面板的可見性

TEST1:ToolStripMenuItem哪些應該顯示面板 - > test_panel_1
TEST2:ToolStripMenuItem哪些應該顯示面板 - > PANEL1

test_panel_1包含按鈕 - > panel_1_button1應隱藏test_panel_1並顯示test_panel_2

PANEL1包含按鈕 - > button1這應該隱藏test_panel2然後顯示面板2

但是,當我運行代碼並單擊test1ToolStripMenuItem它顯示test_panel_1像它應該,然後當我點擊panel_1_bu tton_1它只是清除了test_panel_1,並沒有顯示test_panel_2。不管我首先點擊什麼,test2ToolStripMenuItem根本不顯示panel1。

這裏是我的代碼...

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace panel_test 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void panel_1_button1_Click(object sender, EventArgs e) 
     { 
      test_panel2.Visible = true; 
      test_panel_1.Visible = false; 
     } 

     private void test1ToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      test_panel_1.Visible = true; 
      panel1.Visible = false; 
     } 

     private void button1_click(object sender, EventArgs e) 
     { 
      test_panel_1.Visible = false; 
      test_panel2.Visible = true; 
     } 

     private void test2ToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      test_panel2.Visible = false; 
      panel1.Visible = true; 
     } 

     private void button1_Click_1(object sender, EventArgs e) 
     { 
      this.panel1.Visible = false; 
      this.panel2.Visible = true; 
     } 
    } 
} 

和,不知道這會有所幫助,但...

namespace panel_test 
{ 
    partial class Form1 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.menuStrip1 = new System.Windows.Forms.MenuStrip(); 
      this.testToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
      this.test1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
      this.test2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
      this.test3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
      this.test_panel_1 = new System.Windows.Forms.Panel(); 
      this.panel1 = new System.Windows.Forms.Panel(); 
      this.button1 = new System.Windows.Forms.Button(); 
      this.panel2 = new System.Windows.Forms.Panel(); 
      this.label3 = new System.Windows.Forms.Label(); 
      this.test_panel2 = new System.Windows.Forms.Panel(); 
      this.label2 = new System.Windows.Forms.Label(); 
      this.panel_1_button1 = new System.Windows.Forms.Button(); 
      this.label1 = new System.Windows.Forms.Label(); 
      this.menuStrip1.SuspendLayout(); 
      this.test_panel_1.SuspendLayout(); 
      this.panel1.SuspendLayout(); 
      this.panel2.SuspendLayout(); 
      this.test_panel2.SuspendLayout(); 
      this.SuspendLayout(); 
      // 
      // menuStrip1 
      // 
      this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 
      this.testToolStripMenuItem}); 
      this.menuStrip1.Location = new System.Drawing.Point(0, 0); 
      this.menuStrip1.Name = "menuStrip1"; 
      this.menuStrip1.Size = new System.Drawing.Size(467, 24); 
      this.menuStrip1.TabIndex = 0; 
      this.menuStrip1.Text = "menuStrip1"; 
      // 
      // testToolStripMenuItem 
      // 
      this.testToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { 
      this.test1ToolStripMenuItem, 
      this.test2ToolStripMenuItem, 
      this.test3ToolStripMenuItem}); 
      this.testToolStripMenuItem.Name = "testToolStripMenuItem"; 
      this.testToolStripMenuItem.Size = new System.Drawing.Size(38, 20); 
      this.testToolStripMenuItem.Text = "test"; 
      // 
      // test1ToolStripMenuItem 
      // 
      this.test1ToolStripMenuItem.Name = "test1ToolStripMenuItem"; 
      this.test1ToolStripMenuItem.Size = new System.Drawing.Size(99, 22); 
      this.test1ToolStripMenuItem.Text = "test1"; 
      this.test1ToolStripMenuItem.Click += new System.EventHandler(this.test1ToolStripMenuItem_Click); 
      // 
      // test2ToolStripMenuItem 
      // 
      this.test2ToolStripMenuItem.Name = "test2ToolStripMenuItem"; 
      this.test2ToolStripMenuItem.Size = new System.Drawing.Size(99, 22); 
      this.test2ToolStripMenuItem.Text = "test2"; 
      this.test2ToolStripMenuItem.Click += new System.EventHandler(this.test2ToolStripMenuItem_Click); 
      // 
      // test3ToolStripMenuItem 
      // 
      this.test3ToolStripMenuItem.Name = "test3ToolStripMenuItem"; 
      this.test3ToolStripMenuItem.Size = new System.Drawing.Size(99, 22); 
      this.test3ToolStripMenuItem.Text = "test3"; 
      // 
      // test_panel_1 
      // 
      this.test_panel_1.Controls.Add(this.test_panel2); 
      this.test_panel_1.Controls.Add(this.panel_1_button1); 
      this.test_panel_1.Controls.Add(this.label1); 
      this.test_panel_1.Location = new System.Drawing.Point(44, 28); 
      this.test_panel_1.Name = "test_panel_1"; 
      this.test_panel_1.Size = new System.Drawing.Size(442, 317); 
      this.test_panel_1.TabIndex = 1; 
      this.test_panel_1.Visible = false; 
      // 
      // panel1 
      // 
      this.panel1.Controls.Add(this.button1); 
      this.panel1.Location = new System.Drawing.Point(218, 187); 
      this.panel1.Name = "panel1"; 
      this.panel1.Size = new System.Drawing.Size(328, 318); 
      this.panel1.TabIndex = 1; 
      this.panel1.Visible = false; 
      // 
      // button1 
      // 
      this.button1.Location = new System.Drawing.Point(86, 155); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(75, 23); 
      this.button1.TabIndex = 0; 
      this.button1.Text = "panel 3 to 4"; 
      this.button1.UseVisualStyleBackColor = true; 
      this.button1.Click += new System.EventHandler(this.button1_Click_1); 
      // 
      // panel2 
      // 
      this.panel2.Controls.Add(this.panel1); 
      this.panel2.Controls.Add(this.label3); 
      this.panel2.Location = new System.Drawing.Point(95, 194); 
      this.panel2.Name = "panel2"; 
      this.panel2.Size = new System.Drawing.Size(398, 260); 
      this.panel2.TabIndex = 1; 
      this.panel2.Visible = false; 
      // 
      // label3 
      // 
      this.label3.AutoSize = true; 
      this.label3.Location = new System.Drawing.Point(86, 118); 
      this.label3.Name = "label3"; 
      this.label3.Size = new System.Drawing.Size(71, 13); 
      this.label3.TabIndex = 0; 
      this.label3.Text = "this is panel 4"; 
      // 
      // test_panel2 
      // 
      this.test_panel2.Controls.Add(this.panel2); 
      this.test_panel2.Controls.Add(this.label2); 
      this.test_panel2.Location = new System.Drawing.Point(154, 101); 
      this.test_panel2.Name = "test_panel2"; 
      this.test_panel2.Size = new System.Drawing.Size(358, 237); 
      this.test_panel2.TabIndex = 3; 
      this.test_panel2.Visible = false; 
      // 
      // label2 
      // 
      this.label2.AutoSize = true; 
      this.label2.Location = new System.Drawing.Point(70, 118); 
      this.label2.Name = "label2"; 
      this.label2.Size = new System.Drawing.Size(71, 13); 
      this.label2.TabIndex = 0; 
      this.label2.Text = "this is panel 2"; 
      // 
      // panel_1_button1 
      // 
      this.panel_1_button1.Location = new System.Drawing.Point(86, 155); 
      this.panel_1_button1.Name = "panel_1_button1"; 
      this.panel_1_button1.Size = new System.Drawing.Size(75, 23); 
      this.panel_1_button1.TabIndex = 2; 
      this.panel_1_button1.Text = "button1"; 
      this.panel_1_button1.UseVisualStyleBackColor = true; 
      this.panel_1_button1.Click += new System.EventHandler(this.panel_1_button1_Click); 
      // 
      // label1 
      // 
      this.label1.AutoSize = true; 
      this.label1.Location = new System.Drawing.Point(28, 21); 
      this.label1.Name = "label1"; 
      this.label1.Size = new System.Drawing.Size(42, 13); 
      this.label1.TabIndex = 0; 
      this.label1.Text = "panel 1"; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(467, 357); 
      this.Controls.Add(this.test_panel_1); 
      this.Controls.Add(this.menuStrip1); 
      this.MainMenuStrip = this.menuStrip1; 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.menuStrip1.ResumeLayout(false); 
      this.menuStrip1.PerformLayout(); 
      this.test_panel_1.ResumeLayout(false); 
      this.test_panel_1.PerformLayout(); 
      this.panel1.ResumeLayout(false); 
      this.panel2.ResumeLayout(false); 
      this.panel2.PerformLayout(); 
      this.test_panel2.ResumeLayout(false); 
      this.test_panel2.PerformLayout(); 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.MenuStrip menuStrip1; 
     private System.Windows.Forms.ToolStripMenuItem testToolStripMenuItem; 
     private System.Windows.Forms.ToolStripMenuItem test1ToolStripMenuItem; 
     private System.Windows.Forms.ToolStripMenuItem test2ToolStripMenuItem; 
     private System.Windows.Forms.ToolStripMenuItem test3ToolStripMenuItem; 
     private System.Windows.Forms.Panel test_panel_1; 
     private System.Windows.Forms.Panel test_panel2; 
     private System.Windows.Forms.Label label2; 
     private System.Windows.Forms.Button panel_1_button1; 
     private System.Windows.Forms.Label label1; 
     private System.Windows.Forms.Panel panel2; 
     private System.Windows.Forms.Label label3; 
     private System.Windows.Forms.Panel panel1; 
     private System.Windows.Forms.Button button1; 
    } 
} 

我玩弄用戶控件的卻很少,我喜歡的方式我可以編輯和查看它們比面板好得多;但是,我不知道如何控制隱藏和顯示它們。

感謝您的幫助。我知道這一定是編碼101,但這是我還沒有完全弄清楚的。

+0

檢查您的事件處理程序是否正在使用某些斷點。同時檢查你的面板不在彼此之上。我有時會將面板設置爲不同尋常的顏色,以便我可以清楚地看到事物,以確保事物不會意外重疊。 –

回答

7

它看起來像你的test_panel_2test_panel_1的孩子。 panel1panel2的子女。這可能不是你想要的。發生了什麼事情是因爲test_panel_2裏面的test_panel_1,隱藏test_panel_1也隱藏test_panel_2。那裏有一個等級。

有一個或兩個位置,您將可見性再次設置爲false - 我不確定這些位置是否正確。

+0

謝謝,明天我會試試。我認爲你是對的,但我確實在現有的渠道中從技術上繪製了新的面板,意圖使它成爲形式的一個孩子。有沒有更好的方式去使用usercontrols或其他東西呢?很難編輯和管理多個面板,並且即時只在我的真實應用程序中使用4,所以必須有更好的方法。 –

+0

很難說不知道你要做什麼樣的效果。 –

+0

那麼這是一個很好的方式來燒一個半小時以上的一些嚴重的頭辦公桌。我認爲是時候進行一些睡眠了......不,還有更多的咖啡。 :D謝謝查理! – nevelis

相關問題