I have two forms; I'll call them frmReturn and frmReturnItem. FrmReturn is bound to data in a SQL table; data entered in frmReturnItem is stored as a child table in SQL. In the database, there is a one-->many between Return and ReturnItem. frmReturnItem can be opened only from frmReturn.

frmReturn has a datagrid which displays the summary of the associated ReturnItems.

Problem: I would like the frmReturn's grid to refresh every time frmReturnItem is saved. I can post my existing code if that will help.

Thanks,

Mike

I have two forms; I'll call them frmReturn and frmReturnItem. FrmReturn is bound to data in a SQL table; data entered in frmReturnItem is stored as a child table in SQL. In the database, there is a one-->many between Return and ReturnItem. frmReturnItem can be opened only from frmReturn.

frmReturn has a datagrid which displays the summary of the associated ReturnItems.

Problem: I would like the frmReturn's grid to refresh every time frmReturnItem is saved. I can post my existing code if that will help.

Thanks,

Mike

This is a case where I would create an event for frmReturnItem that gets fired whenever the form's contents are saved. frmReturn subscribes to that event and refreshes the grid whenever it's fired. :)

This is a case where I would create an event for frmReturnItem that gets fired whenever the form's contents are saved. frmReturn subscribes to that event and refreshes the grid whenever it's fired. :)

Sounds good in theory...how do I point that event to objects in the other form? In frmReturn I have a column style csItemNo, for example. in frmReturnItem how do I specify csItemNo.MappingName, when frmReturnItem doesn't know about frmReturn's csItemNo?

Sounds good in theory...how do I point that event to objects in the other form? In frmReturn I have a column style csItemNo, for example. in frmReturnItem how do I specify csItemNo.MappingName, when frmReturnItem doesn't know about frmReturn's csItemNo?

frmReturnItem doesn't need to know anything about frmReturn, it just has to raise the event. Then frmReturn uses its super secret spy knowledge of itself to handle the event. :) Example time! You have a cleverly named parent form called Form1. Form1 has just a docked panel with a BackColor of blue or red. Form1 also creates and shows an object of Form2 in its Load event. Form2 has just a little button. The magic comes from how Form2 defines an event and raises it for the button click event and Form1 uses Form2's event to change the color of the panel.

Public Class Form1
  ' Make an event capable Form2
  Private WithEvents frm As Form2 = New Form2()

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    frm.Show()
  End Sub

  ' Handle the FormSaved event by flipflopping the panel color
  Private Sub frm_FormSaved() Handles frm.FormSaved
    If Panel1.BackColor = Color.Blue Then
      Panel1.BackColor = Color.Red
    Else
      Panel1.BackColor = Color.Blue
    End If
  End Sub
End Class




Public Class Form2
  ' Define an event to be raised
  Public Event FormSaved()

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' When the button is clicked, raise the FormSaved event
    RaiseEvent FormSaved()
  End Sub
End Class

The only thing Form1 knows about Form2 is its name and what events are available. The only thing Form2 knows about Form1 is absolutely nothing. :lol:

Ravalon,

Thanks for that, especially the code example. This is a new one for me. I'll let you know how it works out.

Mike

frmReturnItem doesn't need to know anything about frmReturn, it just has to raise the event. Then frmReturn uses its super secret spy knowledge of itself to handle the event. :) Example time! You have a cleverly named parent form called Form1. Form1 has just a docked panel with a BackColor of blue or red. Form1 also creates and shows an object of Form2 in its Load event. Form2 has just a little button. The magic comes from how Form2 defines an event and raises it for the button click event and Form1 uses Form2's event to change the color of the panel.

Public Class Form1
  ' Make an event capable Form2
  Private WithEvents frm As Form2 = New Form2()

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    frm.Show()
  End Sub

  ' Handle the FormSaved event by flipflopping the panel color
  Private Sub frm_FormSaved() Handles frm.FormSaved
    If Panel1.BackColor = Color.Blue Then
      Panel1.BackColor = Color.Red
    Else
      Panel1.BackColor = Color.Blue
    End If
  End Sub
End Class




Public Class Form2
  ' Define an event to be raised
  Public Event FormSaved()

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' When the button is clicked, raise the FormSaved event
    RaiseEvent FormSaved()
  End Sub
End Class

The only thing Form1 knows about Form2 is its name and what events are available. The only thing Form2 knows about Form1 is absolutely nothing. :lol:

Ravalon,

vb didn't like =; I have to use Panel1.BackColor.Equals.Color.blue Currently, I am calling the the child form with

Dim NewItem As New frmAddItem
NewItem.ReturnID = txtReturnID.Text
NewItem.ShowDialog()

Do I need to change how I call that form?

Currently, I am calling the the child form with

Dim NewItem As New frmAddItem
NewItem.ReturnID = txtReturnID.Text
NewItem.ShowDialog()

Do I need to change how I call that form?

You shouldn't need to. Events sort of happen independently of how you create and manipulate the forms.

Many thanks to Ravalon for the straightforward lesson in sending and receiving events across forms.

It's been very useful solving a problem I spent a lot of time fighting with, on a communications (triggering mscomm.output) programme.

Edited 7 Years Ago by jackthom: n/a

I am running VB.net version 2003
I have two forms - Main and Settings. Settings has a Public Sub called "Import_Settings" where a data file is read from the disk and refreshes all of the controls on the Settings form.

I need to be able to call that sub from the Main form in the load event to be able to determine another variable value. I read a previous example posted here but just cannot get it to work. Can someone assist me?

This article has been dead for over six months. Start a new discussion instead.