locked
passing 2 textbox values to another form RRS feed

  • Question

  • I have 2 textboxes on form1 that I want to pass to 2 labels on form2, I used the Get Property to do this but i am getting errors, can anybody show me how to do this, I am new to visual basic

    Thanks

    Steve

    Sunday, September 26, 2010 2:41 PM

Answers

  • Thanks Frank, but can you give me a better example for passing 2 values, I have no problem passing 1 value

    Thanks for replying

    Steve


    Hi Steve,

    I'm assuming that Form1's textboxes are named TextBox1 and TextBox2 and that Form2's labels are Label1 and Label2. If not, modify it as needed but it's simply:

    Form2.Label1.Text = TextBox1.Text
    Form2.Label2.Text = TextBox2.Text

    This would be called FROM your Form1 of course.

    • Proposed as answer by HrBill32 Wednesday, September 29, 2010 10:47 PM
    • Marked as answer by Bin-ze Zhao Thursday, September 30, 2010 8:15 AM
    Wednesday, September 29, 2010 8:45 PM

All replies

  • you can pass the string values to the new Form's constructor. In this example from Deborah's blog:

    https://msmvps.com/blogs/deborahk/archive/2009/09/21/passing-data-between-forms-constructor.aspx

    you'll see the argument in the Sub New being passed to the form. Then in the Form Load event set the Label Text property to the respective string variable. Using the variable in the example in the link above:

    Private Sub Form2_Load ....
     Label1.Text = passedInText
    End Sub
    
    Sunday, September 26, 2010 3:01 PM
  • Another way that you can do it is directly from Form1

    In Form1, you'd do something like this:

    Form2.Label1.Text = TextBox1.Text

    Sunday, September 26, 2010 4:05 PM
  • Thanks Frank, but can you give me a better example for passing 2 values, I have no problem passing 1 value

    Thanks for replying

    Steve

    Wednesday, September 29, 2010 8:42 PM
  • Thanks Frank, but can you give me a better example for passing 2 values, I have no problem passing 1 value

    Thanks for replying

    Steve


    Hi Steve,

    I'm assuming that Form1's textboxes are named TextBox1 and TextBox2 and that Form2's labels are Label1 and Label2. If not, modify it as needed but it's simply:

    Form2.Label1.Text = TextBox1.Text
    Form2.Label2.Text = TextBox2.Text

    This would be called FROM your Form1 of course.

    • Proposed as answer by HrBill32 Wednesday, September 29, 2010 10:47 PM
    • Marked as answer by Bin-ze Zhao Thursday, September 30, 2010 8:15 AM
    Wednesday, September 29, 2010 8:45 PM
  • of course, in OOP terms, you should not access an object (in this case the Label) in a class (the Form2 class) from another class (Form1) in this way.

    the objects in different classes should be "unaware" of each other. That is why you should pass the TextBox.Text value to the Form2 constructor, and set the value of the parameter to a variable within that class.

    just some food for thought :)

    Hi Frank

     

    Wednesday, September 29, 2010 10:18 PM
  • Once Again A really easy way would be to go in to settings, of the project and make a string call text1 and text2 ect...  and make it so when the user presses enter, or a button ect... It loads that string with the textbox value. so

     

     Dim settings As New My.MySettings

      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                 My.Settings.text1 = textbox1.text

      My.Settings.text2 = textbox2.text


        End Sub

    Ok now make a new form (form2)

    Dim settings As New My.MySettings

    On.load...

    label1.text = My.Settings.text1

    Dont copy this code Retrace it, Follow what i said and U dont need that to save the text as a button u can make it a check box... user press enter... U get it Well Good luck!

    Wednesday, September 29, 2010 10:59 PM
  • of course, in OOP terms, you should not access an object (in this case the Label) in a class (the Form2 class) from another class (Form1) in this way.

    the objects in different classes should be "unaware" of each other. That is why you should pass the TextBox.Text value to the Form2 constructor, and set the value of the parameter to a variable within that class.

    just some food for thought :)

    Hi Frank

     


    Sorry for the delay - Hey Joe!

    Another way would be to create two public string variables. I don't know if it's strictly OOP or not, but it works! :)

    Thursday, September 30, 2010 1:50 PM
  • Just because it works doesn't mean it is a proper design!!!

    If you were in my shop I would not want to clean up your code after you were fired!

    Saturday, October 2, 2010 1:31 PM