关于c#:无法将TextBox从类链接到主窗体

Can't link TextBox from class to main form

我什么都试过了,什么也干不了。已经创建了一个新的类,并在其中设置了所有代码,并试图从form1.cs调用公共方法,但是在我的class1中,我的textbox 1和sender是红色的。

Form1.cs代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
}

下面是我为班级1编写的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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";
    }
}

发件人错误:无法解析符号"sender"文本框错误:无法解析符号"textbox 1"不确定它应该是公共字符串还是无效的

任何帮助都可以!!!!


我可能是错的,但要确保文本框修改器设置为public,如果它是红色的,我就拥有它,这就是我修复该部分的方法。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
}