Listbox variable selection
heres the deal
I want a user to select 2 dffrent items from 2 listboxes , the items being a description. I want to have these items equal some number (diffrent numbers) to be added together and be output to the user.
How do I declare numer values to the listbox items to be added together?
I have no solid code to show because i cant solve this issue yet.
thx
a.wilcoPosted Feb 25, 2006, 8:33 AM
You can put any item in a listbox, and the text it shows comes from the ToString() method. So if you want to associate a number, try making your own little class:
class ListBoxNumberedItem
{
public string Text;
public int Number;
public override ToString()
{
return this.Text;
}
}
then add them to your listbox:
...
ListBoxNumberedItem Item1 = new ListBoxNumberedItem();
Item1.Text = "Hello World";
Item1.Number = 2;
ListBox.Items.Add(Item1);
...
then pull it out the listbox
ListBoxNumberedItem ItemSel1 = (ListBoxNumberedItem)ListBox.SelectedItem;
do that for both listboxes, then just add the number values
int answer = ItemSel1.Number + ItemSel2.Number;
will hePosted Feb 23, 2006, 1:08 PM
If you understand your question correctly, I suggest you use ArrayList.
.....
ArrayList selectedList = new ArrayList();
....
foreach(string item in itemOneBox.Items)
{
if(item == keyNumb)
selectedList.Add(item);
}
foreach(string item in itemTwoBox.items)
{
if(item == keyNumb)
selectedList.Add(item);
}
foreach(string item in selectedList)
targetItemBox.Items.add(item);
......
short, easy, But I don't if it fit your requirment. Any question, email to me: [email protected]
SwanPosted Feb 21, 2006, 8:24 PM
private void btnDo_Click(object sender, System.EventArgs e) { int intAMD1 =5; int intAMD2 =10; int intAMD3 =15; int intIntel1 = 2; int intIntel2 = 4; int intIntel3 = 6; int intBox1 = 0; int intBoxx = 0; if(listBoxAmd.Text=="amd1") { intBox1=intAMD1; } else if(listBoxAmd.Text=="amd2") { intBox1=intAMD2; } else if(listBoxAmd.Text=="amd3") { intBox1=intAMD3; } else { }; if(listBox2.Text=="Intel1") { intBoxx=intIntel1; } else if(listBox2.Text=="Intel2") { intBoxx=intIntel2; } else if(listBox2.Text=="Intel3") { intBoxx=intIntel3; } else { }; int intResult = intBox1 + intBoxx ; label1.Text=Convert.ToString(intResult);SwanPosted Feb 21, 2006, 7:02 PM
AnttiPosted Feb 21, 2006, 6:12 PM
listbox1.Items.Add(12);
listbox2.Items.Add(23);
...
int num1 = (int)listbox1.Items[listbox1.SelectedIndex];
int num2 = (int)listbox2.Items[listbox2.SelectedIndex];
int result = num1 + num2;
MessageBox.Show(num1 +"+"+num2+"="+result);
Can it be more simpler?