Skip to content

Listbox variable selection

Swan

Asked Feb 21, 2006, 11:53 AM EST5 answers
Share
LinkedInX
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

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    SQL
    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?

    0
  • 0
    except the listbox items I need are text strings not integers.... the items need corrisponding values to be used as needed when the user select the text item that matchs their criteria , the user nver sees a number other then output.
    0
  • 0
    got it....it may not be perfect but it works:
    C#
    
    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);
    0
  • 0

    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]

    0
  • 0
    I read your question differently, so heres my stab at an answer :)

    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;
    0