C # Calling a method from another form

Hi I have 2 Form Form1 and Form2

Form1 has a table and there are my records, and there is an empty space for updating the table in Form1.

Form2 is my insert form. I am inserting data into sqlserver.I want after I save the record in Form2 to run Form1 Refresh void. (when Form1, Form2 is open)

thanks.

+2
source share
4 answers

Form 2 must have a link to an instance of Form1. You can submit this link to Form2 by clicking the paste button:

Form2 insertForm = new Form2(); //Form2.ShowDialog(Me); - Correction - 'Me' is for VB. in C# it's: Form2.ShowDialog(this); 

Further in Form2 you can access Form1 as follows:

 (Form1)this.Parent.RefreshTable(); 
+5
source

In Form1, when you open Form2, you join the Form2 OnClose/Closed event or the user Save event, which, when raised, updates the table in form 1.

0
source

Adjust the design for Form2 to get an additional parameter, for example:

 ... private Form1 mainWindow; public Form2(Form1 mainWindow) { this.mainWindow = mainWindow; } ... public void HaveSavedSql() { this.mainWindow.RefreshAll(); } 

However, you should consider looking at Interfaces so that you can share the problems. Interfaces are useful for many things!

0
source

try this call the method from another form:

 if (System.Windows.Forms.Application.OpenForms["ParentFormName"] != null) { (System.Windows.Forms.Application.OpenForms["ParentFormName"] as ParentFormName).MethodName(args); } 
0
source

All Articles