Results 1 to 10 of 10

Thread: Pass a variable from child form to parent form.

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2007
    Posts
    34

    Pass a variable from child form to parent form.

    This is a bit complicated.

    I have a form called fMain, which most of the action in my program happens.

    Then in a function of that form I instance a form called fBeam.

    Then in fBeam, I instance a new form called fSection.


    How can I pass the user selection in fSection to fBeam?

    Thanks

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Pass a variable from child form to parent form.

    When you say the user selection what exactly are you referring to? If its just something like a selection from a combobox then cant you just reference the fSection.MyComboBox.SelectedItem property from fBeam?
    Obviously that example would only work with a combobox but you get the idea
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2007
    Posts
    34

    Re: Pass a variable from child form to parent form.

    Well, the idea is that a big complicated form creates a new instance of another really complicated form in which the user selects a bunch of options and then closes that new form. The results of which get transfered back to the parent.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Pass a variable from child form to parent form.

    Well the same thing still applies, if you everything you want to 'transfer' back to the parent is visual stuff in the controls (e.g change a textbox's text or select a checkbox etc) then you could do this in the click event of your OK button on your fSection form or something similar. All you need to do to set the properties of controls on another form is just put the form instance name before the control name, like so:
    vb Code:
    1. fBeam.MyTextBox.Text = MyTextBoxOnAnotherForm.Text

    However, if its not all properties of controls that you want to share and you are just wanting to pass variables accross the forms then you have a couple of options. I'm fairly new to doing this kind of thing myself so someone with more experience may be able to give you a better solution but I believe you could either declare the variables as Public (I think this is bad practice though) or you could declare the variables as Shared Properties of the main form... orrr you could create your own object that would store all of these variables and pass that accross the forms. Again, I'm quite new to sharing things properly accross forms so sorry if this isn't that helpful!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2007
    Posts
    34

    Re: Pass a variable from child form to parent form.

    The form I'm passing the variables to is an instance. There may be multiple instance of this class. I can't just use the class name now can I?

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Pass a variable from child form to parent form.

    Well then you could declare the variables as Public in a Module and that way they are accessible to everything all the time... but like I said before, I think this is considered a bad practice and someone with more experience will probably post soon with a better solution
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2007
    Posts
    34

    Re: Pass a variable from child form to parent form.

    You're still not getting it. This is an instance of a class. I can't manipulate the instance of a class based on the class name.

  8. #8

    Thread Starter
    Member
    Join Date
    Dec 2007
    Posts
    34

    Re: Pass a variable from child form to parent form.

    Nevermind, I figured it out for my self. The code looked something like:


    Code:
                    
    Dim fForm As fSBeam
    fForm = Me.Owner
    fForm.oBeam = Me.oCalc
    oBeam and oCalc are objects that store various bits of information in this calculation.

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Pass a variable from child form to parent form.

    Quote Originally Posted by NewtonsBit
    You're still not getting it. This is an instance of a class. I can't manipulate the instance of a class based on the class name.
    Declaring a variable as public in a module doesnt require you to use any class names at any point either in the declaration or the usage...
    but anyway, as long as you got it sorted then it doesnt matter eh.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: Pass a variable from child form to parent form.

    Check out this thread. There's an example of what you need in post number 6 http://www.vbforums.com/showthread.p...ighlight=form2

    Class properties are preferred as opposed to using publid properties as they follow the OOP paradigm, or methodology of programming.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

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