Results 1 to 14 of 14

Thread: Form2 - access Form1's textbox [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Form2 - access Form1's textbox [Resolved]

    This must be real simple.

    From form1, I call up form 2.

    now, in form2, I'd like to MessageBox.Show the text in Form1's TextBox1.

    I've obviously tried

    VB Code:
    1. MessageBox.Show Form1.Textbox1.Text

    and that is wrong.

    How do I access this property?
    Last edited by mendhak; May 5th, 2004 at 03:51 PM.

  2. #2

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    OK, I created a module and declared both forms there. Then, it was pretty simple. The IDE complied, and I was happy.


  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Question: Is what I did the correct way?

  4. #4
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    It's fine. The only problem I've found with declaring the forms in a module is when you close a form, you can't reopen it. I had a main form that could call different forms. The secondary forms would get disposed of when they were closed.
    Someone did post a workaround to it, passing the calling form to the secondary form, but I'd reworked the form by then.

  6. #6
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    umm ive always handled forms in module and have never come across the problem of not being able to reopen a form, i know you need to be careful when dealing with forms because you can open them when they dont really exist, all i can really say at this moment in time is if you close it make the module variable nothing then try reopening it

    so use onclosing event
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "This must be real simple.

    From form1, I call up form 2.

    now, in form2, I'd like to MessageBox.Show the text in Form1's TextBox1."


    Unless I'm crazy the answer IS real simple.

    try
    In form1
    VB Code:
    1. Dim frm2 As New fclsTest
    2.  
    3.         frm2.TextBox2.Text = Me.TextBox1.Text
    4.         frm2.ShowDialog()

    in form2 Load event

    VB Code:
    1. MessageBox.Show(TextBox2.Text)
    Last edited by taxes; May 5th, 2004 at 07:17 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    You could also use:

    In form1

    VB Code:
    1. Dim form2 as new fclsTest
    2. form2.MesBox(me.TextBox1.Text)
    3. form2.ShowDialog

    In form 2

    VB Code:
    1. Public Sub MesBox(Message1 as String)
    2. messagebox.show(Message1)
    3. End Sub


    I expect there are other ways also
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dear Mendhak, the only not strange way (because often I like to use very personal strategy ) I have successfully tested in your situation, is declare Form1 as public, in a module. Form2 should be visible, anyway, from Form1, being declared and instanced inside it and so, if you need only to do like similar at what you described, Taxes suggestion is good (always if I well understand ):
    Instance Form2 (inside Form1), then write into its controls or public variables what you need and then show it...or something similar. In this case you'd not need to put any form in a module.
    Someone said to me a Form Child can access its father, but I've never used, nor verified, it.
    Beg your pardon for my caotich english!
    Live long and prosper (Mr. Spock)

  10. #10
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Originally posted by carlblanchard
    umm ive always handled forms in module and have never come across the problem of not being able to reopen a form, i know you need to be careful when dealing with forms because you can open them when they dont really exist, all i can really say at this moment in time is if you close it make the module variable nothing then try reopening it

    so use onclosing event
    Well, I get an error when I try it, saying the form's already disposed.

    Module: declare form1 and form2. Show form1
    form1: show form2
    form2: blah blah, click the little X close button, return to form1
    form1: show form2 -> error, form2 is disposed

    I know I could eliminate the X button, Hide instead of Close, etc. But it's an error I get.

  11. #11
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Yes Salvelinus, if you close the form then you have to reinstance it, but now you do it locally (in Form1), not in a module, and the new instance will not be accessible from everywhere. Anyway this last condition should not be a problem for you. Stay valid that you can instance Form2 and at the next line you can write into it. Isn't it?
    Live long and prosper (Mr. Spock)

  12. #12
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I found it easier to just declare form2 in the form1 event that shows it. I don't really reference anything from one form on another that often.

  13. #13

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Yes, I could also have passed the value to a public variable in Form2. I was wanting to avoid the hassle of having to do that though. Reminiscent of VB6 days

  14. #14

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    And thanks for your input, everyone.

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