在不同的按钮事件上显示不同的控件

时间:2013-11-07 16:26:44

标签: c# windows forms

我不完全确定使用C#和Windows Forms可以实现这一点,但这里有:

我想将我的窗户分成两部分。左边是一系列垂直排列的按钮。

单击每个按钮时,右侧的内容必须更改。我希望不同的控件显示不同的按钮点击。

(有点像网络浏览器标签,只是在左侧窗格而不是在顶部)

我猜我必须使用拆分容器,但是我见过的大多数拆分容器教程都让他们在右边使用相同的控件,每次点击时都会显示不同的数据。

我希望我能清楚自己需要什么。任何帮助或指示将不胜感激。

3 个答案:

答案 0 :(得分:0)

有可能,一种方法是为每个右侧界面创建新的用户控件。当您单击按钮时,它可以将右侧面板中的其中一个控件的新实例加载到其中:

if (splitContainer.Panel2.Controls.Count > 0)
{
    splitContainer.Panel2.Controls[0].Dispose();    // Edit
    splitContainer.Panel2.Controls.Clear();
}

splitContainer.Panel2.Controls.Add(new RightHandControl());

这在我过去尝试时似乎有效。

编辑:正如评论中指出的那样,这个例子在清除时没有正确处理用户控件!要解决此问题,请在清除列表之前调用Dispose()

答案 1 :(得分:0)

您可以使用TabControl完成工作,如on MSDN所述。它显示了如何将标签放在右侧侧的示例,但使用“左侧”切换“右侧”应提供您所寻找的行为。

我在WinForms应用程序中尝试了它,它看起来工作正常,尽管示例中使用了丑陋的颜色......以下需要约30秒才能实现。

从链接引用:

  

显示右对齐标签

     
      
  1. 在表单中添加TabControl。
  2.   
  3. 将Alignment属性设置为Right。 如果你设置左侧,则左侧没有问题
  4.   
  5. 将SizeMode属性设置为Fixed,以便所有选项卡的宽度相同。
  6.   
  7. 将ItemSize属性设置为选项卡的首选固定大小。请记住,ItemSize属性的行为就像选项卡位于顶部,尽管它们是右对齐的。因此,为了使选项卡更宽,您必须更改高度属性,并且为了使它们更高,您必须更改宽度属性。
  8.   
  9. 将DrawMode属性设置为OwnerDrawFixed。
  10.   
  11. 为TabControl的DrawItem事件定义一个处理程序,该事件从左到右呈现文本。
  12.   

这是代码(转换为C#,因为MSDN只显示VB):

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
    Graphics g = e.Graphics;
    Brush _TextBrush = default(Brush);

    // Get the item from the collection. 
    TabPage _TabPage = tabControl1.TabPages[e.Index];

    // Get the real bounds for the tab rectangle. 
    Rectangle _TabBounds = tabControl1.GetTabRect(e.Index);

    if ((e.State == DrawItemState.Selected))
    {
        // Draw a different background color, and don't paint a focus rectangle.
        _TextBrush = new SolidBrush(Color.Red);
        g.FillRectangle(Brushes.Gray, e.Bounds);
    }
    else
    {
        _TextBrush = new System.Drawing.SolidBrush(e.ForeColor);
        e.DrawBackground();
    }

    // Use our own font. 
    Font _TabFont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Pixel);

    // Draw string. Center the text. 
    StringFormat _StringFlags = new StringFormat();
    _StringFlags.Alignment = StringAlignment.Center;
    _StringFlags.LineAlignment = StringAlignment.Center;
    g.DrawString(_TabPage.Text, _TabFont, _TextBrush, _TabBounds, new StringFormat(_StringFlags));
}

只需将您的内容放在TabControl中,您就可以(排序)了。

答案 2 :(得分:0)

您需要的确切布局和控件将取决于您。这是一个简单的起点,让您看到您拥有的选项(当然这里提供了1个选项):

  

1)将splitcontainer添加到表单中。

     

2)将flowLayoutPanel添加到splitcontainer的左侧面板并在父容器中停靠

     

3)因为你要求垂直布局设置FlowDirection    flowLayoutPanel自上而下

     

4)在我的例子中,我使用了6个按钮并命名它们(相反可能是    他们的文字)btncontrol1,2,3 ......等等,也一样。

     

5)将所有按钮单击处理程序设置为相同(在这种情况下)   buttons_Click)

     

6)复制此代码并粘贴

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

        private void buttons_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;

            switch (b.Name)
            {
                //of course after each ClearPanel what i do is only for
                //demonstration.
                case "btnControl1":
                    splitContainer1.Panel2.SuspendLayout();
                    ClearPanel();
                    splitContainer1.Panel2.Controls.Add(new ListBox());
                    splitContainer1.Panel2.ResumeLayout();
                    break;
                case "btnControl2":
                    splitContainer1.Panel2.SuspendLayout();
                    ClearPanel();
                    splitContainer1.Panel2.Controls.Add(new RadioButton());
                    splitContainer1.Panel2.ResumeLayout();
                    break;
                case "btnControl3":
                    splitContainer1.Panel2.SuspendLayout();
                    ClearPanel();
                    splitContainer1.Panel2.Controls.Add(new Button());
                    splitContainer1.Panel2.ResumeLayout();
                    break;
                case "btnControl4":
                    splitContainer1.Panel2.SuspendLayout();
                    ClearPanel();
                    splitContainer1.Panel2.Controls.Add(new DateTimePicker());
                    splitContainer1.Panel2.ResumeLayout();
                    break;
                case "btnControl5":
                    splitContainer1.Panel2.SuspendLayout();
                    ClearPanel();
                    splitContainer1.Panel2.Controls.Add(new DataGridView());
                    splitContainer1.Panel2.ResumeLayout();
                    break;
                case "btnControl6":
                    splitContainer1.Panel2.SuspendLayout();
                    ClearPanel();
                    splitContainer1.Panel2.Controls.Add(new TextBox());
                    splitContainer1.Panel2.ResumeLayout();
                    break;
                default:
                    break;
            }
        }

        private void ClearPanel()
        {
            if (splitContainer1.Panel2.HasChildren)
            {
                foreach (Control c in splitContainer1.Panel2.Controls)
                {
                    c.Dispose();
                }
                splitContainer1.Panel2.Controls.Clear();
            }
        }
    }

希望对你有所帮助。