Referencing another form

I can't figure out how to do this using VB.Net... I was able to do it using VB6:

I have two forms (form1 and form2).  Form1 has a button that opens Form2 (on top of Form1).  The user clicks on a value in a flexgrid on Form2, and I want Form2 to close, and the value they clicked on to be populated in a grid that I have on Form1.  I couldn't find out how to reference the form1 and it's flexgrid from form2 without having to create a new instance of Form1 (dim Form1 as new Form1), which is not what I want. I want to put the value on the original Form1.   

Can anyone help


Answer this question

Referencing another form

  • zhackwyatt

    We're always glad to help.  Glad you got it working.  :) 
  • mosaic

    yes, definitely easier than my way!  :P  I sometimes forget about KISS (Keep It Simple Stupid)
  • Adrian Accinelli

    I'm going to propose a simpler solution. Rather than adding events, and having Form2 attempt to "push" information into Form1, which it really doesn't have a reference to, how about "pulling" data from Form2 back into Form1  That is, if Form2 has a NewValue property (which might contain the value you want to retrieve from Form2), you could write code like this in Form1:
        Dim frm As New Form2
        frm.ShowDialog()
        Me.TextBox1.Text = frm.NewValue
        frm.Dispose()
    And that should be all you need. In this case, you wait for the user to close Form2, and when that happens, you retrieve its NewValue property, then Dispose it. Works here...

  • Tomcatter

    Unlike VB6, there's no such thing as default instances of forms anymore in VB.NET.  You have a couple options, but I think this one is the cleanest and will work best for you.

    Setup a Property on your Form2 that can hold a reference to your instance of Form2, so you can do whatever you'd like with Form1 from Form2.  Something like this should work.


    <b>'Inside Form 2</b>

    Private _Form1Reference As Form1

    Public Property Form1Reference As Form1
         Get
              Return _Form1Reference
         End Get
         Set (ByVal Value As Form1)
              _Form1Reference = Value
         End Set
    End Property

    <b>'Inside your Button Click Event that launches Form2 from Form1</b>

    Dim Temp As New Form1()

    Temp.Form1Reference = Me

    Temp.ShowDialog()

    <b>'Inside whatever event fires where you want it to affect Form1 from Form2 and close itself</b>

    'this is just an example...do whatever is necessary to Form1 by accessing the Form1Reference Property we created earlier
    Form1Reference.SomeProperty = "blah"

    Me.Close()


    Hope this helps  :) 

  • JDigital007

    Ken and Erik,

    Thank you so much.  I ended up using Erik's suggestion and it worked perfectly.  You saved me from having another nightmare day :)    Thanks again.

    Diane

  • Xcalibur37

    Well, there are always tradeoffs. Using the technique you proposed gives you more flexibility, but it's more complex. I went for the "get it done as simply as possible" route, which doesn't provide as much flexibility. There's no free lunch! <g>
  • Referencing another form