0

can sameone provide example code on how to call non-static method on form1 from form2.

form1

    public Form1()
    {
        InitializeComponent();
    }

    public void prikazi()
    {
        MessageBox.Show("ok");
    }

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

form2

    public Form2()
    {
        InitializeComponent();
    }

    private void callMethod_Click(object sender, EventArgs e)
    {
        // this don't work. If I change to public static void on form1 then it work's but I need non-static example
        Form1.prikazi(); 
    }

thanks

Carlo
  • 97
  • 2
  • 13
  • 3
    You need to get ahold of the original instance. Also, name your forms. – SLaks Nov 10 '13 at 19:36
  • 5
    Your architecture scares me. – Yuriy Faktorovich Nov 10 '13 at 19:37
  • possible duplicate of [How to call mainform method in another form in usercontrol C#](http://stackoverflow.com/questions/17672276/how-to-call-mainform-method-in-another-form-in-usercontrol-c-sharp) – O. R. Mapper Nov 10 '13 at 19:38
  • 1
    possible duplicate of [calling method on one form from another form, fill combobox on Form1 if a button on Form2 is clicked](http://stackoverflow.com/questions/6080235/calling-method-on-one-form-from-another-form-fill-combobox-on-form1-if-a-button) – Randy Levy Nov 10 '13 at 19:39
  • Or of [this](http://stackoverflow.com/questions/11165537/passing-textboxs-text-to-another-form-in-c), or [this](http://stackoverflow.com/questions/5928071/call-variable-from-another-form-c-sharp), or [this](http://stackoverflow.com/questions/16916540/accessing-public-property-from-other-form-in-winforms), or [this](http://stackoverflow.com/questions/12890754/call-a-method-from-another-form), or [this](http://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp), or ... – O. R. Mapper Nov 10 '13 at 19:40
  • this is just for example – Carlo Nov 10 '13 at 19:41

4 Answers4

3

It doesn't matter if it's a form class, if you want to access a non-static method, there is no other alternative then to create an instance of the class.

But - It doesn't make sense.. so don't do it

Find other alternatives, like creating the method you need static in a common place, or consider adding this method (or a variation of it) to the form

Yosi Dahari
  • 6,794
  • 5
  • 24
  • 44
2

form1

public Form1()
{
    InitializeComponent();
}

public void prikazi()
{
    MessageBox.Show("ok");
}

private void openf2_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2(this);
    frm.Show();
}

form2

private Form1 parentForm;

public Form2(Form1 parentForm)
{
    this.parentForm = parentForm;
    InitializeComponent();
}

private void callMethod_Click(object sender, EventArgs e)
{
    parentForm.prikazi(); 
}

But better learn to bundle reusable code in to a separate class, rather than passing around form instances.

Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63
1

You need to have an instance of the form to call the method.

There are a few ways that you can make this work

1) Pass an action through to the new form

    public Form2()
    {
        InitializeComponent();
    }


    public Action yourAction {get; set;}

    private void callMethod_Click(object sender, EventArgs e)
    {
        Action instance = yourAction;
        if(instance != null)
            instance();
    }

then in Form 1 you can say

private void openf2_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2();
    frm.yourAction = prikazi;
    frm.Show();
}

2) You can pass an instance of Form1 into Form 2

So in Form 2 you have:

    public Form1 ParentForm {get; set;}

    private void callMethod_Click(object sender, EventArgs e)
    {
        if (ParentForm != null)
            ParentForm.prikazi();
    }

And Form1 you assign a value to the ParentForm variable

private void openf2_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2();
    frm.ParentForm= this;
    frm.Show();
}
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
1

 public partial class Form1 : Form
    {
        internal static Form1 ViewForm1; // make other form run Public void

        public form1()
        {
            InitializeComponent();
            ViewForm1 = this;  //Add this
        }
        public void DoSomething()
        {
          //Code...
        }
}
......................

 public partial class Form1 : Form
    {
        public form2()
        {
            InitializeComponent();            
            Form1.ViewForm1.ShowData(); // call public void from form1
        }
  • 1
    Hi! Please consider including a brief explanation for why this solution works. – Joe Oct 26 '17 at 22:24