无法将文本框从类链接到主窗体
本文关键字:链接 窗体 文本 | 更新日期: 2023-09-27 18:33:35
我刚刚尝试了所有方法,没有任何效果。创建了一个新类并在那里设置了我所有的编码,并尝试从我的 form1 调用公共方法.cs但在我的类 1 中,我的文本框 1 和发送者是红色的。
表单 1 的代码.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";
}
}
发件人错误:无法解析符号"发件人"文本框错误:无法解析符号"文本框 1"不确定它应该是公共字符串还是无效
:'(
任何帮助都可以!!
你需要让你的函数 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);
}
我可能是错的,但请确保文本框修饰符设置为公共,如果它是红色的,我有它,这就是我至少为该部分修复我的方式。