Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将文本框的值(字符串)传递给C#中的构造函数?_C#_Winforms_Calculator - Fatal编程技术网

如何将文本框的值(字符串)传递给C#中的构造函数?

如何将文本框的值(字符串)传递给C#中的构造函数?,c#,winforms,calculator,C#,Winforms,Calculator,对于下面的程序,我假设创建一个类WComplex,该类对复数执行操作。我做到了,它工作得很好。此外,我还必须创建一个简单计算器的Windows窗体应用程序,其中包含3个文本框(value1,value2,结果),4个操作按钮(+,-,*,/)。这将使用WComplex类(通过将其作为新的现有项添加) 我在下面代码中遇到的问题,例如,我试图将第一个文本框中的数字(值1)传递给num1(因此我执行以下操作):WComplex num1=new WComplex(textBox1.text)

对于下面的程序,我假设创建一个类
WComplex
,该类对复数执行操作。我做到了,它工作得很好。此外,我还必须创建一个简单计算器的Windows窗体应用程序,其中包含3个文本框(
value1
value2
结果
),4个操作按钮(
+,-,*,/
)。这将使用
WComplex
类(通过将其作为新的现有项添加)

我在下面代码中遇到的问题,例如,我试图将第一个文本框中的数字(值1)传递给num1(因此我执行以下操作):
WComplex num1=new WComplex(textBox1.text)WComplex
接受两个参数(实参数和虚参数)。但是如何从文本框(v
value 1
)中读取表单
(a+bi)
的值,并将其拆分为两个参数或值?此外,我必须向每个特定的按钮单击添加什么代码,使其执行指定的操作?要求澄清以下评论

**[更新]-我已经知道如何使用split和separator传递这两个值,请参见下面的按钮1\u click(addition),但我仍然收到一个调试错误

Using WComplexClass;

namespace WindowsFormsApplication1
{
    public partial class Calculator : Form
    {

    public Calculator()
    {
        InitializeComponent();

    }

    // ---------------------------------------------------------------------------
   // ---------------------------------------------------------------------------

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //text box (value 1)
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        //text box (value 2)
    } 

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        //text box (result)

    }

    private double button1_Click(object sender, EventArgs e)
    {
        //addition

         string t = textBox1.Text;
        string[] split = t.Split('+');

        double num1 = Double.Parse(split[0]);
        double num2 = Double.Parse(split[1]);

        string t2 = textBox1.Text;
        string[] split2 = t2.Split('+');

        double num3 = Double.Parse(split2[0]);
        double num4 = Double.Parse(split2[1]);

        WComplex num5 = new WComplex(num1,num2);
        WComplex num6 = new WComplex(num3,num4);

        WComplex sum = num5 + num6;
    } 

    private void button2_Click(object sender, EventArgs e)
    {
        //subtraction


    }

    private void button3_Click(object sender, EventArgs e)
    {
        //multiplication


    } 

    private void button4_Click(object sender, EventArgs e)
    {
        //division

    }

    private void Calculator_Load(object sender, EventArgs e)
    {

    }

}
}

你几乎回答了你自己的问题。如果用户在文本框中输入类似“a+bi”的内容,则可以使用String.Split在“+”上拆分它。您还应该验证和解析数据,这样您就可以将
int
值传递给构造函数,而不是
string
值,这些值可以是任何文本。

这是一个项目,我明天要参加考试,考试中使用了一些窗口表单应用程序的概念(我对OOP和C有点陌生)更新后出现的错误是什么?把它编辑成问题。实际上我刚让它工作,但它是随机操作。我必须弄清楚,outGood luck听起来像是你掌握了窍门:)它是在做乘法而不是加法。在这种情况下,我该如何使用string.split?WComplex num1=新的WComplex(textBox1.Text);WComplex num2=新的WComplex(textBox3.Text);w复数和=num1+num2;我围绕您的概念进行了研究,并使其发挥作用,但如果用户以(a-bi)的形式输入值,我是否需要添加另一个分隔符(-)?