Question : VB.Net 2008 Problem passing data between forms

I have an app that I am working on and I am trying to test if Form2 is open from Form4. If it is I want to clear the datagridview on Form2. Both forms are children of mdiparent `main`. This code, although no error thrown, does not work.

The datagrid is not bound to a dataset. It was populated from values entered into textboxes.

I am at a loss here and it is probably something simple but my simple mind can not figure it out.

Thanks in advance for any help.

1:
2:
3:
4:
5:
For Each f As Form In main.MdiChildren
                    If TypeOf f Is Form2 Then
                        Form2.dgVol.Rows.Clear()
                    End If
                Next

Answer : VB.Net 2008 Problem passing data between forms

Instead of:

    If frm.Name = "Form2" Then

I would use:

    If TypeOf frm is Form2 Then

To get rid of the name issues I always convert to lower or upper case when performing matches against names like that:

    If ctrl.Name.ToLower = "dgvol" Then

You could actually SEARCH for it using this code:
*searching in this manner is NOT case sensitive!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
        For Each frm In main.MdiChildren
            If TypeOf frm Is Form2 Then
                Dim f2 As Form2 = CType(frm, Form2)
                Dim ctls() As Control = f2.Controls.Find("dgvol", True)
                If ctls.Length > 0 AndAlso TypeOf ctls(0) Is DataGridView Then
                    Dim dgv As DataGridView = CType(ctls(0), DataGridView)
                    dgv.Rows.Clear()
                    Exit For
                End If
            End If
        Next
Random Solutions  
 
programming4us programming4us