面板可见性在C#visual studio 2010中没有变化

时间:2012-06-26 02:41:36

标签: c# .net visual-studio-2010 panel show-hide

我正在尝试做一些我认为相当简单的事情。我有2个工具条。

test1:ToolStripMenuItem应显示面板 - > test_panel_1
test2:ToolStripMenuItem应显示面板 - > PANEL1

test_panel_1包含按钮 - > panel_1_button1应隐藏test_panel_1并显示test_panel_2

panel1包含按钮 - > button1应隐藏test_panel2,然后显示panel2

然而,当我运行代码并单击test1ToolStripMenuItem时,它会显示test_panel_1,就像它应该的那样,然后当我点击panel_1_button_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;
    }
}

我玩了一些Usercontrols,我喜欢我可以编辑和查看它们的方式比面板更好;但是,我不知道如何控制隐藏和显示它们。

感谢您的帮助。我知道这必须是编码101,但这还是我还没有完全理解的东西。

1 个答案:

答案 0 :(得分:7)

您的test_panel_2似乎是test_panel_1的孩子。 panel1panel2的孩子。这可能不是你想要的。发生了什么事情,因为test_panel_2 test_panel_1内的,隐藏test_panel_1也会隐藏test_panel_2。那里有一个等级。

您可以再次将可见性设置为一个或两个位置 - 我不确定这些是否正确。