1

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

test1:应该显示面板的 ToolStripMenuItem -> test_panel_1
test2:应该显示面板的 ToolStripMenuItem -> panel1

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

panel1 包含 button -> 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;
    }
}

我玩过一点用户控件,我喜欢编辑和查看它们的方式比面板好得多;但是,我不知道如何控制隐藏和显示它们。

谢谢您的帮助。我知道这一定是编码 101,但我还没有完全弄清楚。

4

1 回答 1

7

看起来你test_panel_2是 的孩子test_panel_1panel1是 的孩子panel2。这可能不是您想要的。事情是这样的,因为test_panel_2里面 test_panel_1,躲test_panel_1也躲test_panel_2。那里有等级制度。

有一两个地方您再次将可见性设置为 false - 我不确定这些是否正确。

于 2012-06-26T02:59:38.440 回答