Forms talking to one another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sc5502
    New Member
    • Jun 2014
    • 102

    Forms talking to one another

    Background:

    Front end- MS Visual Studio/Visual Basic 2017
    Back end- MS SQL Server 2016


    I have two forms (Form1 and Form2). I know how to have Form2 pass data back to Form1. Can I have Form2 execute some code (specifically a sub routine) in Form1?

    Thanks in advance.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Yes, make the function in Form1 public (or Friend) so that it can be accessed in Form2.

    If you cannot make this function Shared (static in C#) then you will also have to provide a means to set a reference to Form1 in Form2.

    You can do this by:
    • Providing a constructor in Form2 that takes an instance of Form1 so that it can save a reference to it
    • Implementing a Public (or Friend) method in Form2 that sets a reference to Form1
    • Implementing a Public (or Friend) property in Form2 that allows Form1 set a reference to itself

    Comment

    • sc5502
      New Member
      • Jun 2014
      • 102

      #3
      Thanks. It works except the subroutine in form 1 has code in it to load a data grid view.
      Code:
       Dim qry As String = "select recd_id, recd_number, recd_inventorydate from t021400_records order by recd_number desc;"
              Dim cs As String = "SERVER=techdev01;DATABASE=DB02_RECORD_MANAGEMENT;USER ID=db02;PASSWORD=db02;"
              Try
                  Dim connectionString As String = cs
                  Dim connection As New SqlConnection(connectionString)
                  Dim dataadapter As New SqlDataAdapter(qry, connection)
                  Dim ds As New DataSet()
                  connection.Open()
                  dataadapter.Fill(ds, "dgv")
                  ' dataadapter.Fill(ds)
                  connection.Close()
                  dgv_Records.DataSource = DBNull.Value
                  dgv_Records.DataSource = ds
                  dgv_Records.DataMember = "dgv"
      
      
                  With dgv_Records
                      .RowsDefaultCellStyle.BackColor = Color.Bisque
                      .AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
                  End With
      
      
              Catch EX As Exception
                  MsgBox(EX.ToString & " - (Error)")
                  Exit Sub
              End Try
      The dgv_records (the datagridview) get an error "Cannot refer to an instance member of a class from within a shared member or shared member initializer without an explicit instance of the class." Any ideas?

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Shared/Static methods cannot use anything that is not also Shared/Static (or will be created and destroyed within the method)

        Your dgv_Records DataGridView cannot be a static/shared item because it is tied to the instances of the form.

        Either remove the shared modifier from the method or think of another way to refresh your grid (IE raise an event that the form listens for or something)

        Comment

        Working...