无法将TextBox从类链接到主窗体

时间:2013-10-01 09:51:21

标签: c# visual-studio-2012 calculator

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

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' 不确定它是否应该是公共字符串或无效

:'(

任何帮助都会帮助!!!

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

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