Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Contains dows not work properly...?

Contains dows not work properly...?

Scheduled Pinned Locked Moved C#
regexquestion
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pankaj Joshi
    wrote on last edited by
    #1

    Hello experts, I have a combo box with 26 items. Say A....Z. I have another string that holds the item to match I write following code..but it is not working why...? strItemToMatch="C"; boolean isFound=false; if (ComboBox1.Items.Contains(strItemToMatch.ToString())) isFound=True; else isFound=False;

    Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

    D M L 3 Replies Last reply
    0
    • P Pankaj Joshi

      Hello experts, I have a combo box with 26 items. Say A....Z. I have another string that holds the item to match I write following code..but it is not working why...? strItemToMatch="C"; boolean isFound=false; if (ComboBox1.Items.Contains(strItemToMatch.ToString())) isFound=True; else isFound=False;

      Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Change if (ComboBox1.Items.Contains(strItemToMatch.ToString())) to if (ComboBox1.Items.ToString().ToUpper().Contains(strItemToMatch.ToString().ToUpper())) Hope it helps

      Until you realize this message has nothing to say, its too late to stop reading

      P M 2 Replies Last reply
      0
      • D dan sh

        Change if (ComboBox1.Items.Contains(strItemToMatch.ToString())) to if (ComboBox1.Items.ToString().ToUpper().Contains(strItemToMatch.ToString().ToUpper())) Hope it helps

        Until you realize this message has nothing to say, its too late to stop reading

        P Offline
        P Offline
        Pankaj Joshi
        wrote on last edited by
        #3

        I write this if (Convert.ToBoolean(cmbSubItem.Items.ToString().ToUpper().Trim().Contains(itemlist[0].ToString().ToUpper().Trim()))) But this is also not working...

        Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

        1 Reply Last reply
        0
        • D dan sh

          Change if (ComboBox1.Items.Contains(strItemToMatch.ToString())) to if (ComboBox1.Items.ToString().ToUpper().Contains(strItemToMatch.ToString().ToUpper())) Hope it helps

          Until you realize this message has nothing to say, its too late to stop reading

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          d@nish wrote:

          if (ComboBox1.Items.ToString().ToUpper().Contains(strItemToMatch.ToString().ToUpper()))

          Sorry to say, but that's complete nonsense. ComboBox1.Items.ToString() will give you "System.Windows.Forms.ComboBox+ObjectCollection", so you send him down the wrong road. X|

          Regards, mav -- Black holes are the places where God divided by 0...

          D P 2 Replies Last reply
          0
          • M mav northwind

            d@nish wrote:

            if (ComboBox1.Items.ToString().ToUpper().Contains(strItemToMatch.ToString().ToUpper()))

            Sorry to say, but that's complete nonsense. ComboBox1.Items.ToString() will give you "System.Windows.Forms.ComboBox+ObjectCollection", so you send him down the wrong road. X|

            Regards, mav -- Black holes are the places where God divided by 0...

            P Offline
            P Offline
            Pankaj Joshi
            wrote on last edited by
            #5

            Thanks for your reply I already explore this error.... And now code is string strItemNameToFound = itemlist[0].ToString().Trim(); if (Convert.ToBoolean(cmbSubItem.Items.Contains(strItemNameToFound))) But this is still not working any help...?

            Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

            1 Reply Last reply
            0
            • M mav northwind

              d@nish wrote:

              if (ComboBox1.Items.ToString().ToUpper().Contains(strItemToMatch.ToString().ToUpper()))

              Sorry to say, but that's complete nonsense. ComboBox1.Items.ToString() will give you "System.Windows.Forms.ComboBox+ObjectCollection", so you send him down the wrong road. X|

              Regards, mav -- Black holes are the places where God divided by 0...

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #6

              My fault I accept. Then the only way I can think of is to make sure that you are using right cases."Contains" is case sensitive.

              Until you realize this message has nothing to say, its too late to stop reading

              1 Reply Last reply
              0
              • P Pankaj Joshi

                Hello experts, I have a combo box with 26 items. Say A....Z. I have another string that holds the item to match I write following code..but it is not working why...? strItemToMatch="C"; boolean isFound=false; if (ComboBox1.Items.Contains(strItemToMatch.ToString())) isFound=True; else isFound=False;

                Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                M Offline
                M Offline
                mav northwind
                wrote on last edited by
                #7

                Hi! ComboBox.ObjectCollection.Contains() does work properly, you're not using it correctly. My guess is that you're not adding the items as strings, so the result you get from Contains() is correct. Take a look at that code:

                foreach (string s in new string { "A", "B", "C", "D", "E", "F" })
                ComboBox1.Items.Add(s);

                Debug.Assert(ComboBox1.Items.Contains("C")); // Doesn't show an assertion message
                Debug.Assert(!ComboBox1.Items.Contains("Z")); // Doesn't show an assertion message

                ComboBox1.Items.Clear();

                foreach (char c in new char[] { 'A', 'B', 'C', 'D', 'E', 'F' })
                ComboBox1.Items.Add(c);

                Debug.Assert(ComboBox1.Items.Contains("C")); // Does show an assertion message
                Debug.Assert(!ComboBox1.Items.Contains("Z")); // Does show an assertion message

                Regards, mav -- Black holes are the places where God divided by 0...

                P 1 Reply Last reply
                0
                • M mav northwind

                  Hi! ComboBox.ObjectCollection.Contains() does work properly, you're not using it correctly. My guess is that you're not adding the items as strings, so the result you get from Contains() is correct. Take a look at that code:

                  foreach (string s in new string { "A", "B", "C", "D", "E", "F" })
                  ComboBox1.Items.Add(s);

                  Debug.Assert(ComboBox1.Items.Contains("C")); // Doesn't show an assertion message
                  Debug.Assert(!ComboBox1.Items.Contains("Z")); // Doesn't show an assertion message

                  ComboBox1.Items.Clear();

                  foreach (char c in new char[] { 'A', 'B', 'C', 'D', 'E', 'F' })
                  ComboBox1.Items.Add(c);

                  Debug.Assert(ComboBox1.Items.Contains("C")); // Does show an assertion message
                  Debug.Assert(!ComboBox1.Items.Contains("Z")); // Does show an assertion message

                  Regards, mav -- Black holes are the places where God divided by 0...

                  P Offline
                  P Offline
                  Pankaj Joshi
                  wrote on last edited by
                  #8

                  I agreed with you. But I'm not adding the items same as you. I'm filling item names from database and directly assign the datatable as datasource. And this is also not working why.....?

                  Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                  M 1 Reply Last reply
                  0
                  • P Pankaj Joshi

                    Hello experts, I have a combo box with 26 items. Say A....Z. I have another string that holds the item to match I write following code..but it is not working why...? strItemToMatch="C"; boolean isFound=false; if (ComboBox1.Items.Contains(strItemToMatch.ToString())) isFound=True; else isFound=False;

                    Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Hi, ComboBox.FindString and ComboBox.FindStringExact that may help you. It is often useful to read the documentation on the classes you intend to use. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


                    P 1 Reply Last reply
                    0
                    • P Pankaj Joshi

                      I agreed with you. But I'm not adding the items same as you. I'm filling item names from database and directly assign the datatable as datasource. And this is also not working why.....?

                      Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                      M Offline
                      M Offline
                      mav northwind
                      wrote on last edited by
                      #10

                      There you got it: You're adding objects to the ComboBox by databinding and then ask the ComboBox if its Items collection contains a string. What the ComboBox is showing is the result of a call to .ToString() for each object in the Items list, but the elements in the List are not the actual strings! So, as Luc suggested, using FindString() or FindStringExact() will probably be the easiest way.

                      Regards, mav -- Black holes are the places where God divided by 0...

                      P 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        Hi, ComboBox.FindString and ComboBox.FindStringExact that may help you. It is often useful to read the documentation on the classes you intend to use. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


                        P Offline
                        P Offline
                        Pankaj Joshi
                        wrote on last edited by
                        #11

                        Hey Luc thank you very much for your reply. It is working fine. I also find the source of the problem. Actually I directly assignthe data table as data source so what .NET do..? It will add the data rows in the items collection. And that's why Contains not work because we are searching a string in complete data row. I'll definately take care of it in the future. Thank you once again. :rose: One more question is their any problem to use FindString...?

                        Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                        1 Reply Last reply
                        0
                        • M mav northwind

                          There you got it: You're adding objects to the ComboBox by databinding and then ask the ComboBox if its Items collection contains a string. What the ComboBox is showing is the result of a call to .ToString() for each object in the Items list, but the elements in the List are not the actual strings! So, as Luc suggested, using FindString() or FindStringExact() will probably be the easiest way.

                          Regards, mav -- Black holes are the places where God divided by 0...

                          P Offline
                          P Offline
                          Pankaj Joshi
                          wrote on last edited by
                          #12

                          Ya I got it. Thank you very much for your help. :rose:

                          Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups