我刚刚尝试了一切,但没有任何效果。已经创建了一个新类并在那里设置了我的所有编码并尝试从我的 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”不确定它应该是公共字符串还是无效
:'(
任何帮助都会做的!