简体   繁体   English

通过单击一个用户控件将用户控件置于最前面

[英]Bringing user control to front by clicking on one user control

I need some Help here.我需要一些帮助。 I have 4-5 user controls stacked upon one another.All are of same size and only the one on top is visible.我有 4-5 个用户控件堆叠在一起。所有控件的大小都相同,只有顶部的那个是可见的。 I have a button on the top most user control.我在最顶部的用户控件上有一个按钮。 I wish that when I click the button that usercontrol is sent to back and another user control is brought to front.我希望当我单击按钮时,用户控件被发送到后面,另一个用户控件被带到前面。 This is my Code:这是我的代码:

 private void btnaddcstmrdashbrd_Click(object sender, EventArgs e)
    {
        addorder addorder = new addorder();
        this.Visible = false;
        this.SendToBack();
        addorder.Visible = true;
        addorder.BringToFront();
    }

What happens is, The top most userControl is no more visible.发生的情况是,最顶层的 userControl 不再可见。 However the other user Control does not com to front and is not visible.然而,其他用户控件没有 com 到前面并且不可见。 I would really appreciate some help.我真的很感激一些帮助。

If the user controls are in different types, you can refer to the following code. 如果用户控件的类型不同,可以参考下面的代码。 Bind the same method to the button that in user control, and use switch to judge the user control type. 与用户控件中的按钮绑定相同的方法,并使用switch判断用户控件类型。

 UserControl1 userControl1 = new UserControl1(); UserControl2 userControl2 = new UserControl2(); UserControl3 userControl3 = new UserControl3(); private void Form1_Load(object sender, EventArgs e) { Button button1 = new Button { Text = "Next Control", Dock = DockStyle.Bottom, }; button1.Click += Button_Click; Button button2 = new Button { Text = "Next Control", Dock = DockStyle.Bottom, }; button2.Click += Button_Click; Button button3 = new Button { Text = "Next Control", Dock = DockStyle.Bottom, }; button3.Click += Button_Click; userControl1.Controls.Add(button1); userControl2.Controls.Add(button2); userControl3.Controls.Add(button3); Controls.Add(userControl1); Controls.Add(userControl2); Controls.Add(userControl3); } private void Button_Click(object sender, EventArgs e) { string controlName = ((Button)sender).Parent.Name; switch (controlName) { case "UserControl1": userControl2.BringToFront(); break; case "UserControl2": userControl3.BringToFront(); break; case "UserControl3": userControl1.BringToFront(); break; } }

The test result, 测试结果,

在此处输入图像描述


Update更新

If the button is added from designer, you can refer to the following code.如果按钮是从设计器添加的,您可以参考以下代码。

First, define the usercontrol property in Form1.cs.首先,在 Form1.cs 中定义 usercontrol 属性。 And pass the Form object via parameter.并通过参数传递表格 object。

public partial class Form1 : Form
{
    public static Form1 form1;

    public Form1()
    {
        InitializeComponent();
        form1 = this;
    }

    public UserControl1 UC1{get;set;}
    public UserControl2 UC2{get;set;}
    public UserControl3 UC3{get;set;}

    private void Form1_Load(object sender, EventArgs e)
    {
        UC1 = new UserControl1(form1);
        UC2 = new UserControl2(form1);
        UC3 = new UserControl3(form1);
        Controls.Add(UC1);
        Controls.Add(UC2);
        Controls.Add(UC3);
    }
}

Then, modity the code in UserControl1.cs.然后,修改 UserControl1.cs 中的代码。

public partial class UserControl1 : UserControl
{
    public Form1 f1;

    public UserControl1(Form1 form1)
    {
        InitializeComponent();
        f1 = form1;
    }

    // this button is dragged and dropped from toolbox
    private void button1_Click(object sender, EventArgs e)
    {
        f1.UC2.BringToFront();
        //f1.UC3.BringToFront();
        //f1.UC1.BringToFront();
    }
}

Do the same for the rest of UserControls.对 UserControls 的 rest 执行相同操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM