简体   繁体   English

如何通过单击另一个用户控件中的按钮来在用户控件的文本框中显示一些文本?

[英]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.

相关问题 从一个视图切换到另一个视图时,如何保持用户控件的 Textbox Text 属性的值? - How do I persist the value of the Textbox Text property of a user control when switching from one view to another? 如何在用户控件中公开 TextBox 文本属性 - How to expose TextBox Text Property in a User Control 如何从另一个用户控件显示用户控件? - how to show user control from another user control? 我如何关闭从另一个用户控件的按钮中弹出的用户控件,单击WPF中的事件 - How i can Close User Control which were open in pop up from another User Control's button click Event in WPF 如何在WPF用户控件中触发单击事件(带按钮的文本框)? - How to Trigger Click Event in WPF User control (Textbox with Button)? WPF,如何从另一个用户控件的 TextBox 元素聚焦? - WPF, How to focus a TextBox element of an user control from another? 如何从一个用户控件到另一个用户控件获取文本框信息 - How to get Textbox information from one user control to another 如何将文本从动态生成的用户控件传输到文本框 - How to transfer the text from dynamically generated user control to a textbox 如何在用户控制页上的占位符中设置'textbox.Text'值? - How to set 'textbox.Text' value in placeholder on User Control Page? 带文本框的用户控件 - user control with a textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM