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; }
}
这篇关于将值从用户控件传递到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将值从用户控件传递到表单


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01