8 Replies - 7413 Views - Last Post: 13 April 2011 - 09:03 AM Rate Topic: -----

#1 Googler20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-April 11

VB 10 Passing Variables Between Forms

Posted 12 April 2011 - 10:17 AM

Hello I am working on a program, and I would like to have the program pass a name from on form to another.

This is my code :

form 1 :
 Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
        Me.Hide()
        Form2.Show()      'to open next form 

    End Sub
End Class



form 2 :
 Public Class Form2

    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = Form1.TextBox1.Text   'so the name put into textbox1 on form 1 will transfer to form 2's textbox 1
        Me.Hide()
        Form3.Show()
    End Sub

End Class 


This is what I have tried. I want what is put into textbox1 (by the user) to transfer to form 2 with the click of the button. But it does not transfer. There are no errors, but in form 2 my textbox is empty.

Any suggestions on what I am doing wrong?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: VB 10 Passing Variables Between Forms

#2 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2506
  • View blog
  • Posts: 4,615
  • Joined: 24-September 10

Re: VB 10 Passing Variables Between Forms

Posted 12 April 2011 - 10:35 AM

Here is an MSDN article on it:
http://msdn.microsof...5(v=vs.80).aspx

there are also many tutorials in the tutorial section of DIC that cover it as well.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,142
  • Joined: 12-June 08

Re: VB 10 Passing Variables Between Forms

Posted 12 April 2011 - 10:36 AM

Have you tried exposing your form2's text property as a property of the form?

What I mean is

form2:
public property User
get
return TextBox1.Text
set (byval value string)
TextBox1.Text = value
end property





Then in form1 right before you do "Form2.Show()" do a
form2.User = form1's textbox




You could also just make the control exposed, but meh - that's not fun.
Was This Post Helpful? 1
  • +
  • -

#4 minalira143  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-April 11

Re: VB 10 Passing Variables Between Forms

Posted 12 April 2011 - 10:45 AM

View PostGoogler20, on 12 April 2011 - 10:17 AM, said:

Hello I am working on a program, and I would like to have the program pass a name from on form to another.

This is my code :

form 1 :
 Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
        Me.Hide()
        Form2.Show()      'to open next form 

    End Sub
End Class



form 2 :
 Public Class Form2

    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = Form1.TextBox1.Text   'so the name put into textbox1 on form 1 will transfer to form 2's textbox 1
        Me.Hide()
        Form3.Show()
    End Sub

End Class 


This is what I have tried. I want what is put into textbox1 (by the user) to transfer to form 2 with the click of the button. But it does not transfer. There are no errors, but in form 2 my textbox is empty.

Any suggestions on what I am doing wrong?

Thanks


There's nothing wrong in your code. When you press the button of form1, the form2 will appear, and if you press the form2's button, form2 will copy the form1.textbox1's text,
to fomr2.textbox2. and then form2 will me come hide.
It is how I understand your code if we'll just base on the given code.

Try first to delete the statement that will hide the form and see what will happen.

I hope this will help.
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: VB 10 Passing Variables Between Forms

Posted 12 April 2011 - 11:07 AM

What you have should work since you're using the default instance so my question is, did you actually type anything into the textbox on form1 when testing this?
Was This Post Helpful? 0
  • +
  • -

#6 Googler20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-April 11

Re: VB 10 Passing Variables Between Forms

Posted 12 April 2011 - 11:44 AM

I do not want to have to push the button on form 2 for the textbox1 to display form 1's textbox1, I would like as soon as form 2 opens for the name to be visible already.

View Postminalira143, on 12 April 2011 - 10:45 AM, said:

View PostGoogler20, on 12 April 2011 - 10:17 AM, said:

Hello I am working on a program, and I would like to have the program pass a name from on form to another.

This is my code :

form 1 :
 Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
        Me.Hide()
        Form2.Show()      'to open next form 

    End Sub
End Class



form 2 :
 Public Class Form2

    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = Form1.TextBox1.Text   'so the name put into textbox1 on form 1 will transfer to form 2's textbox 1
        Me.Hide()
        Form3.Show()
    End Sub

End Class 


This is what I have tried. I want what is put into textbox1 (by the user) to transfer to form 2 with the click of the button. But it does not transfer. There are no errors, but in form 2 my textbox is empty.

Any suggestions on what I am doing wrong?

Thanks


There's nothing wrong in your code. When you press the button of form1, the form2 will appear, and if you press the form2's button, form2 will copy the form1.textbox1's text,
to fomr2.textbox2. and then form2 will me come hide.
It is how I understand your code if we'll just base on the given code.

Try first to delete the statement that will hide the form and see what will happen.

I hope this will help.


View PostCharlieMay, on 12 April 2011 - 11:07 AM, said:

What you have should work since you're using the default instance so my question is, did you actually type anything into the textbox on form1 when testing this?


Yes I am entering something into textbox on form1, I am entering a name, but it does not appear onto form2.
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: VB 10 Passing Variables Between Forms

Posted 12 April 2011 - 02:07 PM

Try this

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
        Me.Hide()
        Form2.TextBox1.Text = Me.TextBox1.Text '<--- Added this line
        Form2.Show()      'to open next form 
    End Sub

Was This Post Helpful? 0
  • +
  • -

#8 minalira143  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-April 11

Re: VB 10 Passing Variables Between Forms

Posted 13 April 2011 - 09:01 AM

Why don't you try to put you event in form2.Load?

try this one, put this in form2.Load event


Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     TextBox1.Text = Form1.TextBox1.Text 
End Sub




If you want to put the value of textbox in form1 to form2 automatically. Just double click the form2 to view it's Load event. I hope it will help.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,142
  • Joined: 12-June 08

Re: VB 10 Passing Variables Between Forms

Posted 13 April 2011 - 09:03 AM

Yes or add a parametrized constructor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1