C# C-如何在用户控件之间传输信息
我在做一个应用程序,用户在文本框中输入一个值,然后按下一个按钮,两者都在同一个用户控件中。然后文本框的结果将显示在其他用户控件的标签上。这两个用户控件都处于相同的windows窗体中 谢谢C# C-如何在用户控件之间传输信息,c#,winforms,C#,Winforms,我在做一个应用程序,用户在文本框中输入一个值,然后按下一个按钮,两者都在同一个用户控件中。然后文本框的结果将显示在其他用户控件的标签上。这两个用户控件都处于相同的windows窗体中 谢谢 最常见的方法是使用事件。我会这样做: 首先定义一个EventArgs: public class MyEventArgs : EventArgs { public string Text { get; private set; } public MyEventArgs(string Text
最常见的方法是使用事件。我会这样做: 首先定义一个EventArgs:
public class MyEventArgs : EventArgs
{
public string Text { get; private set; }
public MyEventArgs(string Text)
{
this.Text = Text;
}
}
然后在用户控件中,使用按钮:
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);
}
}
然后订阅表单中的MyUserControl.ButtonClicked事件,并调用第二个控件中的方法
注意:如果按钮的行为与文本框中的文本实际上不相关,则可以使用属性来获取输入的文本,并为事件获取空的EventArgs
另外,MyEventArgs、MyUserControl和ButtonClicked的名称仅用于演示目的。我鼓励您在代码中使用更具描述性/相关性的命名。最常用的方法是使用事件。我会这样做: 首先定义一个EventArgs:
public class MyEventArgs : EventArgs
{
public string Text { get; private set; }
public MyEventArgs(string Text)
{
this.Text = Text;
}
}
然后在用户控件中,使用按钮:
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);
}
}
然后订阅表单中的MyUserControl.ButtonClicked事件,并调用第二个控件中的方法
注意:如果按钮的行为与文本框中的文本实际上不相关,则可以使用属性来获取输入的文本,并为事件获取空的EventArgs
另外,MyEventArgs、MyUserControl和ButtonClicked的名称仅用于演示目的。我鼓励您在代码中使用更具描述性/相关性的命名。尝试以下方法:
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
}
然后在主窗体中:
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
}
试试这个:
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
}
然后在主窗体中:
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
}
请提供您的一些代码,这样我们就可以看到您必须开始和也-这是WPF还是windows窗体?您使用的是什么UI设计模式。您的控制器应该能够通过更新其依赖项属性触发标签更新。soryy im new here@theodox…我使用windows窗体请提供您的一些代码,以便我们可以看到您必须启动的内容,并且-这是WPF还是windows窗体?您使用的是什么UI设计模式。您的控制器应该能够通过更新其依赖项属性触发标签更新。soryy im new here@theodox…我使用windows窗体