sora0419
sora0419

Reputation: 2378

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.

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.

Upvotes: 4

Views: 27299

Answers (2)

Pranay Rana
Pranay Rana

Reputation: 176896

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;

Upvotes: 2

Sameer
Sameer

Reputation: 2171

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 == "")

Upvotes: 5

Related Questions