简体   繁体   English

C#-如何在用户控件之间传输信息

[英]C# - How to Transfer Information Between User Control

im doing an application where user enter a value inside the text box then he press a button, both in the same user control. 我正在做一个应用程序,用户在文本框内输入一个值,然后他在同一用户控件中按下一个按钮。 Then the result from the text box will show at the label of other user control. 然后,来自文本框的结果将显示在其他用户控件的标签上。 Both of the user control is in the same windows form. 两个用户控件都处于相同的Windows窗体中。

Thanks! 谢谢!

Image of user interface 用户界面图片

在此处输入图片说明

The most common way to do this is use an event. 最常见的方法是使用事件。 This is how I would do it: 这就是我要做的:

First define an EventArgs: 首先定义一个EventArgs:

public class MyEventArgs : EventArgs
{
    public string Text { get; private set; }

    public MyEventArgs(string Text)
    {
        this.Text = Text;
    }
}

Then in your UserControl (the one with the button): 然后在您的UserControl(带有按钮的控件)中:

public partial class MyUserControl
{
    public event EventHandler<MyEventArgs> ButtonClicked;

    public MyUserControl()
    {
        //...

        button1.Click += (o, e) => OnButtonClicked(new MyEventArgs(textBox1.Text));
    }

    protected virtual void OnButtonClicked(MyEventArgs args)
    {
        var hand = ButtonClicked;
        if(hand != null) ButtonClicked(this, args);
    }
}

Then subscribe to your MyUserControl.ButtonClicked event in the form and call a method in the second control. 然后,在表单中订阅MyUserControl.ButtonClicked事件,并在第二个控件中调用一个方法。


Note if the behavior of the button and the text in the textbox are actually not related, you can use a property to get the text entered and an empty EventArgs for your event instead. 请注意,如果按钮的行为与文本框中的文本实际上相关,则可以使用属性获取输入的文本,并使用事件的空EventArgs代替。

PS The names MyEventArgs , MyUserControl , and ButtonClicked are just for demonstration purposes. PS名称MyEventArgsMyUserControlButtonClicked仅用于演示目的。 I encourage you to use more descriptive/relevant naming in your code. 我鼓励您在代码中使用更具描述性/相关性的命名方式。

try this: 尝试这个:

public class FirstUserControl:UserControl
{
    Public event EventHandler MyEvent;

    //Public property in your first usercontrol
    public string MyText
    {
        get{return this.textbox1.Text;} //textbox1 is the name of your textbox
    }

    private void MyButton_Clicked(/* args */)
    {
        if (MyEvent!=null)
        {
            MyEvent(null, null);
        }
    }
    //other codes
}


public class SecondUserControl:UserControl
{
    //Public property in your first usercontrol
    public string MyText
    {
        set{this.label1.Text = value;} //label1 is the name of your label
    }

    //other codes
}

then in your MainForm: 然后在您的MainForm中:

public class MainForm:Forms
{
    //Add two instance of the UserControls

    public MainForm()
    {
        this.firstUserControl.MyEvent += MainWindow_myevent;
    }

    void MainWindow_myevent(object sender, EventArgs e)
    {
        this.secondUserControl.MyText = this.firstUserControl.MyText;
    }

    //other codes
}

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

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