C# running function from 1 form to another?

Hello ,
sorry for the “Non- mikrotik question” :slight_smile:
I have build a mice API for my router in 1 form.
now I want to open a new window to show me some data and run some functions(from the main form)

this is what I did :

namespace MyAPI 
{
public partical class Form1 : Form
Mk mikrotik 
.
.
string PingTIme; //get the resualt from CheckPing function.
public Form1()
private void Form1_Load(object sender , EvenArgs e)

public void CheckPing();
.
.
.

private void button1_Click (object sender , EvenArgs e)
{
var form = new Form2();
form.Show();
}

class MK()


public partical class Form2 : Form
{
Form1 mainForm;
public Form2()
{
intializeComponent();
}

private void button1_Click()
{
mainForm.CheckPing();
textBox1.Text = PingTime;
}
}

but it doesn’t work in Form2
any idea why?

Thanks ,

  • The mainForm instance can be passed in the constructor of Form2 (“this” keyword references to current object)
  • The PingTime variable has to be declared public in order to be accessible in Form2


public partial class Form1 : Form
{
  public string PingTime; //get the resualt from CheckPing function.

  private void button1_Click(object sender, EventArgs e)
  {
    var form = new Form2(this);
    form.Show();
  }
}

public partial class Form2 : Form
{
  Form1 mainForm;

  public Form2(Form1 parent)
  {
    mainForm = parent;
  }

  private void button1_Click(object sender, EventArgs e)
  {
    mainForm.CheckPing();
    textBox1.Text = mainForm.PingTime;
  }
}

Example: http://share.linqpad.net/p4luqn.linq
2017-07-11_22-55-28.png

Thanks ,
but i found it will be much easy for me towork with tabs and not another forms


Thanks you anyway :slight_smile: