c#在form2上的form1上运行函数

时间:2023-02-04 00:57:37

This question probably has already been answered, I may be type the wrong question in google, but I want to run the function time_refresh that is on form1 and works well from form2. this what I got so far:

这个问题可能已经得到了解答,我可能在google中键入了错误的问题,但我想运行form1上的函数time_refresh并且从form2运行良好。这就是我到目前为止所得到的:

Form1

Form1中

public void time_refresh()
    {
        run_stop.Text = Properties.Settings.Default.s_update + " " + Properties.Settings.Default.s_update_int;
        run_stop.ForeColor = Color.Green;
    }

Form2

窗体2

private Main_form_1 form1 = null;

and

form1.time_refresh();

But it get an error

但它得到一个错误

Object reference not set to an instance of an object. Kind of new to c# so any help would be excellent.

你调用的对象是空的。对c#有点新意,所以任何帮助都会很棒。

5 个解决方案

#1


0  

The proper way to address this issue is through events. Form2 shouldn't be accessing Form1 directly, it should simply be informing Form1 that it needs to update itself through an event.

解决这个问题的正确方法是通过事件。 Form2不应该直接访问Form1,它应该只是告知Form1它需要通过事件更新自己。

On Form2 create some event like:

在Form2上创建一些事件,如:

public Action event Refresh; //TODO rename as appropriate

Then, on Form2 you can fire that event whenever you want Form1 to know that it needs to refresh itself:

然后,在Form2上,只要您希望Form1知道它需要刷新自己,您就可以触发该事件:

public void OnRefresh()  //call this method to fire the event
{
    if(Refresh != null)
        Refresh();
}

Then on Form1 you can attach an event handler:

然后在Form1上,您可以附加一个事件处理程序:

Form2 other = new Form2();
other.Refresh += () => time_refresh();
other.Show();

#2


0  

you might want to set in the constructor of your form 2 the instance of form1 used

您可能希望在表单2的构造函数中设置使用的form1实例

private Form1 _currentForm1;
Public Form2(Form1 currentForm1)
{
    _currentForm1 = currentForm1;
}

and in your Form1 (in the constructor for example)

并在您的Form1中(例如在构造函数中)

private Form1 _thisForm1;
public Form1()
{
    _thisForm1 = this;
}

//in your method simply say
Form2 form2 = new Form2(_thisForm1)

this will allow you to use public methods from form1 in your form 2 code. While not having the problem to instantiate a new form1 as that won't be the form shown to the user.

这将允许您在表单2代码中使用form1中的公共方法。虽然没有问题来实例化新的form1,因为它不会是向用户显示的表单。

#3


0  

You should initialize the variable form1 inside your Form2.
As is, it's a variable of type Main_form_1 having a null value (meaning that it is not assigned to any valid instance of Main_form_1)

您应该在Form2中初始化变量form1。它是一个Main_form_1类型的变量,具有空值(意味着它没有分配给Main_form_1的任何有效实例)

The most obvious method is to pass the instance of the current Main_form_1 when you create an instance of Form2.
For example, inside the current Main_form_1 you create an instance of Form2.
At this point you pass the reference of the current Main_form_1

最明显的方法是在创建Form2实例时传递当前Main_form_1的实例。例如,在当前Main_form_1中创建Form2的实例。此时,您传递当前Main_form_1的引用

 Form2 f = new Form2(this);

and in the constructor of Form2

并在Form2的构造函数中

public Form2(Main_form_1 f)
{
   form1 = f;
}

now your instance of Form2 has a correct reference to the running Main_form_1 and could call the time_refresh() method on the correct instance of Main_form_1

现在,您的Form2实例具有对正在运行的Main_form_1的正确引用,并且可以在Main_form_1的正确实例上调用time_refresh()方法

#4


0  

you get that error because you didn't assign form1 value.

你得到那个错误,因为你没有分配form1值。

you can, for example create a property on form2:

你可以,例如在form2上创建一个属性:

public Main_form_1 Form1 { get { return form1; } set { form1 = value; } }

and assign it from the form1 on form2 initialization:

并在form2初始化时从form1分配它:

// code from some form1 method
var form2 = new form2();
form2.Form1 = this;
form2.Show();

#5


-1  

You have to instantiate a new instance of Form1. Try declaring it like this:

您必须实例化Form1的新实例。尝试像这样声明:

private Main_form_1 form1 = new Main_form_1();

private Main_form_1 form1 = new Main_form_1();

And then you can call form1.time_refresh();

然后你可以调用form1.time_refresh();

Also, what you could do is have your Form2 require an instance of Form1.

此外,您可以做的是让您的Form2需要Form1的实例。

public Main_form_1 _form1; // global variable
public Form2(Main_form_1 form1)
{
     _form1 = form1;
}

And from there you can call the functions and it should affect your original form.

从那里你可以调用函数,它应该影响你的原始形式。

#1


0  

The proper way to address this issue is through events. Form2 shouldn't be accessing Form1 directly, it should simply be informing Form1 that it needs to update itself through an event.

解决这个问题的正确方法是通过事件。 Form2不应该直接访问Form1,它应该只是告知Form1它需要通过事件更新自己。

On Form2 create some event like:

在Form2上创建一些事件,如:

public Action event Refresh; //TODO rename as appropriate

Then, on Form2 you can fire that event whenever you want Form1 to know that it needs to refresh itself:

然后,在Form2上,只要您希望Form1知道它需要刷新自己,您就可以触发该事件:

public void OnRefresh()  //call this method to fire the event
{
    if(Refresh != null)
        Refresh();
}

Then on Form1 you can attach an event handler:

然后在Form1上,您可以附加一个事件处理程序:

Form2 other = new Form2();
other.Refresh += () => time_refresh();
other.Show();

#2


0  

you might want to set in the constructor of your form 2 the instance of form1 used

您可能希望在表单2的构造函数中设置使用的form1实例

private Form1 _currentForm1;
Public Form2(Form1 currentForm1)
{
    _currentForm1 = currentForm1;
}

and in your Form1 (in the constructor for example)

并在您的Form1中(例如在构造函数中)

private Form1 _thisForm1;
public Form1()
{
    _thisForm1 = this;
}

//in your method simply say
Form2 form2 = new Form2(_thisForm1)

this will allow you to use public methods from form1 in your form 2 code. While not having the problem to instantiate a new form1 as that won't be the form shown to the user.

这将允许您在表单2代码中使用form1中的公共方法。虽然没有问题来实例化新的form1,因为它不会是向用户显示的表单。

#3


0  

You should initialize the variable form1 inside your Form2.
As is, it's a variable of type Main_form_1 having a null value (meaning that it is not assigned to any valid instance of Main_form_1)

您应该在Form2中初始化变量form1。它是一个Main_form_1类型的变量,具有空值(意味着它没有分配给Main_form_1的任何有效实例)

The most obvious method is to pass the instance of the current Main_form_1 when you create an instance of Form2.
For example, inside the current Main_form_1 you create an instance of Form2.
At this point you pass the reference of the current Main_form_1

最明显的方法是在创建Form2实例时传递当前Main_form_1的实例。例如,在当前Main_form_1中创建Form2的实例。此时,您传递当前Main_form_1的引用

 Form2 f = new Form2(this);

and in the constructor of Form2

并在Form2的构造函数中

public Form2(Main_form_1 f)
{
   form1 = f;
}

now your instance of Form2 has a correct reference to the running Main_form_1 and could call the time_refresh() method on the correct instance of Main_form_1

现在,您的Form2实例具有对正在运行的Main_form_1的正确引用,并且可以在Main_form_1的正确实例上调用time_refresh()方法

#4


0  

you get that error because you didn't assign form1 value.

你得到那个错误,因为你没有分配form1值。

you can, for example create a property on form2:

你可以,例如在form2上创建一个属性:

public Main_form_1 Form1 { get { return form1; } set { form1 = value; } }

and assign it from the form1 on form2 initialization:

并在form2初始化时从form1分配它:

// code from some form1 method
var form2 = new form2();
form2.Form1 = this;
form2.Show();

#5


-1  

You have to instantiate a new instance of Form1. Try declaring it like this:

您必须实例化Form1的新实例。尝试像这样声明:

private Main_form_1 form1 = new Main_form_1();

private Main_form_1 form1 = new Main_form_1();

And then you can call form1.time_refresh();

然后你可以调用form1.time_refresh();

Also, what you could do is have your Form2 require an instance of Form1.

此外,您可以做的是让您的Form2需要Form1的实例。

public Main_form_1 _form1; // global variable
public Form2(Main_form_1 form1)
{
     _form1 = form1;
}

And from there you can call the functions and it should affect your original form.

从那里你可以调用函数,它应该影响你的原始形式。