Access textbox

comcrack

Well-known member
Joined
Jun 14, 2003
Messages
52
Location
Canada
hy,

In visual basic .net, how can i access to mainform.textbox1.text in other forms???


Thanck you
ComCrack
 
If you start your program from sub main just declare your form as public beofre starting it. If you start from a form you can either pass the instance around through constructors or make some public variable in a module.
To pass instance through constructors:
Let say Form1 is your main form and Form2 is some other form.
In Form2:
Code:
Dim formmain as Form some variable to hold the instance
pass in the form instance
Public Sub New(byval mainform as form1) get the variable that 
contains the forms instance
MyBase.New
InitializeComponents()
formmain = mainform get the instance to a variable that
can be used in this throughout this class
End Sub

in some other sub

public sub DoSomething()
formmain.sometextbox.text = "some text" use the variable with
that instance to access the textbox on the form.
end sub
You can do that, or declare a public variable.
 
Another solution is to make a public property in your main form to access private members.

C#:
public string SomeText {
   get {
      return Textbox1.Text;
   }
   set {
      Textbox1.Text = value;
   }
}

You can then use it as such in other forms;

C#:
MainForm.SomeText = "Hello there.";
string str = MainForm.SomeText;
Console.Writeline(str + "-" + MainForm.SomeText);

With this solution you dont have to worry about changing code in multiple forms if you happen to change the textbox to another control (a label for example) or rename the textbox for some reason, all youd have to do is change the code in the main form and not have to worry about breaking the code in others.
 
in lienform
Code:
dim mainframe as mainframe
private sub button1_click(byval sender as system.object,byval e as system.eventargs)hangles button1.click
mainframe.text1.text = "HY"
me.close()
end sub

Error : additional information: object reference not set to an instance of an object.

What can I do??

The mainframe is already loadded and lienform is loadded by mainframe.



Think you
ComCrack
 
There is my project:

[edit] Attachment removed - please dont post binaries [/edit]
 
Last edited by a moderator:
I changed your LienForm file and in the MainFrame change your Lien form declaration to:
Code:
Dim LienForm As New LienForm(me)
It will pass in the reference to your form and then use it to access the textbox.
 

Attachments

Back
Top