In vb.net application I have Form1 & Form2.
When user enter data and save it by btnsave event in form1, the Form2 should show msgbox "New entry" in form2 while its still open.

How can set this event?

Dani AI

Generated

— the error you saw (“Msgbox is not the member of Form2”) happens because MsgBox/MessageBox.Show are not instance methods of your form class. The correct pattern is to expose a public method or property on the actual Form2 instance (or raise an event), then call that method on the same Form2 object that is already shown. Calling New Form2() will create a new window instead of updating the open one.

Here are two safe approaches.

  1. Public method on Form2 + find the open instance
    
    ' In Form2.vb
    Public Class Form2
     Public Sub NotifyNewEntry()
         MessageBox.Show(Me, "New entry saved.")   ' owned by Form2
     End Sub
    End Class

' In Form1.vb (after save completes)
Dim f2 As Form2 = Nothing
For Each f As Form In Application.OpenForms
If TypeOf f Is Form2 Then
f2 = DirectCast(f, Form2)
Exit For
End If
Next
If f2 IsNot Nothing Then f2.NotifyNewEntry()


2) Event-based (decoupled)
```vb
' In Form1.vb
Public Event DataSaved(ByVal sender As Object, ByVal e As EventArgs)
' after saving:
RaiseEvent DataSaved(Me, EventArgs.Empty)

' In Form2.vb (subscribe to the Form1 instance when opening Form2)
AddHandler Form1.DataSaved, AddressOf OnForm1DataSaved

Private Sub OnForm1DataSaved(sender As Object, e As EventArgs)
    MessageBox.Show(Me, "New entry saved.")
End Sub

Notes and troubleshooting:

  • If you follow ’s label idea, update it through a public method/property on Form2 instead of manipulating controls from Form1. That keeps encapsulation clean.
  • Don’t call New Form2() when you want to update the visible Form2; that’s the most common mistake.
  • If the save runs on a background thread, marshal back to the UI thread with Invoke/BeginInvoke.
  • Using VB default form instances (e.g., Form2.NotifyNewEntry()) can work but be careful if your app creates multiple instances.

This keeps the UI update explicit, avoids the error you saw, and works whether Form2 was opened earlier or is created later.

Recommended Answers

All 4 Replies

On your code bellow the new entry add this

 Form2.MsgBox("Your Message Here")

To you code.

Sorry i tried the above code and i got the error msg
Msgbox is not the member of form2

Add a label to your Form2.

Ow you can do as ddanbe said or put a label in your Form1, your choice and if you want to display a message then you can just hide the label and point Form1 to send message to this label then on Form2 point it to retrieve this message.

commented: Good advice. +15
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.