uneek_18 Posted October 2, 2003 Posted October 2, 2003 Im a newbie in vb.net, please help me. 1. I have a two forms. form1 and form2 2. form1 has a textbox1 in it and a button1, which when clicked will show form2. 3. form2 has a textbox1 and a button1 also. 4. I want to pass the value of the textbox in form2 to the textbox in form1 when the user clicks the button in form2 Thanks Quote
aewarnick Posted October 2, 2003 Posted October 2, 2003 Declare a static variable at the top of the class, Form2. public static string txtbox2=""; Then you can access it by using the namespace.Form2. Or, better yet, In your Form2's constructor (the method with the same name as the class; has InitializeComponent in it), do this: public Form2(Form f) { this.otherForm= f; //otherForm is a variable you will have to declare at the top. When you use otherForm in your code, it will point to f, and f points to the Form passed to it. } The call from Form1: Form2 f2=new Form2(this); To access the textbox in Form1 from Form to, you must make it public. Quote C#
uneek_18 Posted October 2, 2003 Author Posted October 2, 2003 Thank you How about a code of the same problem in vb.net? Thanks again Quote
*Experts* mutant Posted October 2, 2003 *Experts* Posted October 2, 2003 'in your second form from which you want to edit the first one... Dim firstform As Form1 Public Sub New(byval formone As Form1) firstform = formone InitializeComponent() MyBase.New() End Sub Now you can access the first from with the firstform variable. And now when declaring Form2, pass in the instance of form1 to it: Dim f2 As New Form2(Me) Quote
uneek_18 Posted October 3, 2003 Author Posted October 3, 2003 Thanks a lot mutant i really need that I can see that vb.net has really become much like an Object Oriented PL. Im having a hard time in moving to vb.net. It has really changed a lot. Any Tip? Quote
aewarnick Posted October 3, 2003 Posted October 3, 2003 Learn C# instead! A whole new experience! Quote C#
*Experts* mutant Posted October 3, 2003 *Experts* Posted October 3, 2003 Once you learn the concept of object orientation you will discover that it is one of the most useful things in programming :). Quote
Keroleen Posted October 17, 2003 Posted October 17, 2003 To add a question to this ... I can do the above (pass value from one form to another)..but how about this: I have an Admin section in my application, where the admin user will change a value (like 1 or 2). This value is passed to another form (we'll call this form UserData). This works fine for me via the above tips. Now for me, What i need is if a normal access level user comes into the UserData form and does his thing but he doesnt have to worry about the value. The value should already be set by the admin person and static throughout all normal access user forms. What i am finding is now, i can pass a value but once i close the form, the value is lost. Is there a way in Visual Basic.net to 'store' the value statically into another form. My forms will be accessed via a menu system so there will always be a form open. Thanks in advance Quote
*Experts* mutant Posted October 17, 2003 *Experts* Posted October 17, 2003 To store a variable that way, the best way would be to create a class with a shared member, which is the same as static, and use that. You said something about saving that info somehow. With the example above Im assuming that you want to save it while the app is running, if you need to save that value and the app is exited then you would have to save that value to a file or however you want. Quote
Keroleen Posted October 17, 2003 Posted October 17, 2003 Thank you for your reply ... but can you explain in more detail how to do that (maybe an example pls)..(im kinda new to this...):o Do I create the shared variable (this is the same concept as globals in C++ right?) in the main menu form or in any of the forms i want to work with ? Thanks so much.. :( :confused: Quote
*Experts* mutant Posted October 18, 2003 *Experts* Posted October 18, 2003 Shared is similar to a global variable. It can be accessed everywhere. Here is an example of what I was talking about: Public Class SharedVars 'you can call the class anything you want Shared varname As VarType 'make it shared End Class Now you can access it from anywhere in your program like this: SharedVars.varname :) 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.