问题
I'm starting on C# now. I had a problem in tackling a question my lecturer asked me to do. Below is the GUI.
http://i.share.pho.to/daa36a24_c.png
This is the code i did but i didn't manage to code part the i mentioned below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double num1;
double num2;
double answer;
num1 = double.Parse(textBox1.Text);
num2 = double.Parse(textBox2.Text);
textBox4.Text = Convert.ToString(answer);
}
}
}
I will need to add/subtract/multiple/divide first and second number so that it will produce --> (first number + the operation + second number = the answer).
The problem is i need to select the operation by clicking on the + , - , * , / symbols on the textbox. I could do it easily by using radio button or etc but my lecturer insist on this format. Please do assist for the coding of "operation" selection. Thank you.
回答1:
You use the OnIndexChanged
event of a listbox to know which operator was selected.
This will allow you to calculate on each click of the list box.
Notice in the operatorListBox1_SelectedIndexChanged
event method, you use sender
(the object that was clicked on) to find the SelectedItem
. Cast this to a string (its an object in the listbox) and your sign will appear. (no pun intended)
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int firstNum = 2;
private int secondNum = 4;
private int answer;
public Form2()
{
InitializeComponent();
operatorListBox1.Items.Add("+");
operatorListBox1.Items.Add("-");
operatorListBox1.Items.Add("*");
operatorListBox1.Items.Add("/");
//this next line would go in your designer.cs file. I put it here for completeness
this.operatorListBox1.SelectedIndexChanged += new System.EventHandler(this.operatorListBox1_SelectedIndexChanged);
}
private void operatorListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
calculateAnswer(((ListBox)sender).SelectedItem.ToString());
}
private void calculateAnswer(string sign)
{
switch (sign)
{
case "+":
answer = firstNum + secondNum;
break;
case "-":
answer = firstNum - secondNum;
break;
case "*":
answer = firstNum * secondNum;
break;
case "/":
answer = firstNum / secondNum;
break;
}
textBox4.Text = firstNum + " " + sign + " " + secondNum + " = " + answer;
}
}
}
回答2:
As long as the operations are in a listBox, use this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double num1;
double num2;
double answer;
num1 = double.Parse(textBox1.Text);
num2 = double.Parse(textBox2.Text);
if (listBox1.SelectedIndex == 0)
{
answer = num1 + num2
}
if (listBox1.SelectedIndex == 1)
{
answer = num1 - num2
}
if (listBox1.SelectedIndex == 2)
{
answer = num1 * num2
}
if (listBox1.SelectedIndex == 3)
{
answer = num1 / num2
}
textBox4.Text = Convert.ToString(answer);
}
}
}
来源:https://stackoverflow.com/questions/19080667/c-sharp-multiline-calculation