Changing the contents of a textbox via a module

Jay1b

Contributor
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
Could someone please tell me how can i change the contents of a textbox from a module in vb.net?

I know in VB5 i could just type

FormName.TextboxName.Text = "New Text"

But unless i am making a very stupid mistakes, this doesnt seem to work with vb.net - i have remembered to set the modifliers to Public.

Thanks
 
I get a blue line under - frmOptions.txtFields

When i hover over it, it say "Reference to a non-shared member requires and object reference"

Thanks.
 
If you do it like that you will always create a new instance of that form and in turn a new instance of the TextBox which might not be what you always want. If you simply want one instance to which you refer store it in a shared variable in a class.
 
Mutant, how can i store a textbox in a class? Or am i being completely stupid?

I know in VB5 i could simply change the data straight away, with the other form open in the background, and it would automatically update - that is what i am after now.
 
I mean something like this:
Visual Basic:
Public Class SharedObjects 'you can call it whatever you want
   Public Shared TheTextBox As TextBox 'store an instance in this variable
End Class
As the variable is shared it can be accessed from any part of your application by simply using:
Visual Basic:
SharedObjects.TheTextBox.Something()
 
You might also want to restructure your classes.

1) You shouldn't use Modules at all. Ever. Use Classes with Shared members:
Visual Basic:
Module ThisIsBad
  Public Sub Blah(...)
  End Sub
End Module

'to call it
Blah(...)
Becomes
Visual Basic:
Public Class ThisIsGood
  Public Shared Shared Blah(...)
  End Sub
End Class

'to call it
ThisIsGood.Blah(...)
Also, you shouldn't require your class to access objects in another class. Instead you should make the class containing the objects pass them into the other class.
Visual Basic:
Public Class Form1 : Inherits Form
  '....
  Dim c As New OutputClass()
  c.OutputData(Me.TextBox1)
End Class

Public Class OutputClass
  Public Sub OutputData(tb As TextBox)
    tb.Text = "Data"
  End Sub
End Class
That example allows the second class (OutputClass) to put data into the textbox in the first class (Form1) without having to actually have the instance of the second class. Rather than the second class fetching the object from the first class, the first class *gives* the object to the second class.

Sorry if that was a bit confusing. :p
 
Surprisiningly i do actually follow that, well kinda. :)

Public Class Form1 : Inherits Form

Is form1 the name of the form, that its associated with?, The name of the class itself? OR both? Inherits Form, i would imagine was by form, can i make it inherit more then one form?

I think i will use classes on future projects, but as i am half way through this one now - i will probably stick to modules.
 
Form1 is the name of your class, as for the name of the object you can call it whatever you want as long as its a valid name.
You cant inherit more than one object, Form object is the base for all your forms that you add controls and code too.
 
Back
Top