Check if Combo box is empty C#How do I calculate someone's age in C#?What is the difference between String and string in C#?Hidden Features of C#?Cast int to enum in C#How do I enumerate an enum in C#?What are the correct version numbers for C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?making a combo box editableType Checking: typeof, GetType, or is?How to Create combo box to auto fill while user type the spelings inside the combo Box in c#

Inside Out and Back to Front

"move up the school" meaning

What is the minimum wait before I may I re-enter the USA after a 90 day visit on the Visa B-2 Program?

Does a hash function have a Upper bound on input length?

Why should fork() have been designed to return a file descriptor?

Time war story - soldier name lengthens as he travels further from the battle front

Can a person become a professor in English without having a Bachelor degree in English?

Book in which the "mountain" in the distance was a hole in the flat world

Do pedestrians imitate automotive traffic?

How far off did Apollo 11 land?

Could Europeans in Europe demand protection under UN Declaration on the Rights of Indigenous Peoples?

Nilpotent elements of Lie algebra and unipotent groups

How to declare an array without specifying its size, but with an initializer inside a class in C++?

Making an example from 'Clean Code' more functional

A bicolour masyu

Do you need to have the original move to take a "Replaces:" move?

Alignment problem with a mathematical equation in a presentation in beamer

Soft constraints and hard constraints

Remove side menu(right side) from finder

What would be the effects of (relatively) widespread precognition on the stock market?

French equivalents of "X puts the smile back on her face"

How to hook up Korg EX-8000 to a computer w/o a keyboard?

Why didn't Balak request Bilam to bless his own people?

Why are flying carpets banned while flying brooms are not?



Check if Combo box is empty C#


How do I calculate someone's age in C#?What is the difference between String and string in C#?Hidden Features of C#?Cast int to enum in C#How do I enumerate an enum in C#?What are the correct version numbers for C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?making a combo box editableType Checking: typeof, GetType, or is?How to Create combo box to auto fill while user type the spelings inside the combo Box in c#






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am trying to check if a combo box is empty using C# in a Windows Application Form. Below are two possible ways:



  1. if (string.IsNullOrEmpty(comboBox1.Text))

  2. if (comboBox1.SelectedIndex == -1)

In order to ensure that the user would ONLY select a value from the drop down and NOT write it s own answer, which is the best approach?
From my research the second method (if (comboBox1.SelectedIndex == -1)) will satisfy my needs. Am l right?










share|improve this question
























  • If dropdown lits has empty string as an option the 1st possibility is wrong (user can deliberatly choose the empty option). In case of 2nd option I'd rather put it as if (comboBox1.SelectedIndex < 0) in order not to use magic constant -1

    – Dmitry Bychenko
    Mar 26 at 12:41


















0















I am trying to check if a combo box is empty using C# in a Windows Application Form. Below are two possible ways:



  1. if (string.IsNullOrEmpty(comboBox1.Text))

  2. if (comboBox1.SelectedIndex == -1)

In order to ensure that the user would ONLY select a value from the drop down and NOT write it s own answer, which is the best approach?
From my research the second method (if (comboBox1.SelectedIndex == -1)) will satisfy my needs. Am l right?










share|improve this question
























  • If dropdown lits has empty string as an option the 1st possibility is wrong (user can deliberatly choose the empty option). In case of 2nd option I'd rather put it as if (comboBox1.SelectedIndex < 0) in order not to use magic constant -1

    – Dmitry Bychenko
    Mar 26 at 12:41














0












0








0








I am trying to check if a combo box is empty using C# in a Windows Application Form. Below are two possible ways:



  1. if (string.IsNullOrEmpty(comboBox1.Text))

  2. if (comboBox1.SelectedIndex == -1)

In order to ensure that the user would ONLY select a value from the drop down and NOT write it s own answer, which is the best approach?
From my research the second method (if (comboBox1.SelectedIndex == -1)) will satisfy my needs. Am l right?










share|improve this question
















I am trying to check if a combo box is empty using C# in a Windows Application Form. Below are two possible ways:



  1. if (string.IsNullOrEmpty(comboBox1.Text))

  2. if (comboBox1.SelectedIndex == -1)

In order to ensure that the user would ONLY select a value from the drop down and NOT write it s own answer, which is the best approach?
From my research the second method (if (comboBox1.SelectedIndex == -1)) will satisfy my needs. Am l right?







c# winforms combobox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 12:43









Michal Ciechan

8,87310 gold badges57 silver badges98 bronze badges




8,87310 gold badges57 silver badges98 bronze badges










asked Mar 26 at 12:37









Error 1004Error 1004

5,1422 gold badges8 silver badges25 bronze badges




5,1422 gold badges8 silver badges25 bronze badges












  • If dropdown lits has empty string as an option the 1st possibility is wrong (user can deliberatly choose the empty option). In case of 2nd option I'd rather put it as if (comboBox1.SelectedIndex < 0) in order not to use magic constant -1

    – Dmitry Bychenko
    Mar 26 at 12:41


















  • If dropdown lits has empty string as an option the 1st possibility is wrong (user can deliberatly choose the empty option). In case of 2nd option I'd rather put it as if (comboBox1.SelectedIndex < 0) in order not to use magic constant -1

    – Dmitry Bychenko
    Mar 26 at 12:41

















If dropdown lits has empty string as an option the 1st possibility is wrong (user can deliberatly choose the empty option). In case of 2nd option I'd rather put it as if (comboBox1.SelectedIndex < 0) in order not to use magic constant -1

– Dmitry Bychenko
Mar 26 at 12:41






If dropdown lits has empty string as an option the 1st possibility is wrong (user can deliberatly choose the empty option). In case of 2nd option I'd rather put it as if (comboBox1.SelectedIndex < 0) in order not to use magic constant -1

– Dmitry Bychenko
Mar 26 at 12:41













3 Answers
3






active

oldest

votes


















1














If your concern is only making sure that users choose an item from the list available, simply change your combobox's DropDownStyle to DropDownList



or if you want to allow them to type but then ensure it is on the list, you can do something like this:



var txt = comboBox1.Text;

if(string.IsNullOrEmpty())
return;

var test = comboBox1.Items?.OfType<string>().ToList().Any(x => x?.Trim() == txt?.Trim());


so if test is false, it means what they have selected/typed does not exist in list of available items






share|improve this answer

























  • It would prohibit making changes to the combo in code...

    – Werner
    Mar 26 at 12:45



















0














for combobox you can use this code below to check if it's empty or not



 if(comboBox1.Items.Count == 0 )

// your code






share|improve this answer






























    0














    This is what i try and it 's work. Feel free to comment:



    if (comboBox1.SelectedIndex > -1 )





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55357385%2fcheck-if-combo-box-is-empty-c-sharp%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If your concern is only making sure that users choose an item from the list available, simply change your combobox's DropDownStyle to DropDownList



      or if you want to allow them to type but then ensure it is on the list, you can do something like this:



      var txt = comboBox1.Text;

      if(string.IsNullOrEmpty())
      return;

      var test = comboBox1.Items?.OfType<string>().ToList().Any(x => x?.Trim() == txt?.Trim());


      so if test is false, it means what they have selected/typed does not exist in list of available items






      share|improve this answer

























      • It would prohibit making changes to the combo in code...

        – Werner
        Mar 26 at 12:45
















      1














      If your concern is only making sure that users choose an item from the list available, simply change your combobox's DropDownStyle to DropDownList



      or if you want to allow them to type but then ensure it is on the list, you can do something like this:



      var txt = comboBox1.Text;

      if(string.IsNullOrEmpty())
      return;

      var test = comboBox1.Items?.OfType<string>().ToList().Any(x => x?.Trim() == txt?.Trim());


      so if test is false, it means what they have selected/typed does not exist in list of available items






      share|improve this answer

























      • It would prohibit making changes to the combo in code...

        – Werner
        Mar 26 at 12:45














      1












      1








      1







      If your concern is only making sure that users choose an item from the list available, simply change your combobox's DropDownStyle to DropDownList



      or if you want to allow them to type but then ensure it is on the list, you can do something like this:



      var txt = comboBox1.Text;

      if(string.IsNullOrEmpty())
      return;

      var test = comboBox1.Items?.OfType<string>().ToList().Any(x => x?.Trim() == txt?.Trim());


      so if test is false, it means what they have selected/typed does not exist in list of available items






      share|improve this answer















      If your concern is only making sure that users choose an item from the list available, simply change your combobox's DropDownStyle to DropDownList



      or if you want to allow them to type but then ensure it is on the list, you can do something like this:



      var txt = comboBox1.Text;

      if(string.IsNullOrEmpty())
      return;

      var test = comboBox1.Items?.OfType<string>().ToList().Any(x => x?.Trim() == txt?.Trim());


      so if test is false, it means what they have selected/typed does not exist in list of available items







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 26 at 12:49

























      answered Mar 26 at 12:42









      Amir No-FamilyAmir No-Family

      5474 silver badges13 bronze badges




      5474 silver badges13 bronze badges












      • It would prohibit making changes to the combo in code...

        – Werner
        Mar 26 at 12:45


















      • It would prohibit making changes to the combo in code...

        – Werner
        Mar 26 at 12:45

















      It would prohibit making changes to the combo in code...

      – Werner
      Mar 26 at 12:45






      It would prohibit making changes to the combo in code...

      – Werner
      Mar 26 at 12:45














      0














      for combobox you can use this code below to check if it's empty or not



       if(comboBox1.Items.Count == 0 )

      // your code






      share|improve this answer



























        0














        for combobox you can use this code below to check if it's empty or not



         if(comboBox1.Items.Count == 0 )

        // your code






        share|improve this answer

























          0












          0








          0







          for combobox you can use this code below to check if it's empty or not



           if(comboBox1.Items.Count == 0 )

          // your code






          share|improve this answer













          for combobox you can use this code below to check if it's empty or not



           if(comboBox1.Items.Count == 0 )

          // your code







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 26 at 12:47









          sayah imadsayah imad

          5314 silver badges15 bronze badges




          5314 silver badges15 bronze badges





















              0














              This is what i try and it 's work. Feel free to comment:



              if (comboBox1.SelectedIndex > -1 )





              share|improve this answer



























                0














                This is what i try and it 's work. Feel free to comment:



                if (comboBox1.SelectedIndex > -1 )





                share|improve this answer

























                  0












                  0








                  0







                  This is what i try and it 's work. Feel free to comment:



                  if (comboBox1.SelectedIndex > -1 )





                  share|improve this answer













                  This is what i try and it 's work. Feel free to comment:



                  if (comboBox1.SelectedIndex > -1 )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 26 at 13:58









                  Error 1004Error 1004

                  5,1422 gold badges8 silver badges25 bronze badges




                  5,1422 gold badges8 silver badges25 bronze badges



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55357385%2fcheck-if-combo-box-is-empty-c-sharp%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

                      밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

                      1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴