Access a Control from any Class

Hi.
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
[147 byte] By [codefund.com] at [2007-12-16]
# 1
You'll need to somehow maintain a reference to the form after you open it, so you can refer to the control. If you want to get to it from any class, then you'll need a public variable in one class (or in a module, in VB.NET) that refers to the form.

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)

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
acessing the forms control directly from other class, is a bad practice especially when you're talking about OO, you should wrap the form to do specific work, encapsulates as much as possible.expose as little as you could. you'll be grateful if you start this form the beginning

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

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Thank you very much.
I'm going to implement some Functions to
access the Controls. You really helped me a lot.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
Well... Its not really working. :-)
I have 2 Files. Form1 and a Class1.
All the controls are set to "friend".
What exactly do I have to write into
the 2 Files?
If I do this in the Class1 file:

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.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
First of all, I must confess that I agree TOTALLY with Erymuzuan Mustapa's comment--I would never, ever expose a control as a public member of a form. But to keep this simple...

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.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6
Ah, I think now I got it. :-)
Thank you for your Comments. I've only programmed in VB6 before
and the OOP stuff in VB.net is pretty new to me.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7
just to throw a comment on top of all of this: UserControls are great for wrapping functionality, like sections of forms that will be used more than once on different forms, etc. Just like everything else, they're just a class, so you can write whatever methods you need to wrap functionality of your "form" to use when you have instances of your UserControls on all of your Forms.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...