如何在 windows 表单应用程序中更改 class 中除 Form1.cs 之外的文本框文本

How to change the textbox text in class other than Form1.cs in windows form application

所以我有 class DigitButton,按下按钮时会调用 NormalDigitClick。并调用 appendNumber.

public class DigitButton
{
    public static void NormalDigitClick(object sender, EventArgs e)
    {
        Button button = (Button)sender;
        Model model = new Model();
        model.appendNumber(button.Text);
    }
}

appnedNumber() 附加了数字,我试图在 setTextboxVslue() 的文本框中反映这个数字,但文本框没有显示任何内容。不确定出了什么问题。

public class Model :Form1
{
    public static string textBoxValue;

    public void appendNumber(string valueToBeAppended)
    {
        if (textBoxValue == "0")
        {
            textBoxValue = "";
        }
        textBoxValue = textBoxValue + valueToBeAppended;
        setTextboxValue(textBoxValue);
    }
    private void setTextboxValue(string textBoxValue)
    {
        textBox2.Text = textBoxValue;
    }
}

您没有访问在您的应用程序中打开的表单。您正在创建一个新的 Model 实例,然后您甚至不显示它。您需要获取已在您的应用程序中打开的模型表单的实例。替换以下两行:

Model model = new Model();
model.appendNumber(button.Text);

使用以下行:

(Application.OpenForms["Model"] as Model).appendNumber(button.Text);

每当调用 appendTextbox 时。创建新实例(模型),因此值不会改变

尝试这样的事情,

    Model model = new Model();

改变

    Model model = (Model1)button.Parent;