Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Pass value from One form to another

Pass value from One form to another

Scheduled Pinned Locked Moved Windows Forms
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NetDeveloper09
    wrote on last edited by
    #1

    Hi i have a form1 with one textbox1 and button1 when i click the button another form2 loads which has one textbox1 and button one when i type alfanumeric values in the textbox of form2 and press the button1 of form2 it is not displaying the value in form1 textbox1. I am using property approach. My code is as: Form One:

    Public Class Form1

    Public \_form2 As Form2 = New Form2
    
    Private Sub btnList\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
        \_form2.Show()
    
    End Sub
    
    Public WriteOnly Property \_textBox() As String
    
        Set(ByVal Value As String)
    
            tbxVal.Text = Value
    
        End Set
    
    End Property
    

    End Class

    Form Two

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim frm As Form1 = New Form1
        Value = \_textBox1
        Me.Close()
    
    End Sub
    
    Public ReadOnly Property \_textBox1() As String
        Get
            Return tbxValue1.Text
        End Get
    End Property
    
    Private Sub Form2\_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim fm As Form1 = New Form1
        fm.tbxVal.Text = Value
    End Sub
    
    A P 2 Replies Last reply
    0
    • N NetDeveloper09

      Hi i have a form1 with one textbox1 and button1 when i click the button another form2 loads which has one textbox1 and button one when i type alfanumeric values in the textbox of form2 and press the button1 of form2 it is not displaying the value in form1 textbox1. I am using property approach. My code is as: Form One:

      Public Class Form1

      Public \_form2 As Form2 = New Form2
      
      Private Sub btnList\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
          \_form2.Show()
      
      End Sub
      
      Public WriteOnly Property \_textBox() As String
      
          Set(ByVal Value As String)
      
              tbxVal.Text = Value
      
          End Set
      
      End Property
      

      End Class

      Form Two

      Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

          Dim frm As Form1 = New Form1
          Value = \_textBox1
          Me.Close()
      
      End Sub
      
      Public ReadOnly Property \_textBox1() As String
          Get
              Return tbxValue1.Text
          End Get
      End Property
      
      Private Sub Form2\_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
          Dim fm As Form1 = New Form1
          fm.tbxVal.Text = Value
      End Sub
      
      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      .NetDeveloper09 wrote:

      Dim frm As Form1 = New Form1

      You are actually creating a new instance of form1 here. This instance is not aware of the value you set in the first instance. Create a property in form2 and then set it in the form1 btnList_Click. You should then be able to get your textbox value in form2.

      The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick - Silverlight *.XCP files. Visit the Hindi forum here.

      N 2 Replies Last reply
      0
      • A Abhinav S

        .NetDeveloper09 wrote:

        Dim frm As Form1 = New Form1

        You are actually creating a new instance of form1 here. This instance is not aware of the value you set in the first instance. Create a property in form2 and then set it in the form1 btnList_Click. You should then be able to get your textbox value in form2.

        The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick - Silverlight *.XCP files. Visit the Hindi forum here.

        N Offline
        N Offline
        NetDeveloper09
        wrote on last edited by
        #3

        hi i under stand, some one has replied to me which i believe is the answer but i have forgot how to do it. "You need to retrieve the contents of the form2 TextBox control before form2 is disposed. To do that, you have to handle the Closing event for form2 inside form1. In that event handler, you simply use your property to retrieve the contents of the Form2.TextBox, and do whatever you need to with it."

        1 Reply Last reply
        0
        • A Abhinav S

          .NetDeveloper09 wrote:

          Dim frm As Form1 = New Form1

          You are actually creating a new instance of form1 here. This instance is not aware of the value you set in the first instance. Create a property in form2 and then set it in the form1 btnList_Click. You should then be able to get your textbox value in form2.

          The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick - Silverlight *.XCP files. Visit the Hindi forum here.

          N Offline
          N Offline
          NetDeveloper09
          wrote on last edited by
          #4

          I tried your way its not working, any other suggestion.

          J 1 Reply Last reply
          0
          • N NetDeveloper09

            I tried your way its not working, any other suggestion.

            J Offline
            J Offline
            johannesnestler
            wrote on last edited by
            #5

            :doh: ... you are allocating ("New") another instance in your Ok and Closed-Handler - this is a simple mistake. Your code smells like "beginner-style" maybe you want to learn about "variable scope"? Solution: 1. Make a public property for Form2 which will give you the textbox.Text (you did that) 2. Read the property after showing the Form -> Simple, isn't it? 3. Sit down and read a book about programming before you do further coding... (variable scope is always a thing you need to know, in any language)

            Public Class Form1

              Public \_form2 As Form2 = New Form2
            
              Private Sub btnList\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
                    \_form2.Show()
                    tbxVal.Text = \_form2.textBox1
            
              End Sub
            
              Public WriteOnly Property \_textBox() As String
            
                    Set(ByVal Value As String)
            
                          tbxVal.Text = Value
            
                    End Set
            
              End Property
            

            End Class

            N 1 Reply Last reply
            0
            • N NetDeveloper09

              Hi i have a form1 with one textbox1 and button1 when i click the button another form2 loads which has one textbox1 and button one when i type alfanumeric values in the textbox of form2 and press the button1 of form2 it is not displaying the value in form1 textbox1. I am using property approach. My code is as: Form One:

              Public Class Form1

              Public \_form2 As Form2 = New Form2
              
              Private Sub btnList\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
                  \_form2.Show()
              
              End Sub
              
              Public WriteOnly Property \_textBox() As String
              
                  Set(ByVal Value As String)
              
                      tbxVal.Text = Value
              
                  End Set
              
              End Property
              

              End Class

              Form Two

              Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

                  Dim frm As Form1 = New Form1
                  Value = \_textBox1
                  Me.Close()
              
              End Sub
              
              Public ReadOnly Property \_textBox1() As String
                  Get
                      Return tbxValue1.Text
                  End Get
              End Property
              
              Private Sub Form2\_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
                  Dim fm As Form1 = New Form1
                  fm.tbxVal.Text = Value
              End Sub
              
              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              This is a really common problem, and it's almost always answered incorrectly. Never, ever, attempt to use the instance scope to control the passing backwards and forwards of data between two modeless forms. Look to use delegate/events instead, and have the first form subscribe to an event on the second form which will be fired when it needs to return the data.

              I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

              My blog | My articles | MoXAML PowerToys | Onyx

              1 Reply Last reply
              0
              • J johannesnestler

                :doh: ... you are allocating ("New") another instance in your Ok and Closed-Handler - this is a simple mistake. Your code smells like "beginner-style" maybe you want to learn about "variable scope"? Solution: 1. Make a public property for Form2 which will give you the textbox.Text (you did that) 2. Read the property after showing the Form -> Simple, isn't it? 3. Sit down and read a book about programming before you do further coding... (variable scope is always a thing you need to know, in any language)

                Public Class Form1

                  Public \_form2 As Form2 = New Form2
                
                  Private Sub btnList\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
                        \_form2.Show()
                        tbxVal.Text = \_form2.textBox1
                
                  End Sub
                
                  Public WriteOnly Property \_textBox() As String
                
                        Set(ByVal Value As String)
                
                              tbxVal.Text = Value
                
                        End Set
                
                  End Property
                

                End Class

                N Offline
                N Offline
                NetDeveloper09
                wrote on last edited by
                #7

                Knowledge has no boundaries any one who struggles for it he gains the knowledge, one should not underestimate the person on the other side and should not pass remarks that hurt other poeple or discourage people. You should show respect to people who are in your type of field. If you donot like to answer any question you are free to do that. For your information i have already done it without your support. Kind Regards Mirza

                L 1 Reply Last reply
                0
                • N NetDeveloper09

                  Knowledge has no boundaries any one who struggles for it he gains the knowledge, one should not underestimate the person on the other side and should not pass remarks that hurt other poeple or discourage people. You should show respect to people who are in your type of field. If you donot like to answer any question you are free to do that. For your information i have already done it without your support. Kind Regards Mirza

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  I suggest you take the advice given to you, even when at first sight you don't like it much. As for your problem, there is at least a dozen ways to "solve" it, unfortunately only one or two of these ways are decent, all others are bad, i.e. they work in a simple situation and will bite you when things become more complex. It takes time and experience to learn it all, and most of all an open mind. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                  N 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    I suggest you take the advice given to you, even when at first sight you don't like it much. As for your problem, there is at least a dozen ways to "solve" it, unfortunately only one or two of these ways are decent, all others are bad, i.e. they work in a simple situation and will bite you when things become more complex. It takes time and experience to learn it all, and most of all an open mind. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                    N Offline
                    N Offline
                    NetDeveloper09
                    wrote on last edited by
                    #9

                    <Thank you for the advice, i always appriciate good advice, you are right there are and its true also time is the only teacher, and most of all with an open mind is true and will to strugle in life and always give your thoughts a possitive direction, if one do that ones mind will always be open. >

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups