Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have one form, named Form1 (main one) and another one, named Form2. In Form1 I have a method which interact with a textBox control in Form1. I want to call this method in Form2.



---------------------------------------------------------------------------------------------------------
C#
namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        internal static Form2 form2 = new Form2();

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonShowForm2_Click(object sender, EventArgs e)
        {
            form2.Show();
        }

        public void ShowText()//the method which I want to call
        {
            textBoxInForm1.Text = "Method Called From Another Form";
        }
    }
}


---------------------------------------------------------------------------------------------------------

C#
namespace WindowsFormsApplication
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void buttonCallMethod_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();//this creates new instance, which is wrong
            form1.ShowText();//the method defined in Form1
            this.Close();
        }
    }
}

---------------------------------------------------------------------------------------------------------
Posted
Updated 9-Aug-15 23:24pm
v6

I do strongly recommend to read Sergey's past answers[^] and His excellent tip: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^]
 
Share this answer
 
I had to use the current form with defining one in the Form1 class:

---------------------------------------------------------------------------------------------------------
C#
namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        internal static Form2 form2;
        internal static Form1 form1;

        public Form1()
        {
            InitializeComponent();
            form1 = this;//refering to the current Form1
        }

        private void buttonShowForm2_Click(object sender, EventArgs e)
        {
            form2 = new Form2();
            form2.Show();
        }

        public void ShowText()//the method which I want to call
        {
            textBoxInForm1.Text = "Method Called From Another Form";
        }
    }
}


---------------------------------------------------------------------------------------------------------
C#
namespace WindowsFormsApplication
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void buttonCallMethod_Click(object sender, EventArgs e)
        {
            Form1.form1.ShowText();//the method defined in Form1
            this.Close();
        }
    }
}


---------------------------------------------------------------------------------------------------------
 
Share this answer
 
v3
You might do something like
C#
public partial class Form2 : Form
{
  Form1 form1;
  public Form2(Form1 form1)
  {
      this.form1 = form1;
      //...
  }
  private void buttonCallMethod_Click(object sender, EventArgs e)
  {
      form1.ShowText();//the method defined in Form1
      //...
  }
  //...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900