简体   繁体   English

从 C# 中的另一个表单类更改按钮的文本

[英]Change text of Button from another form class in C#

I want to modify something in Mission Planner and I want to change button from another class/form in C# in this function :我想在Mission Planner 中修改一些东西,我想在这个函数中从 C# 中的另一个类/表单更改按钮:

public void CloseAllConnections()
{
    myButton1.Text = "Disconnecting all";

    ... 
}

function that is located in :位于的函数:

namespace MissionPlanner
{
    public partial class MainV2 : Form
    {
        ...
    }

    ...
}

the idea is that everything works perfectly when i am focused on that menu, but sometimes i get a error这个想法是当我专注于该菜单时一切正常,但有时我会收到错误

i even made a function like this我什至做了一个这样的功能

public MyButton GetMyButton1 { get { return myButton1; } }

and also created new instance并且还创建了新实例

var myObject = new MainV2(true);
myObject.myButton1.Text = "Disconnecting all";

nothing works ...没有任何作用...

i don't even know where is the function called from, because is clear that is not called from MainV2 class ...我什至不知道从哪里调用的函数,因为很明显它不是从MainV2类调用的......

An exception of type 'System.NullReferenceException' occurred in MissionPlanner.exe but was not handled in user code

Any ideas?有任何想法吗? Thank you.谢谢你。

You need to grab an instance of the form where the button resides.您需要获取按钮所在的表单实例。 Do this by saving a static reference to the form in it's constructor:通过在它的构造函数中保存对表单的静态引用来做到这一点:

namespace MissionPlanner
{
    public partial class MainV2 : Form
    {
        public static MainV2 CurrentForm;
        public MainV2()
        {
            CurrentForm = this;
            InitializeComponent();
        }

Then somewhere else in your code:然后在您的代码中的其他地方:

public void CloseAllConnections()
{
    MainV2.CurrentForm.myButton1.Text = "Disconnecting all";

    ... 
}

It appears that your click event form object is called (name) myButton1, and that you are calling the following to change it: myobject.myButton1.Text = "Disconnecting all".您的单击事件表单对象似乎被称为(名称)myButton1,并且您正在调用以下内容来更改它:myobject.myButton1.Text = "Disconnecting all"。 Try using myButton1.Text = "Disconnecting all" instead.尝试使用 myButton1.Text = "Disconnecting all" 代替。

One thing that you could try is to pass the button from the form to the class that will be modifying the button.您可以尝试的一件事是将按钮从表单传递到将修改按钮的类。

public class Example
{
    public Button MyButton1 { get; set; }

    public Example(Button myButton1)
    {
        MyButton1 = myButton1;
    }

    public void CloseAllConnections()
    {
        MyButton1.Text = "Disconnecting all";
    }
}

This should successfully set the button's text on MainV2.这应该成功地在 MainV2 上设置按钮的文本。

Are you using any sort of multi-threading in your app?您是否在您的应用程序中使用任何类型的多线程? if so: Make sure you either only change the button from the same thread it was created by, or simply use the ControlInstance.Invoke() method to delegate the change.如果是这样:确保您只从创建按钮的同一线程更改按钮,或者仅使用 ControlInstance.Invoke() 方法来委托更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM