|
 |
|
[Winform]{C#} Combobox อยากทราบวิธีนำ combobox มาคำนวณ กับ texbox ครับ |
|
 |
|
|
 |
 |
|
ถ้าเอาง่ายก็อย่างนี้ก็ได้ครับ
Code (C#)
textbox1 = 500
anInteger = Convert.ToDouble(textbox1.Text);
if(combobox.SelectedIndex == 0)
{
textbox2.text = aninteger * 0.2
}
else if(combobox.SelectedIndex == 1)
{
textbox2.text = aninteger * 0.3
}
else if(combobox.SelectedIndex == 2)
{
textbox2.text = aninteger * 0.5
}

|
ประวัติการแก้ไข 2014-05-06 14:27:24 2014-05-06 14:33:59
 |
 |
 |
 |
Date :
2014-05-06 14:26:45 |
By :
teerapat_kan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าต้องการให้ข้อมูลหรือผลลัพธ์ไปปรากฎใน combobox ก่อนอื่นเลยต้องกำหนด Property ของ combobox ดังนี้
DropDownStyle = DropDown
จากนั้นวาง Code ที่ event SelectedIndexChanged ของ combobox ดังนี้
Code (C#)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox1.SelectedIndex < 0) return;
double result;
if (double.TryParse(this.textBox1.Text, out result))
{
double percent = 0.0;
switch (this.comboBox1.SelectedIndex)
{
case 0 :
percent = 0.2;
break;
case 1 :
percent = 0.3;
break;
case 2 :
percent = 0.5;
break;
}
this.BeginInvoke((MethodInvoker)delegate {
this.comboBox1.Text = string.Format("{0} = {1}", this.comboBox1.Text, (result * percent).ToString("N2"));
});
}
}
ผมไม่ใส่ comment ที่ code ให้นะครับ จขกท ค่อยๆ อ่าน code ไปนะครับ ไม่ยากมาก
|
ประวัติการแก้ไข 2014-05-06 15:09:07
 |
 |
 |
 |
Date :
2014-05-06 15:07:11 |
By :
gunnermontana |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|