我剛剛嘗試了所有內容,沒有任何內容能夠正常工作。已經創建了一個新類,並將其中的所有編碼設置在此處,並嘗試從我的form1.cs中調用公共方法,但在我的class1中,我的文本框1和發件人處於紅色書寫狀態。無法將文本框從課程鏈接到主表格
代碼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' 不知道它應該是公共字符串還是void
:'(
任何幫助將做pls!
Ehhh ..您將代碼從事件處理程序移至單獨的類,但未將事件處理程序的「發件人」參數賦予分隔的代碼。因此,「發件人」變量未在「數字」方法 –