How to Get Data From User Control on Form
Solution 1
Create one public property in UserControl
which set or get TextBox
text. Like
public String Name
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
Now you can access Name from your Form. like :
if(UserControl1.Name == "")
Solution 2
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
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
UserControl uc =frm.Controls.FindControl("myusercontrol",true);
from.warning.Text = uc.Textbox1.Text;
Related videos on Youtube
sora0419
Updated on September 11, 2020Comments
-
sora0419 over 3 years
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.
On the main form, there is a dropdown list the contains 2 values, "
UCType1
" and "UCType2
". I created 2 different user controls. 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. There is a textbox "Name
" on the user control.The scenario is: when the user hit "
Execute
", if "Name
" is blank, "Warning
" will display some error message.I was trying to use
if(UserControl1.Name.Text == "")
on the main form but I couldn't even reference it like that.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.
-
Pranay Rana about 10 yearsis that worked for you ??
-
-
sora0419 about 10 yearson my main form, i used UserControl uc1 = new UserControl(); warning.Text = uc1.Name; but nothing was showing up in the warning label.
-
Sameer about 10 yearswhere you write `warning.Text = uc1.Name;
-
sora0419 about 10 yearsit wont let me compile because uc1 was not declared. Displaying user control is declared in ComboBox_SelectedIndexChanged() and warning.Text is set in Execute, 2 different function :/
-
sora0419 about 10 yearswhat do I put in "myusercontrol" if I never actually name it?
-
Sameer about 10 yearsDeclare uc1 as class variable outside of any method. and then initialize and use in which ever method you want.