[英]How can I show some text on a textbox in a user control by clicking a button in another user control?
please be kind enough to note that I'm new to visual studio.I have a form named form1 in it I have 2 user controls namely uctxt and ucbtn. 请友好地注意我是Visual Studio的新手。我有一个名为form1的表单,其中有2个用户控件,即uctxt和ucbtn。 uctxt has a textbox named txt1 and ucbtn has a button named btn1.
uctxt有一个名为txt1的文本框,而ucbtn有一个名为btn1的按钮。
I need to fill txt1 with some text by clicking btn1(I tried public modifiers). 我需要通过单击btn1(我尝试使用公共修饰符)将一些文本填充到txt1中。 I searched everywhere on the internet for a solution for this and I found nothing.
我在互联网上到处搜索了解决方案,但没有发现任何问题。
I tried: 我试过了:
public void Btn1_Click(object sender, EventArgs e)
{
uctxt ucText = new uctxt();
ucText.txt1.Text = "welcome";
}
You need at least to expose the OnClick
event on the ucbtn
: 您至少需要在
ucbtn
上公开OnClick
事件:
public event EventHandler UserControlButtonClick;
protected void Btn1_Click(object sender, EventArgs e) =>
if (this.UserControlButtonClick != null)
this.UserControlButtonClick(this, e);
And the label on the uctxt
: 以及
uctxt
上的uctxt
:
public String UserControlLabelText
{
get{return txt1.Text;}
set{txt1.Text = value;}
}
After that you can use them both from the main form like this: 之后,您可以从主表单使用它们,如下所示:
ucbtn1.UserControlButtonClick += new EventHandler(ucbtn1_ButtonClick);
protected void ucbtn1_ButtonClick(object sender, EventArgs e)
{
uctxt1.UserControlLabelText = "your text";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.