13 Replies - 1117 Views - Last Post: 25 October 2012 - 05:29 AM Rate Topic: -----

#1 Deadboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 12

Comparing two listbox items...

Posted 07 October 2012 - 12:25 AM

I have created two listboxes-listbox1 and listbox2.listbox1 contains few items and listbox2 also contains the same items.
I wanted to created a code that would find which items are selected in both the listboxes,compare both the items and according to that show some text by clicking on a button.I've tried the following code but it is not working:-


string curItem = listBox1.SelectedItem.ToString();
            string curItem2 = listBox2.SelectedItem.ToString();
            if ((curItem == listBox1.Items[0].ToString()) && (curItem2 == listBox2.Items[3].ToString()))
            {
                textBlock4.Text = "ok";
            }


It shows the output for any combination of listbox items.I wanted different output for different combination.
Please help me.

Is This A Good Question/Topic? 0
  • +

Replies To: Comparing two listbox items...

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Comparing two listbox items...

Posted 07 October 2012 - 12:37 AM

If you have listboxes, where only one item can be selected, than: if (curItem == curItem2).
Was This Post Helpful? 0
  • +
  • -

#3 Deadboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 12

Re: Comparing two listbox items...

Posted 07 October 2012 - 12:45 AM

No,it is not necessary that both the items in the listboxes must be the same,they can be different.
Was This Post Helpful? 0
  • +
  • -

#4 Deadboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 12

Re: Comparing two listbox items...

Posted 07 October 2012 - 01:19 AM

Anyone?What i want to do is find which items are selected in both the listboxes and,based on the combination,perform different functions.
Was This Post Helpful? 0
  • +
  • -

#5 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Comparing two listbox items...

Posted 07 October 2012 - 02:04 AM

In case of multiple selected items, you can do:
            for (int n = 0; n <= listBox1.SelectedItems.Count - 1; n++)
            {
                if (listBox1.SelectedItems[n].ToString() != listBox2.SelectedItems[n].ToString())
                {
                    return;
                }
            }
            textBlock4.Text = "OK";


Was This Post Helpful? 0
  • +
  • -

#6 Deadboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 12

Re: Comparing two listbox items...

Posted 07 October 2012 - 05:26 AM

Sorry,i meant that only ONE item can be selected from each listbox and based on the combination perform different combinations.

I mean different functions based on diferent combinations.What should i do?
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Comparing two listbox items...

Posted 07 October 2012 - 10:38 AM

There are many ways to do this: lots of 'if' statements, 'case' statements, arrays of delegates. Which are you comfortable with using?
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: Comparing two listbox items...

Posted 07 October 2012 - 04:22 PM

Can you use
 if ((listBox1.SelectedIndex = 0) && (listBox2.SelectedIndex= 3))
    {
      //do something
    }

This post has been edited by CharlieMay: 07 October 2012 - 04:23 PM

Was This Post Helpful? 1
  • +
  • -

#9 PyroCybin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 02-July 12

Re: Comparing two listbox items...

Posted 07 October 2012 - 09:03 PM

What kind of comparison are you trying to perform? To compare indices you can use the method above, to compare the contents of the list you can use a cast.


if((int)listBox1.SelectedItem < (int)listBox1.SelectedItem){}


Sorry about the double post but i can't edit yet. that code should have been

if((int)listBox1.SelectedItem < (int)listBox2.SelectedItem){}

Was This Post Helpful? 1
  • +
  • -

#10 Deadboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 12

Re: Comparing two listbox items...

Posted 08 October 2012 - 04:46 AM

I had to compare indices.CharlieMay's method worked. :bigsmile:
Thanks PyroCybin,it might be useful later.
Was This Post Helpful? 0
  • +
  • -

#11 Deadboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 12

Re: Comparing two listbox items...

Posted 08 October 2012 - 05:47 AM

One more thing...

Actually,I'm trying to make a unit converter in which 1st listbox contains about 15 units like meter,foot,etc...The 2nd listbox contains same units.I wanted the user to select one unit from each listbox and calculate the values approximately.But using above method will take many lines of code as i'll write separate code block for each pair of units.

Is there any fast comparison method?

Sorry,i meant 'appropriately' not 'approximately'.
Was This Post Helpful? 0
  • +
  • -

#12 PyroCybin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 02-July 12

Re: Comparing two listbox items...

Posted 08 October 2012 - 05:50 AM

A somewhat simpler way would be to choose some reference unit, say meters. Then you simply write a function for converting each unit to meters and a function for converting meters into each unit. Then to calculate say feet to inches you first convert feet to meters and then meters to inches. its less efficient but uses less code.
Was This Post Helpful? 0
  • +
  • -

#13 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Comparing two listbox items...

Posted 08 October 2012 - 08:39 AM

Using the indexes is bad design. What happens if you change the order or add a new one?
PyroCybin has the right idea, I'd do something like:

switch (<FromListBoxValueAsString>) {
    case "feet": meters = FeetToMeters(<NumberToConvert>); break;
    case "inches": meters = InchesToMeters(<NumberToConvert>); break;
    etc.
}

switch (<ToListBoxValueAsString>) {
    case "feet" : result = MetersToFeet(meters); break;
    case "inches" : result = MetersToInches(meters); break;
    etc.
}

Display(result);

The stuff in <> you'll have to figure out how to get and substitute, not meant as actual variable names
Was This Post Helpful? 1
  • +
  • -

#14 Deadboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 12

Re: Comparing two listbox items...

Posted 25 October 2012 - 05:29 AM

Thanks,i'll try that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1