Access a Control from any Class
How can I access a TextBox on Form1 from an Class,
which is in the same Project.
Cann anyone post an short example?
Thank you
Also, you'll need to make sure that the Modifiers property on the control is set to Friend or Public, so it's "visible" outside the parent form. Then, you can refer to the public variable that refers to your form, and work with the control as a property of that form.
For example, if you have code like this:
Public frm As MyForm
then somewhere else, you add
frm = New MyForm()
and if you've set TextBox1 so that it's visible outside MyForm's class, then you should be able to use code like this from any other class:
MessageBox.Show(frm.TextBox1.Text)
for example instead of changing the access modifier for the form member , create a new method/property to access the form functionality.
class Form1 // get the person detail
{
public string GetPersonName()
{
return textBox1.text;
}
}
just my 2 cents
Dim frm As New Form1()
Sub Test()
frm.Textfield1.text = "Test"
End sub
nothing really happens with the form when I call the Sub.
hmmm.... :-) Maybe you can help me.
Anyway, nothing happens with the form because you never displayed the form. You need:
Dim frm As New Form1()
Sub Test()
frm.Textfield1.text = "Test"
frm.Show()
End sub
The fact that TextField1 showed up in the IntelliSense list of available properties should tell you that the concept is working. Also, if you're only using the variable frm in one place, it can be local to the procedure. I just assumed you wanted to be able to get at it from multiple locations.