将值从用户控件传递到表单

Pass value from Usercontrol to Form(将值从用户控件传递到表单)

本文介绍了将值从用户控件传递到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用文本框创建了一个 usercontrol1.在我的表单中,我添加了一个用户控件(带有文本框的 usercontrol1)和一个文本框.我已经知道如何将值从 Form 传递给 Usercontrol.

I create a usercontrol1 with textBox. And with my form I add a usercontrol(the usercontrol1 with textBox) and a textBox. I already know how to pass value from Form to Usercontrol.

Form Code
public string ID
    {
        get { return textBox1.Text; }
    }
private void textBox1_TextChanged(object sender, EventArgs e)
    {
        userControl11.ID = ID;
    }

Usercontrol Code
public string BorrowerID
    {
        set { textBox1.Text = value; }
    }

但是不知道如何将值从Usercontrol的textBox传递到Form的textbox?我发现了如何从用户控件关闭表单.

But don't know how to pass the value from textBox of Usercontrol to textbox of Form? I found on how to close the form from usercontrol.

((Form)this.TopLevelControl).Close();

更改父窗体颜色

this.ParentForm.BackColor= Color.Red;

我将如何实现类似的方法或其他方法将值从用户控件传递到表单?

How would i implement something like this or other method to pass value from usercontrol to form?

((Form)this.TopLevelControl).ID = ID;

this.ParentForm.ID= ID;

推荐答案

我在一个新项目中创建了 UserControl1 并将其引用到包含表单的项目中,而不是直接在表单的项目中添加 UserControl,这就是为什么变得复杂.

I create the UserControl1 in a new project and reference it to my project that contains the form instead of directly adding UserControl in the form´s project, that's why things get complicated.

现在在这里将值从 UserControl 传递给 Form

Here it now to pass value from UserControl to Form

用户控制

public string ID2
{
    get { return textBox1.Text; }
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    var textBoxContent = this.textBox1.Text;
    var parent = this.Parent as Form1;
    parent.ID2 = ID2;  
}

Form1

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

这篇关于将值从用户控件传递到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将值从用户控件传递到表单

基础教程推荐