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

Dani AI

Generated

had the right idea: keep frmReturnItem blind to frmReturn and just signal, "I saved something." The parent can then reload the child rows for the current ReturnID. If you want the parent grid to refresh while the child stays open (multiple saves), raise a custom event that carries the ReturnID; if the child closes on save, you can also use DialogResult (see note below).

' --- Child: frmReturnItem ---
Public Class ReturnItemSavedEventArgs
    Inherits EventArgs
    Private mReturnId As Integer
    Public ReadOnly Property ReturnId() As Integer
        Get : Return mReturnId : End Get
    End Property
    Public Sub New(ByVal returnId As Integer) : mReturnId = returnId : End Sub
End Class

Public Class frmReturnItem
    Public Event Saved(ByVal sender As Object, ByVal e As ReturnItemSavedEventArgs)

    Private mReturnID As Integer
    Public Property ReturnID() As Integer
        Get : Return mReturnID : End Get
        Set(ByVal value As Integer) : mReturnID = value : End Set
    End Property

    Private Sub btnSave_Click(...) Handles btnSave.Click
        ' 1) Persist to SQL (transaction recommended), then:
        RaiseEvent Saved(Me, New ReturnItemSavedEventArgs(mReturnID))
        ' If you close after save instead, you can do:
        ' Me.DialogResult = DialogResult.OK : Me.Close()
    End Sub
End Class

' --- Parent: frmReturn ---
Private Sub OpenItemEditor()
    Dim dlg As New frmReturnItem()
    dlg.ReturnID = CInt(txtReturnID.Text)
    AddHandler dlg.Saved, AddressOf OnItemSaved
    dlg.Show(Me) ' or ShowDialog(Me)
End Sub

Private Sub OnItemSaved(ByVal sender As Object, ByVal e As ReturnItemSavedEventArgs)
    ' 2) Requery only the affected rows and rebind
    ReturnItemAdapter.FillByReturnId(ds.ReturnItem, e.ReturnId)
    CType(Me.BindingContext(ds, "ReturnItem"), CurrencyManager).Refresh()
End Sub

Notes and tips:

  • Do not touch frmReturn’s grid or column styles (like csItemNo) from frmReturnItem; keep that mapping logic in frmReturn only, as you suspected.
  • If you close the child on save, you can skip events: If dlg.ShowDialog(Me) = DialogResult.OK Then ReloadItemsForReturn(...).
  • If your grid shows an aggregate summary, re-execute the summary query after save; Refresh() alone will not pull new rows.

Recommended Answers

All 10 Replies

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.

Thank You Sir for the Explanation with Code.

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?

Sorry for the trouble. I figured it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.