none
Passing a textbox value to another form...

    Question

  • Hey there, im using vb 2008 and have come across a very baffling problem.

    I have 3 forms, the form one is an intro, form 2 loads a user and form 3 displays a user.

    i use the streamread / writer to load values on form 2, then when successful it loads form 3 and the vaules are transferred perfectly, now to save time i changed the start up form from form1 to form2 (saves having to skip every time). so after having all the code load and transfer i wanted to see it in action and then the issues started.

    Form1 (starts and i click the mouse to continue)

    Form2 (starts and loads a user)

    Form3 (fails to pass on values)

    yet if i set the start up form to form2 it passes the values again...

    Code is simple...

    form3.textbox1.text = textbox1.text

    i just cant understand how another form would stop this basic task from working! can anyone shed some light?
    Thursday, September 24, 2009 12:32 PM

Answers

  • SORTED- turns out to be the way i change forms, this is what i used:
    Dim form3 As New form3
    
                Me.Hide()
                form3.Show()
                form3.Visible = True
    i just had to remove the Dim form3 bit and the values pass fine.

    although it has been solved can any brainiac tell me why this did what it did?
    • Marked as answer by fugu996 Thursday, September 24, 2009 12:48 PM
    Thursday, September 24, 2009 12:48 PM

All replies

  • SORTED- turns out to be the way i change forms, this is what i used:
    Dim form3 As New form3
    
                Me.Hide()
                form3.Show()
                form3.Visible = True
    i just had to remove the Dim form3 bit and the values pass fine.

    although it has been solved can any brainiac tell me why this did what it did?
    • Marked as answer by fugu996 Thursday, September 24, 2009 12:48 PM
    Thursday, September 24, 2009 12:48 PM
  • You may take a look at this sample tutorial on different ways of passing data from one form to another
    http://www.codeproject.com/KB/cs/pass_data_between_forms.aspx  The code is C# but easy to understand. You can convert the code to vb at http://www.carlosag.net/Tools/CodeTranslator/

    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Thursday, September 24, 2009 12:52 PM
  • It sounds like you were creating a variable named form3 and now you're not?

    I think what's happening is there are two variables named form3. When you define a form, Visual Basic creates a single special instance of the form named after the form. In this case, that means it creates a form named "form3" that is of type form3. Unfortunately VB doesn't give you any clues that this form exists, you just have to know it.

    So I suspect you were saving the data into one form3, creating a second variable named form3, and then displaying the one that didn't have any data on it.

    In general you should either:

        1. Use the default instance defined by VB if you will only ever need one instance of the form

    Or

        2. Create your own instance with a different name and never use the default instance.

    I generally recommend the second approach because you can see where the form is defined and created so there's less chance for confusion.

    (Incidentally this default instance stuff is a throwback from way back in VB 3. It was one of the more common sources of very confusing mistakes in VB Classic and manym of us lobbied Microsoft to drop this in .NET. Alas, they didn't.)

    Rod

    Rod Stephens, Visual Basic MVP

    Visual Basic 2008 Programmer's Reference
    http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
    Thursday, September 24, 2009 1:02 PM
  • Thank you, makes life easier when you understand whats happening.

    Thursday, September 24, 2009 1:22 PM