【问题标题】:Panel visibility not changing in C# visual studio 2010C# Visual Studio 2010 中的面板可见性没有改变
【发布时间】:2012-06-27 08:15:03
【问题描述】:

我正在尝试做一些我认为相当简单的事情。我有 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;
    }
}

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

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

【问题讨论】:

  • 使用一些断点检查您的事件处理程序是否被命中。还要检查您的面板是否彼此重叠。有时我会将面板设置为不寻常的颜色,以便我可以清楚地看到事物,以确保事物不会意外重叠。

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


【解决方案1】:

看起来您的test_panel_2test_panel_1 的子级。 panel1panel2 的孩子。这可能不是您想要的。发生的事情是,因为test_panel_2 在内部 test_panel_1,隐藏test_panel_1 也隐藏test_panel_2。那里有一个层次结构。

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

【讨论】:

  • 谢谢,我明天试试。不过,我认为您是对的,我确实在现有频道中从技术上绘制了新面板,目的是使其成为表单的子项。有没有更好的方法来使用用户控件或其他东西来解决这个问题?多个面板很难编辑和管理,而且我在我的真实应用中只使用4个,一定有更好的方法。
  • 很难说不知道你想要什么样的效果。
  • 嗯,这是一个很好的方式,可以在一些严肃的办公桌上花费半个小时。我想是时候睡一觉了……不……再喝点咖啡。 :D 谢谢查理!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-10
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
相关资源
最近更新 更多