如何从另一个获取值到自定义用户控件 [英] how to get value from another from to an custom user control

查看:108
本文介绍了如何从另一个获取值到自定义用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单Form1和一个自定义用户控件,我自己有文本框和一个按钮。当我点击我的自定义用户控件的按钮时,会出现另一个表单,说Form2有按钮和标签。在2的按钮点击事件中我希望表单标签的值在我的自定义用户控件的文本框中。在此先感谢您的回答:)。

i have one form say Form1 and a custom user control, that i made myself having textbox and a button. when i click in the button of my custom user control another form appears say Form2 having button and label. in the button click event of the from 2 i want the value of label of form to in textbox of my custom user control. Thanks in advance for you answer :).

推荐答案

在Form1上放置一个公共事件,它将一些文本作为参数并使用该文本更新您的自定义控件。 br />


当您显示Form2时,请务必将所有者设置为Form1



然后从Form2上的按钮单击事件调用Form1上的事件,并在Form2上调用文本



例如在Form1上我放置了一个Button1显示Form2 ...

Put a public event on Form1 that will take some text as an argument and update your custom control with that text.

When you display Form2 be sure to set the Owner as Form1

Then from the button click event on Form2 call the event on Form1 with the text on Form2

For example on Form1 I have placed a Button1 which displays a Form2...
private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.Show(this);
}



我还在Form1上创建了一个事件......


I also created an event on Form1 ...

public void AnEvent(string text)
{
    MessageBox.Show(text);   // replace this with the update to your custom control
}



在Form2上我也有一个按钮1来执行此操作...


On Form2 I also have a button1 that does this...

private void button1_Click(object sender, EventArgs e)
{
    Form1 f = (Form1)this.Owner;
    f.AnEvent("My text");
}


这篇关于如何从另一个获取值到自定义用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆