Karlx Swanovski
Karlx Swanovski

Reputation: 2989

Transfer value of Form to Usercontrol

I create a user control and add a textbox to it. In my windows form I add the user control i created and add a textbox and a button. How to copy the text I input from the textbox of Form to textbox of Usercontrol and vice versa. Something like usercontrol.textBox1.text = textBox1.text

Upvotes: 0

Views: 7278

Answers (4)

Karlx Swanovski
Karlx Swanovski

Reputation: 2989

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 ID
{
    set { textBox1.Text = value; }
}

Upvotes: 2

Reza Shirazian
Reza Shirazian

Reputation: 2353

Although some stuff are inherited when creating a custom user control, for the most part you have to define your own properties. (like text value, etc..)

I would take a look at this: http://msdn.microsoft.com/en-us/library/6hws6h2t.aspx good luck!

Upvotes: 0

YoryeNathan
YoryeNathan

Reputation: 14532

You could add to your User Control code a public property that delegates into the TextBox's Text property:

public string MyTxtBoxValue { get { return this.txtBox.Text; } }

And you could also have a setter to that, of course, if needed.

What you don't want to do, however, is exposing the whole TextBox by making it public. That is flawed.

Upvotes: 2

George Johnston
George Johnston

Reputation: 32278

There are multiple ways to access your user control text box data. One way to accomplish this would be to expose the text box on the user control at a scope that can be accessed via the form it's loaded on. Another way would be raising an event on the button click of the user control and subscribing to it on the parent form.

Upvotes: 0

Related Questions