在窗口应用程序C#中将值从用户控件传递到窗体窗体 [英] Pass Value From user control to Window form in window application C#

查看:103
本文介绍了在窗口应用程序C#中将值从用户控件传递到窗体窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家,

我有一个问题。我想将值从用户控件传递到窗口窗体。我这样做了

表格编码

Dear Experts,
i have a question. I want to pass value from user control to window form. I have done it with like this
Coding on Form

public string ID2
        {
            set
        {
            textBox1.Text = value;

        }




private void button1_Click(object sender, EventArgs e)
        {

           Sample samp = new Sample();
           samp.ID = ID;
            Controls.Add(samp);
            samp.BringToFront();
        }





关于USER Control



On USER Control

public string ID2
        {
            get
            {
                return textBox1.Text;

            }
        }







private void button1_Click(object sender, EventArgs e)
        {
            var textBoxContent = this.textBox1.Text;
           // ParentForm parent = (ParentForm)this.Owner;
            var parent = this.Parent as Form1;
            //var parent=this.Parent as
            parent.ID2 = ID2;
            this.Hide();

        }





这样做非常好。但问题是,我在用户控件中提供了form1的引用。所以,我只能在Form1上使用它,但我想在form2上使用相同的用户控件。我怎么能这样做?

PLZ快速回复





谢谢

Sunil Dhanerwal



This is doing very nicely. But the problem is, i am giving reference of form1 in user control. So, i can use it only on Form1 but same user control i want to use on form2. how can i do that?
plz reply fast


Thanks
Sunil Dhanerwal

推荐答案

看看这个:在两种形式之间传递信息,第2部分:儿童到父母 [ ^ ] - 它谈论表单,但它对于用户控件完全相同:表单是Parent,UserControl是孩子。
Have a look at this: Transferring information between two forms, Part 2: Child to Parent[^] - it talks about forms, but it's exactly the same for user controls: the form is the Parent, the UserControl is the child.


我自己解决了这个问题。

感谢回复家伙
i have solved it myself.
thanks for reply guys


这篇关于在窗口应用程序C#中将值从用户控件传递到窗体窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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