Windows Develop Bookmark and Share   
 index > Windows Forms General > Accessing Controls
 

Accessing Controls

How would i go about accessing a control in another form...Like if enter text in one txtbox and click a button I want that text to show up in another textbox on another form when its loaded....do i need to make my controls public???
Tryin2Bgood  Wednesday, October 04, 2006 1:04 PM

You dont need to make your controls public, rather use a public propery on the other form and keep a reference of the form on the main form from where you want to set that value and set value of that property which should have code to set that value ultimately to the control.

like this:

Form2 form2 = new Form2();

form2.Show();

form2.SetText = "This is some Text";

in form to Define a property like this:

public string SetText

{

get { return this.textBox1.Text; }

set { this.textBos1.Text = value; }

}

Cheers ;-)

}

RizwanSharp  Wednesday, October 04, 2006 1:22 PM

you could make your controls public but that would be bad design, as suggested, use the properties to access a control.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=729974&SiteID=1

same thing as the link above and the response given already. Only thing that really needs to be modified is to access the control from the control's owner form so in the example in the link instead of doing:

get{ return this.theName; } etc....

should be:

get { return this.theControl.Text; }

set { this.theControl.Text = value; }

 

If you are trying to access the parent form control from the child form, same thing applies and you would still have to pass a reference of the parent to the child form as shown in the example

ahmedilyas  Wednesday, October 04, 2006 1:29 PM

You dont need to make your controls public, rather use a public propery on the other form and keep a reference of the form on the main form from where you want to set that value and set value of that property which should have code to set that value ultimately to the control.

like this:

Form2 form2 = new Form2();

form2.Show();

form2.SetText = "This is some Text";

in form to Define a property like this:

public string SetText

{

get { return this.textBox1.Text; }

set { this.textBos1.Text = value; }

}

Cheers ;-)

}

RizwanSharp  Wednesday, October 04, 2006 1:22 PM

you could make your controls public but that would be bad design, as suggested, use the properties to access a control.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=729974&SiteID=1

same thing as the link above and the response given already. Only thing that really needs to be modified is to access the control from the control's owner form so in the example in the link instead of doing:

get{ return this.theName; } etc....

should be:

get { return this.theControl.Text; }

set { this.theControl.Text = value; }

 

If you are trying to access the parent form control from the child form, same thing applies and you would still have to pass a reference of the parent to the child form as shown in the example

ahmedilyas  Wednesday, October 04, 2006 1:29 PM

You can use google to search for other answers

Custom Search

More Threads

• Routing of User Input through .net framework
• Confused about behavior of SystemEvents.SessionSwitch
• Windows form loading with form Back ground Image
• custom connection string in sqlconnection
• Anyone got experiance with CRC16?
• VB.NET 2005 - Treeview won't sort properly
• how to select then next node in treeview control?
• multi demensional array question
• moving mouse to a control position
• TreeView selected node back color