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

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현