0

我刚刚尝试了一切,但没有任何效果。已经创建了一个新类并在那里设置了我的所有编码并尝试从我的 form1.cs 调用公共方法,但在我的 class1 中,我的 textbox1 和 sender 是红色的。

Form1.cs 的代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    string text;
    double operand_1, operand_2, solution;
    char Operator;

    #region Form Code
    private void Form1_Load(object sender, EventArgs e)
    {
        operand_1 = operand_2 = solution = 0;
        text = "0";
        Operator = '=';
    }
    #endregion

    #region Number Buttons
    private void numbers_Click(object sender, EventArgs e)
    {
        Class1 cls = new Class1();
        cls.Numbers(textBox1.Text);
    }
    #endregion

    #region Operator Buttons
    private void operators_Click(object sender, EventArgs e)
    {
        Class1 cls1 = new Class1();
        cls1.Operations();
    }
    #endregion
}

然后这是我为 Class1 编码的编码:

public class Class1
{
    string text;
    double operand_1, operand_2, solution;
    char Operator;

    public void Numbers(string text)
    {
        Button button = (Button)sender;
        text += button.Text;
        while (text != "0" && text[0] == '0' && text[1] != '.')
            text = text.Substring(1);
        textBox1.Text = text;
        return text;
    }

    public void Operations()
    {
        if (Operator != '=')
        {
            operand_2 = double.Parse(textBox1.Text);

            switch (Operator)
            {
                case '+': solution = operand_1 + operand_2;
                    break;
                case '-': solution = operand_1 - operand_2;
                    break;
                case '*': solution = operand_1 * operand_2;
                    break;
                case '/': solution = operand_1 / operand_2;
                    break;
            }
            Operator = '=';
            textBox1.Text = solution.ToString();
        }
        operand_1 = double.Parse(textBox1.Text);
        Operator = char.Parse(((Button)sender).Text);
        text = "0";
    }
}

发件人错误:无法解析符号“发件人”文本框错误:无法解析符号“textBox1”不确定它应该是公共字符串还是无效

:'(

任何帮助都会做的!

4

2 回答 2

0

你需要让你的函数 Numbers 返回一个字符串。然后在您对它的调用中,从表单中将文本框文本设置为返回值。

private void numbers_Click(object sender, EventArgs e)
{
    Class1 cls = new Class1();
    textBox1.Text = cls.Numbers(textBox1.Text);
}
public string Numbers(string text)
{
    Button button = (Button)sender;
    text += button.Text;
    while (text != "0" && text[0] == '0' && text[1] != '.')
        text = text.Substring(1);

    return text;
}

public void Operations(string textBoxText, string buttonText)
{
    string returnText = "";

    if (Operator != '=')
    {
        operand_2 = double.Parse(textBoxText);

        switch (Operator)
        {
            case '+': solution = operand_1 + operand_2;
                break;
            case '-': solution = operand_1 - operand_2;
                break;
            case '*': solution = operand_1 * operand_2;
                break;
            case '/': solution = operand_1 / operand_2;
                break;
        }
        Operator = '=';
        returnText = solution.ToString();
    }
    operand_1 = double.Parse(textBoxText);
    Operator = char.Parse(buttonText);
    text = "0";
    return returnText;
}

private void operators_Click(object sender, EventArgs e)
{
    Class1 cls1 = new Class1();
    cls1.Operations(textBox1.Text, ((Button)sender).Text);
}
于 2013-10-01T10:06:23.547 回答
0

我可能是错的,但请确保文本框修饰符设置为公共,如果它是红色的我有,这就是我至少为那部分修复我的方式。

于 2016-06-26T00:51:09.403 回答