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;
I am trying to check if a combo box is empty using C# in a Windows Application Form. Below are two possible ways:
- if (string.IsNullOrEmpty(comboBox1.Text))
- 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
add a comment |
I am trying to check if a combo box is empty using C# in a Windows Application Form. Below are two possible ways:
- if (string.IsNullOrEmpty(comboBox1.Text))
- 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
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 asif (comboBox1.SelectedIndex < 0)
in order not to use magic constant-1
– Dmitry Bychenko
Mar 26 at 12:41
add a comment |
I am trying to check if a combo box is empty using C# in a Windows Application Form. Below are two possible ways:
- if (string.IsNullOrEmpty(comboBox1.Text))
- 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
I am trying to check if a combo box is empty using C# in a Windows Application Form. Below are two possible ways:
- if (string.IsNullOrEmpty(comboBox1.Text))
- 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
c# winforms combobox
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 asif (comboBox1.SelectedIndex < 0)
in order not to use magic constant-1
– Dmitry Bychenko
Mar 26 at 12:41
add a comment |
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 asif (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
add a comment |
3 Answers
3
active
oldest
votes
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
It would prohibit making changes to the combo in code...
– Werner
Mar 26 at 12:45
add a comment |
for combobox you can use this code below to check if it's empty or not
if(comboBox1.Items.Count == 0 )
// your code
add a comment |
This is what i try and it 's work. Feel free to comment:
if (comboBox1.SelectedIndex > -1 )
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
It would prohibit making changes to the combo in code...
– Werner
Mar 26 at 12:45
add a comment |
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
It would prohibit making changes to the combo in code...
– Werner
Mar 26 at 12:45
add a comment |
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
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
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
add a comment |
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
add a comment |
for combobox you can use this code below to check if it's empty or not
if(comboBox1.Items.Count == 0 )
// your code
add a comment |
for combobox you can use this code below to check if it's empty or not
if(comboBox1.Items.Count == 0 )
// your code
add a comment |
for combobox you can use this code below to check if it's empty or not
if(comboBox1.Items.Count == 0 )
// your code
for combobox you can use this code below to check if it's empty or not
if(comboBox1.Items.Count == 0 )
// your code
answered Mar 26 at 12:47
sayah imadsayah imad
5314 silver badges15 bronze badges
5314 silver badges15 bronze badges
add a comment |
add a comment |
This is what i try and it 's work. Feel free to comment:
if (comboBox1.SelectedIndex > -1 )
add a comment |
This is what i try and it 's work. Feel free to comment:
if (comboBox1.SelectedIndex > -1 )
add a comment |
This is what i try and it 's work. Feel free to comment:
if (comboBox1.SelectedIndex > -1 )
This is what i try and it 's work. Feel free to comment:
if (comboBox1.SelectedIndex > -1 )
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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