Jay1b Posted September 9, 2003 Posted September 9, 2003 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 Quote
Administrators PlausiblyDamp Posted September 9, 2003 Administrators Posted September 9, 2003 If the textbox is public that should work. Does it give any errors? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jay1b Posted September 9, 2003 Author Posted September 9, 2003 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. Quote
Jay1b Posted September 9, 2003 Author Posted September 9, 2003 (Well its actually a listbox, but i cant see that makes any difference). Quote
Administrators PlausiblyDamp Posted September 9, 2003 Administrators Posted September 9, 2003 forms are now classes and need to be treated as such dim f as new frmOptions f.txtFields = "New Text" f.Show() Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* mutant Posted September 10, 2003 *Experts* Posted September 10, 2003 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. Quote
Jay1b Posted September 10, 2003 Author Posted September 10, 2003 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. Quote
*Experts* mutant Posted September 10, 2003 *Experts* Posted September 10, 2003 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() Quote
*Experts* Volte Posted September 10, 2003 *Experts* Posted September 10, 2003 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(...)BecomesPublic 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 ClassThat 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 Quote
Jay1b Posted September 11, 2003 Author Posted September 11, 2003 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. Quote
*Experts* mutant Posted September 11, 2003 *Experts* Posted September 11, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.