Results 1 to 5 of 5

Thread: Not Getting valuefrom another form

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Mumbai
    Posts
    31

    Not Getting valuefrom another form

    when I click on the button1 on the form1 then I am able to go to form2 but not getting the value in the txtform2 text box.


    Here is the code which i am tring
    under form1:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim frm As New Form2()
    frm.Show()
    End Sub


    under form2:

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim frm1 As New Form1()
    txtForm2.Text = frm1.txtForm1.Text
    End Sub



    Reason expected !!

  2. #2
    Lively Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    95
    'Dim frm1 As New Form1()' this line of code creates a brand new instance of Form1 so any values you extract from it will be it's default values. You can set up a member variable in Form2 to pass the Value from Form1.

    i.e.

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim frm As New Form2() 
    frm.valueFromFrom1 = Me.txtForm1.Text 
    frm.Show() 
    End Sub
    then in form2

    Code:
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    txtForm2.Text = Me.valueFromForm1 
    End Sub

  3. #3
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Or you could create a new constructor and pass the text box value when you create the new form 2 like:

    VB Code:
    1. Dim frm As New Form2(txtform1.Text)

  4. #4
    Lively Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    95
    if you can add it as a constructor parameter, doesn't this make the form incompatible with the form designer?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    19
    No its not going to make it incompatiable with designer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width