Mike Peters

Hi

First of all I'm new to c#. I want a button to instigate a different behaviour in reponse to an item in a listbox. If the user enters a number in one textbox and another in another textbox and then selects addition (from a list of operations) from the listbox, on pressing the calculate button the result is displayed in another textbox.

Mike



Re: Windows Forms General Button action on listbox event

micvos

Could you elaborate I'm not sure what's your question.




Re: Windows Forms General Button action on listbox event

Bob zhu - MSFT

hope follow code can fit your requirement

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace CS3

{

public partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

private void Form3_Load(object sender, EventArgs e)

{

this.listBox1.Items.AddRange(new string[]{"+","-","*","/"});

this.textBox1.Text = this.textBox2.Text = "0";

}

private void button1_Click(object sender, EventArgs e)

{

double a, b;

a= Convert.ToDouble(this.textBox1.Text.Trim());

b = Convert.ToDouble(this.textBox2.Text.Trim());

MessageBox.Show(a.ToString() + " " + b.ToString());

if (this.listBox1.SelectedIndex == -1)

{ MessageBox.Show("select operation"); }

else

{

if (this.listBox1.SelectedIndex==3 && b == 0)

{ MessageBox.Show("Error"); }

else

switch (this.listBox1.SelectedIndex)

{

case 0: this.textBox3.Text = Convert.ToString(a + b); break;

case 1: this.textBox3.Text = Convert.ToString(a - b); break;

case 2: this.textBox3.Text = Convert.ToString(a * b); break;

case 3: this.textBox3.Text = Convert.ToString(a / b); break;

}

}

}

}

}