[英]How to Get Data From User Control on Form
I'm building a simple app in Visual Studio 2010 using C#, and I'm doing this to get familiar with user control because it seems to make it easier than creating multiple forms. 我正在使用C#在Visual Studio 2010中构建一个简单的应用程序,我这样做是为了熟悉用户控件,因为它似乎比创建多个表单更容易。
On the main form, there is a dropdown list the contains 2 values, " UCType1
" and " UCType2
". 在主窗体上,有一个下拉列表,其中包含2个值“
UCType1
”和“ UCType2
”。 I created 2 different user controls. 我创建了2个不同的用户控件。 I was using panel on the main form to display the user control according to what they selected in the dropdown list.
我在主窗体上使用面板根据他们在下拉列表中选择的内容显示用户控件。
I was able to display the appropriate user control based on the user selection, but now I ran into some issue. 我能够根据用户选择显示适当的用户控件,但现在我遇到了一些问题。 I couldn't get the main form to read data from the user control.
我无法获取主窗体来从用户控件中读取数据。
Let's say there is a button " Execute
" and a label " Warning
" on the main form. 假设主表单上有一个“
Execute
”按钮和一个标签“ Warning
”。 There is a textbox " Name
" on the user control. 用户控件上有一个文本框“
Name
”。
The scenario is: when the user hit " Execute
", if " Name
" is blank, " Warning
" will display some error message. 方案是:当用户点击“
Execute
”时,如果“ Name
”为空,“ Warning
”将显示一些错误消息。
I was trying to use if(UserControl1.Name.Text == "")
on the main form but I couldn't even reference it like that. 我试图在主窗体上使用
if(UserControl1.Name.Text == "")
,但我甚至无法像这样引用它。
I know I can just create separate forms to make it easier since that will make all variable in the same file, but I want to know if there's a way to do it with user control because I would like to get familiar with it. 我知道我可以创建单独的表单以使其更容易,因为这将使所有变量在同一个文件中,但我想知道是否有办法用户控制,因为我想熟悉它。
Thanks 谢谢
This is how I display my user control 这就是我显示用户控件的方式
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
UCType1 uc1 = new UCType1();
panel1.Controls.Clear();
panel1.Controls.Add(uc1);
}
and when I was trying to display the data from user control 当我试图显示来自用户控件的数据时
private void executeButton_Click(object sender, EventArgs e)
{
UCType1 uc1 = new UCType1();
warning.Text = uc1.Name;
}
nothing was happening. 一切都没发生。
Create one public property in UserControl
which set or get TextBox
text. 在
UserControl
创建一个公共属性,用于设置或获取TextBox
文本。 Like 喜欢
public String Name
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
Now you can access Name from your Form. 现在您可以从表单中访问名称。 like :
喜欢 :
if(UserControl1.Name == "")
There is problem with you code , you are creating object of control again rather than using existing control object 您的代码存在问题,您再次创建控件对象而不是使用现有控件对象
UCClientType1 uc1 = new UCClientType1();//access exsting object do not create new
warning.Text = uc1.Name;
as i provided my update below serach for the control you added and than access value 因为我提供了以下更新serach为您添加的控件和访问值
you need to do like this 你需要这样做
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
UCType1 uc1 = new UCType1();
uc1.Name = "uc1";
uc1.Id = "uc1";
panel1.Controls.Clear();
panel1.Controls.Add(uc1);
}
code changes in method shoudl be like this 方法中的代码更改应该是这样的
private void executeButton_Click(object sender, EventArgs e)
{
UCClientType1 uc =frm.panel1.Controls.FindControl("uc1",true) as UCClientType1;
if(uc1 != null)
warning.Text = uc1.Name;
}
just use this below code in your user control to get access to parent from 只需在您的用户控件中使用以下代码即可访问父级
Form parentForm = (this.Parent as Form);
var data = parentForm.textbox1.Text;
If you want to get usercontrol loaded in mainform than you can do this 如果你想在mainform中加载usercontrol而不是你可以这样做
UserControl uc =frm.Controls.FindControl("myusercontrol",true);
from.warning.Text = uc.Textbox1.Text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.