Question Multiple Forms

Joined
Oct 9, 2009
Messages
14
Programming Experience
Beginner
Background:

OK, so I have two forms. Form1 is a bunch of user interface objects, Form2 is a datagridview. I want to make it so that when I click a button Page1 the program will store the user information and then create a SQL Query to fill From2.

Problem:

When I click the button the other form is created, but the grid does not fill. The weird thing is that while trouble shooting this I created a button on form 2 that uses all of the same subroutines as the button on form 1, but for some reason the button on form one does not work, while the one on form 2 does.


Code for form1 button (doesn't work):

VB.NET:
Public Sub btnCompile_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnCompile.MouseClick

        Dim frmInstance As Form3
        frmInstance = New Form3

        Dim frmInstance1 As MDIParent1
        frmInstance1 = New MDIParent1

        MDIParent1.CreateForm3()
        Form3.Load_and_Manupulate()

    End Sub



code for form2 button (works):


VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call populateDataGridView1()
    End Sub



Code within the sub:

VB.NET:
  Public Sub populateDataGridView1()

        Dim cn As New OdbcConnection("Dsn=INSPDBP1_ms;uid=" & ID & ";server=INSPDBP1;pwd=" & pass)

        Try
            Dim SQL As String
            'build the SELECT statement
            SQL = String.Format("SELECT ........")
             Dim cmd As New OdbcCommand(SQL, cn)
            'open the connection
            cmd.Connection.Open()

            'get the DataReader object from command object
            Dim rdr As OdbcDataReader = cmd.ExecuteReader()
            'check if it has any rows
            If rdr.HasRows Then
                With Me.DataGridView1
                    'remove existing rows from grid
                    .Rows.Clear()
                    'get the number of columns
                    Dim ColumnCount As Integer = rdr.FieldCount
                    'add columns to the grid
                    For i As Integer = 0 To ColumnCount - 1
                        .Columns.Add(rdr.GetName(i), rdr.GetName(i))
                    Next
                    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
                    'loop through every row
                    While rdr.Read
                        'get all row values into an array
                        Dim objCells(ColumnCount - 1) As Object
                        rdr.GetValues(objCells)
                        'add array as a row to grid
                        .Rows.Add(objCells)
                    End While
                End With
            Else
                'display message if no rows found
                MessageBox.Show("Not found")
                Me.dataGridView1.Rows.Clear()
            End If
            'clear up the resources
            rdr.Close()
        Catch ex As Exception
            'display if any error occurs
            MessageBox.Show("Error: " & ex.Message)
            'close the connection if it is still open
            If cn.State = ConnectionState.Open Then
                cn.Close()
            End If
        End Try
    End Sub
 
Last edited by a moderator:
First up, please post in the most appropriate forum for the topic and not just the first one you come to. Thread moved.

As for the question, it looks like a case of "default instances strike again" to me. In that first code snippet you are explicitly creating an instance of Form3 and then assigning it to a variable:
VB.NET:
frmInstance = New Form3
but then, instead of referring to that instance using that variable, you refer to the default instance using the class name:
VB.NET:
Form3.Load_and_Manupulate()
You're referring to two different objects so what you do to one doesn't affect the other.

If you don't know what a default instance is then follow the Blog link in my signature and read my submission on the subject.
 
Thank you for that, I opted for Method 1, so that I can have multiple versions of the form. This way the user can compare two sets of data if they want. The code for form2 now just fills the datagrid upont the forms creation. Below is is the code i used to make it an MDI application.




Code for From1:

Public Sub btnCompile_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnCompile.MouseClick

Dim F3 As New Form3()
F3.MdiParent = MDIParent1
F3.Show()


Code for form 2:

Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call populateDataGridView1()

end sub




P.S.
Sorry about the wrong topic.
 
Last edited:
Back
Top