question

JamieHayashi-3240 avatar image
0 Votes"
JamieHayashi-3240 asked JamieHayashi-3240 commented

Calling Method to the other form.

Good Day, I have a problem calling a method in other form. I have a code and the visual studio don't detect any error but my code did not run, I don't know why. Here's my code.

Form 1

private void btnenter_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.UpdateGridView();
}


Form 2

public void UpdateGridView()
{
//CODE HERE...

}


anybody can told what, where's the wrong of the code??



dotnet-csharpmicrosoft-authenticatormicrosoft-graph-applications
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

anybody can told what, where's the wrong of the code??

Your code creates a new instance of Form1. The code does not affect any existing instance of Form1.

The standard approach is moving shared code to a separate class file or class library that each forms can call. In this case, I assume you need an event and even handler. Form1 triggers an event and Form2 handles the event. Any parameters are passed as event arguments. See the C# programming guide.

Events overview (Windows Forms .NET)
Events (C# Programming Guide)


0 Votes 0 ·

Sir have you a example code sir.

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered JamieHayashi-3240 commented

@JamieHayashi-3240, Welcome to Microsoft Q&A, you could try to use Application.OpenForms Property to get what you wanted.

Here is a code example you could refer to.

Form1:

      public partial class Form1 : Form
         {
             public Form1()
             {
                 InitializeComponent();
                    
             }
        
             private void button1_Click(object sender, EventArgs e)
             {
                 Form2 form = (Form2)Application.OpenForms["Form2"];
                 form.UpdateGridView();
             }
        
             private void Form1_Load(object sender, EventArgs e)
             {
                 Form2 form2 = new Form2();
                 form2.Show();
             }
         }
         
 Form2:
    
   public partial class Form2 : Form
     {
         public Form2()
         {
             InitializeComponent();
         }
         public void UpdateGridView()
         {
             dataGridView1.Rows[1].Cells[1].Value = 23;
         }
    
         private void Form2_Load(object sender, EventArgs e)
         {
             DataTable dt = new DataTable();
             dt.Columns.Add("Name");
             dt.Columns.Add("Age");
             dt.Rows.Add("test1", 22);
             dt.Rows.Add("test2", 32);
             dt.Rows.Add("test3", 12);
             dataGridView1.DataSource= dt;
    
         }
     }

Result:

199052-2.gif



Hope this could help you.

Best Regards,
Jack



If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



2.gif (21.9 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Think's Jack

I already fix this bug., Here my code.. Thank you so much.

 //form1
  private void Button1_Click(object sender, EventArgs e)
         {
             frm1FrontDesk_new frontdesk = new frm1FrontDesk_new(this);//for methods
             frontdesk.Show();
    
         }
   public void ViewerGrid()//methods for viewer
             { 
 try{con.open();
 //code here
 con.close();
 }catch()
    
 {
 con.close();
 }
    
 }
 //Note: in my pass code I forgot to put (this) to call this form.. sorry my mistake.. :)
 //************************************
 //form2 
    
 public partial class frm1FrontDesk_new : Form
 {
  public frm1FrontDesk_new(frm1FrontDesk frm1)//calling methods
         {
             InitializeComponent();
    
             frm = frm1;
         }
         frm1FrontDesk frm = new frm1FrontDesk();
    
 }
  private void btnenter_Click(object sender, EventArgs e)
         {
    
 //code here//
      frm.ViewerGrid();//for methods
  this.Close();
 }


0 Votes 0 ·