Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

C#
  • *Experts*
Posted

'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)

Posted

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?

  • 2 weeks later...
Posted

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

  • *Experts*
Posted

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.

Posted

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:

  • *Experts*
Posted

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

:)

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...