By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,983 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,983 IT Pros & Developers. It's quick & easy.

Forms talking to one another

100+
P: 102
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.
Feb 21 '18 #1
Share this Question
Share on Google+
3 Replies


Frinavale
Expert Mod 5K+
P: 9,731
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
Feb 22 '18 #2

100+
P: 102
Thanks. It works except the subroutine in form 1 has code in it to load a data grid view.
Expand|Select|Wrap|Line Numbers
  1.  Dim qry As String = "select recd_id, recd_number, recd_inventorydate from t021400_records order by recd_number desc;"
  2.         Dim cs As String = "SERVER=techdev01;DATABASE=DB02_RECORD_MANAGEMENT;USER ID=db02;PASSWORD=db02;"
  3.         Try
  4.             Dim connectionString As String = cs
  5.             Dim connection As New SqlConnection(connectionString)
  6.             Dim dataadapter As New SqlDataAdapter(qry, connection)
  7.             Dim ds As New DataSet()
  8.             connection.Open()
  9.             dataadapter.Fill(ds, "dgv")
  10.             ' dataadapter.Fill(ds)
  11.             connection.Close()
  12.             dgv_Records.DataSource = DBNull.Value
  13.             dgv_Records.DataSource = ds
  14.             dgv_Records.DataMember = "dgv"
  15.  
  16.  
  17.             With dgv_Records
  18.                 .RowsDefaultCellStyle.BackColor = Color.Bisque
  19.                 .AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
  20.             End With
  21.  
  22.  
  23.         Catch EX As Exception
  24.             MsgBox(EX.ToString & " - (Error)")
  25.             Exit Sub
  26.         End Try
  27.  
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?
Feb 22 '18 #3

Frinavale
Expert Mod 5K+
P: 9,731
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)
Feb 28 '18 #4

Post your reply

Sign in to post your reply or Sign up for a free account.