comcrack Posted June 14, 2003 Posted June 14, 2003 hy, In visual basic .net, how can i access to mainform.textbox1.text in other forms??? Thanck you ComCrack Quote [ () /\/\ [ |\ /\ [ |<
*Experts* mutant Posted June 14, 2003 *Experts* Posted June 14, 2003 Kepp an instance of your main form or the textbox in some public variable or pass it around your forms. Quote
comcrack Posted June 14, 2003 Author Posted June 14, 2003 how can i do that?? --> i'm begining i vb .net Quote [ () /\/\ [ |\ /\ [ |<
*Experts* mutant Posted June 14, 2003 *Experts* Posted June 14, 2003 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: 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 form's 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. Quote
wyrd Posted June 14, 2003 Posted June 14, 2003 Another solution is to make a public property in your main form to access private members. public string SomeText { get { return Textbox1.Text; } set { Textbox1.Text = value; } } You can then use it as such in other forms; MainForm.SomeText = "Hello there."; string str = MainForm.SomeText; Console.Writeline(str + "-" + MainForm.SomeText); With this solution you don't 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 you'd have to do is change the code in the main form and not have to worry about breaking the code in others. Quote Gamer extraordinaire. Programmer wannabe.
comcrack Posted June 14, 2003 Author Posted June 14, 2003 in lienform 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 Quote [ () /\/\ [ |\ /\ [ |<
wyrd Posted June 14, 2003 Posted June 14, 2003 "Dim mainframe As mainframe" Change to Dim mainframe As New mainframe Quote Gamer extraordinaire. Programmer wannabe.
comcrack Posted June 14, 2003 Author Posted June 14, 2003 I can't because mainframe is already loadded Quote [ () /\/\ [ |\ /\ [ |<
*Experts* mutant Posted June 14, 2003 *Experts* Posted June 14, 2003 Thats why I showed you how to pass instances to use it in other form :) Quote
comcrack Posted June 14, 2003 Author Posted June 14, 2003 but it doesn't work. I dont know why Quote [ () /\/\ [ |\ /\ [ |<
*Experts* mutant Posted June 14, 2003 *Experts* Posted June 14, 2003 Can you show your constructor of second form and how you call it, and where you use the instance? Quote
comcrack Posted June 14, 2003 Author Posted June 14, 2003 (edited) There is my project: [edit] Attachment removed - please don't post binaries [/edit] Edited June 15, 2003 by divil Quote [ () /\/\ [ |\ /\ [ |<
*Experts* mutant Posted June 14, 2003 *Experts* Posted June 14, 2003 I changed your LienForm file and in the MainFrame change your Lien form declaration to: Dim LienForm As New LienForm(me) It will pass in the reference to your form and then use it to access the textbox.lienform.vb 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.