Click to See Complete Forum and Search --> : VB.net control manipulation
Bantam
January 4th, 2003, 06:25 PM
How can I manipulate a control in one form from another form? It was easy in VB 6, but in .net I can't seem to figure it out. It's not as simple as:
formname.textbox.text = "I've got mail, yayyy!"
Thanks for the help
TheCPUWizard
January 4th, 2003, 09:52 PM
Well it is and it isn't
In VB6 the controls were effectively public members, in .NET they are private members.
Most OOP developers agree (at least to some point) that data (which is effectively what the control is) should be encapsulated. One of the reasons involves object tracking and meaning. Since a control needs to always have a "parent" control or form, it would be a "bad thing" if a reference to a control was maintained after the "parent" no longer existed. This is insured by making the controls private.
In general, it is better to add a method/property what would allow external items to access the information you desire:
e.g:
Sub FormWithControls.SetSpecialTest(data as string)
MyControl.Text = data
End Sub
It is possible to "short circuit" this, but it is typically not wise and will tend to cause problems over the life of any significant project. Therefore the following is NOT recommended (at least by me!
:p .... simply cahnge the controls variable declaration from private to public.
Bantam
January 4th, 2003, 10:06 PM
The reason I want to manipulate another form's controls is because I'm bringing up a 'search' form for a database... it'll have a bunch of text boxes that you could put data into to search for a specific record in a database. I want to use the connection, adapter, dataset, and sqlcommand controls off the parent form, so I don't have to duplicate.
Any better suggestions? I could code them as methods, but that sounds like it'll get really hairy, really quick, especially when I want to fill a datagrid on the child form with data from the dataset of the parent form...
I'm not sure how to declare a control public, btw... thanks for the help.
Jym
January 5th, 2003, 12:05 PM
are you trying to fill a datagrid with a record set ? Just declare the RecordSet in a modual and then it will be Public and you will be able to use the RecordSet to fill a datagrid on any form
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.