Search code examples
c#visual-studiotextboxmultiplicationmultiline

product of two multiline textboxes and display it also to another multiline textbox


I want to know how to perform calculations such as multiplication of two multiline textboxes and the product of it will also display in a multiline textbox. Please see the design below

enter image description here


Solution

  • If you want to get the multiplication of two multiline textboxes and display the result in a multiline textbox, you can use the following code:

    private void textBox2_LostFocus(object sender, EventArgs e)
        {
            float a, b;
            for (int i = 0; i < textBox1.Lines.Length ; i++)
            {
    
                float.TryParse(textBox1.Lines[i], out a);
                float.TryParse(textBox2.Lines[i], out b);
                textBox3.AppendText((a * b).ToString()+ Environment.NewLine);
            }
        }
    

    And you have to add an EventHandler in Form1.Designer.cs file:

            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(312, 110);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 167);
            this.textBox2.TabIndex = 4;
            this.textBox2.LostFocus += new System.EventHandler(this.textBox2_LostFocus);
    

    enter image description here