Results 1 to 6 of 6

Thread: Different Forms

  1. #1

    Thread Starter
    Lively Member FYRe's Avatar
    Join Date
    Aug 2005
    Location
    Singapore
    Posts
    102

    Different Forms

    Let's say I have a TextBox1; on my MainForm which contains a value of 4.
    I added a new WindowsForm and created a TextBox2 on my new form. How do I extract/get the value, 4 from my MainForm ?



    thks,
    FYRe
    sOMEONE'S gONNA dO iT, wHY nOT yOU ?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Different Forms

    You can either have the first form set a value in the second form, or you can have the second form retrieve the value from the first form. In the first case the first form needs a reference to the second form. In the second case the second form needs a reference to the first form.

    If you want to use the second case, you could leave the TextBox as Friend or change it to Public and then the second form can retrieve that value directly. My preference would be to declare the TextBox as Private and then create a Public method or ReadOnly property in the first form that returns the value, then have the second form call that method or property.
    Last edited by jmcilhinney; Sep 13th, 2005 at 11:31 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member FYRe's Avatar
    Join Date
    Aug 2005
    Location
    Singapore
    Posts
    102

    Re: Different Forms

    In short, I want to pass a value from my MainForm to my new WindowsForm. Could someone show me an example?

    ----------------------------------------
    MainForm:



    ----------------------------------------
    NewForm:



    ----------------------------------------




    thks,
    FYRe
    sOMEONE'S gONNA dO iT, wHY nOT yOU ?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Different Forms

    There are two ways. You can define a method in the NewForm class that takes the desired value as an argument, and then call that method from the MainForm class. This method could be a constructor if you want. The other option would be to define a property in the NewForm class and set that property from the MainForm. I'm assuming you already know how to define and call a method, and if you're not familiar with defining properties then you can use the example I gave you in your previous thread. You wouldn't make the property ReadOnly in this case of course. In fact, you could choose to make it WriteOnly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member FYRe's Avatar
    Join Date
    Aug 2005
    Location
    Singapore
    Posts
    102

    Re: Different Forms

    I'm terribly sorry, I don't quite understand all the jargons.
    So, here, I attached some screenshots; a similar example of what I'm doing.

    Referring to " Form2codes.JPG ",

    I want the value of TextBox1 in Form 2 to be 4, as in Form1.
    Attached Images Attached Images    
    sOMEONE'S gONNA dO iT, wHY nOT yOU ?

  6. #6
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Different Forms

    You could put a value to the constructor of your form2.
    Expand the region windows form designer code
    VB Code:
    1. dim mynumber as integer<----your variable
    2. 'then put a parameter to your sub new
    3.  Public Sub New(number as integer)
    4.         MyBase.New()
    5.  
    6.         'This call is required by the Windows Form Designer.
    7.         InitializeComponent()
    8.         me.mynumber=number<---pass the value
    9.         'Add any initialization after the InitializeComponent() call
    10.  
    11.     End Sub
    12. 'then you can have the value
    13. 'in your form2 load event
    14. me.textbox1.text=mynumber.tostring()
    15.  
    16. 'in calling your form2
    17. dim f as new form2(number)
    18. f.showdialog()

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