Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

  • *Experts*
Posted
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.
Posted

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.

  • *Experts*
Posted

I mean something like this:

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:

SharedObjects.TheTextBox.Something()

  • *Experts*
Posted

You might also want to restructure your classes.

 

1) You shouldn't use Modules at all. Ever. Use Classes with Shared members:

Module ThisIsBad
 Public Sub Blah(...)
 End Sub
End Module

'to call it
Blah(...)

Becomes

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.

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

Posted

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.

  • *Experts*
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...