【问题标题】:Change text of Button from another form class in C#从 C# 中的另一个表单类更改 Button 的文本
【发布时间】:2015-03-19 13:35:07
【问题描述】:

我想修改 Mission Planner 中的某些内容,并且我想在此函数中从 C# 中的另一个类/表单更改按钮:

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

    ... 
}

函数位于:

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

    ...
}

这个想法是,当我专注于该菜单时,一切都会完美运行,但有时我会出错

我什至做了这样的功能

public MyButton GetMyButton1 { get { return myButton1; } }

并且还创建了新实例

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

没有任何效果...

我什至不知道从哪里调用的函数,因为很明显不是从MainV2类调用...

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

有什么想法吗?谢谢。

【问题讨论】:

  • 我们能否解释一下为什么这个辅助表单会修改另一个表单上的按钮值?

标签: c# .net c#-4.0 c#-3.0


【解决方案1】:

您的点击事件表单对象似乎被称为(名称)myButton1,并且您正在调用以下命令来更改它:myobject.myButton1.Text = "Disconnecting all"。尝试改用 myButton1.Text = "Disconnecting all"。

【讨论】:

    【解决方案2】:

    您需要获取按钮所在的表单实例。 通过在其构造函数中保存对表单的静态引用来做到这一点:

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

    然后在您的代码中的其他地方:

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

    【讨论】:

    • 在我的情况下,CurrentForm 是什么?另一种形式?
    • 当我从public void CloseAllConnections() 函数调用MainV2.CurrentForm.myButton1.Text = "Disconnecting all"; 时,我也得到了这个Error 10 'System.Windows.Forms.Form' does not contain a definition for 'myButton1' and no extension method 'myButton1' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?) C:\wzi5\ARDUINO\MissionPlanner-master\MissionPlanner-master\MainV2.cs 3014 32 MissionPlanner ... 现在怎么办?
    • CurrentForm 将是 MainV2 表单的当前实例(如果它是打开的)。如果 MainV2 表单未打开,则它将为空。
    • 对不起,定义应该是:public static MainV2 CurrentForm;编辑答案更正
    • 我做了修改,我得到同样的错误An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code Additional information: Cross-thread operation not valid: Control 'myButton1' accessed from a thread other than the thread it was created on.怎么办??我不知道...
    【解决方案3】:

    您可以尝试的一件事是将按钮从表单传递给将修改按钮的类。

    public class Example
    {
        public Button MyButton1 { get; set; }
    
        public Example(Button myButton1)
        {
            MyButton1 = myButton1;
        }
    
        public void CloseAllConnections()
        {
            MyButton1.Text = "Disconnecting all";
        }
    }
    

    这应该会成功设置 MainV2 上的按钮文本。

    【讨论】:

    • 感谢您的回复,但我从一开始就收到此错误An exception of type 'System.NullReferenceException' occurred in MissionPlanner.exe but was not handled in user code Additional information: Object reference not set to an instance of an object. .... 怎么办?
    • 快速提问。您在哪里创建包含CloseAllConnections 方法的类的新实例?
    • 这里是解释:pastebin.com/Es0f8Y6z你怎么看这个?
    • User2747636,我不确定它是否只是粘贴到 pastebin 的方式,但看起来您的 public void CloseAllConnections() 方法在 MainV2 类本身中。 MainV2 是有你的按钮的表格吗?如果是这样,那么您应该能够使用myButton1.Text = "Disconnecting all" 更改文本,而无需我上面建议的任何内容。
    【解决方案4】:

    您是否在应用程序中使用了任何类型的多线程?如果是这样的话: 确保你要么只从创建按钮的同一线程更改按钮, 或者简单地使用 ControlInstance.Invoke() 方法来委派更改。

    【讨论】:

      猜你喜欢
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多