I have a case statement that evaluates a string in a combo box on a form which is either English or Metric. What I'm finding is that the case statement does not see the pull down box contents which is loaded by default unless it is physically reselected by the user. In other words the combo box defaults to "metric" but the case statement does not recognize it unless I go and reselect "metric" in the combo box. Any help would be appreciated.

Dani AI

Generated

— quick diagnosis and practical fixes based on the posts by , and .

The visible item in the dropdown can come from three different places (the control Text, a data-bound item such as a DataRowView, or the SelectedItem/SelectedValue). Common causes for the behavior you describe are: (1) your code reads the combo before the binding/selection is established, (2) the combo is data-bound so SelectedItem is not a string (it may be a DataRowView), or (3) a simple case/whitespace mismatch ("metric" != "Metric"). That also explains the compile error you saw when trying to switch on SelectedItem — SelectedItem is an object and must be converted to a string for a string-based switch.

Use a small, defensive helper to get the actual text regardless of how the combo was filled:

// safe read whether the combo is filled from objects or plain strings
string GetComboText(ComboBox cb, string displayMember)
{
    if (cb.SelectedItem is System.Data.DataRowView drv)
        return (drv[displayMember] ?? string.Empty).ToString().Trim();
    return (cb.Text ?? string.Empty).Trim();
}

Then compare explicitly and case-insensitively:

string units = GetComboText(bunitsCB, "UnitColumnName");
if (string.Equals(units, "metric", StringComparison.OrdinalIgnoreCase)) { /* metric logic */ }
else if (string.Equals(units, "english", StringComparison.OrdinalIgnoreCase)) { /* english logic */ }
else { /* no selection */ }

Practical checklist:

  • If you set DataSource, set DisplayMember/ValueMember and then set SelectedIndex (or SelectedValue) after the data load so the selection is definite.
  • If you read the combo in Form.Load, try moving the logic to Form.Shown or run it after binding completes.
  • Use TryParse for numeric conversions to avoid exceptions from empty text or culture issues.

This addresses the visible-but-unread symptom, the SelectedItem compile error, and the case-sensitivity pitfall.

Recommended Answers

All 13 Replies

Whats wrong with the other question you already have a thread on this subject for?

different problem

i don't understand very well your problem. Give us the code, example... don't let us guess.

If this is the problem I think it is, I usually fix it by using the Text property of the combo box. It should allow you to get the text of what's in the combo box without the user actually having to select it.

that is what im using the text property of the combo box

OK...have you tried using the SelectedItem property?

OK...have you tried using the SelectedItem property?

Yes I get an error saying " a value of an integral type is expected"

Show some code, show the line with the error, show what you tried to do to fix it

Here is my code and when I run it always gets to the default althought there are strings showing in the combo box that came from the data base

switch (bunitsCB.Text)
{
case "Metric":
bl = Convert.ToDecimal(blanklTB.Text) / Convert.ToDecimal(25.4);
bw = Convert.ToDecimal(blankwTB.Text) / Convert.ToDecimal(25.4);
bp = Convert.ToDecimal(blankpTB.Text) / Convert.ToDecimal(25.4);
pd = Convert.ToDecimal(partdepthTB.Text) / Convert.ToDecimal(25.4);break;
case "English":
bl = Convert.ToDecimal(blanklTB.Text);
bw = Convert.ToDecimal(blankwTB.Text);
bp = Convert.ToDecimal(blankpTB.Text);
pd = Convert.ToDecimal(partdepthTB.Text);break;
default:
bl = 0;
bw = 0;
bp = 0;
pd = 0;
checkLB.Items.Add("YOU MUST SELECT A BLANK UNIT OF MEASURE");break;
}
switch (dieunitsCB.Text)
{
case "Metric":
dl = Convert.ToDecimal(dielTB.Text) / Convert.ToDecimal(25.4);
dw = Convert.ToDecimal(diewTB.Text) / Convert.ToDecimal(25.4);
ds = Convert.ToDecimal(dieshTB.Text) / Convert.ToDecimal(25.4);break;
case "English":
dl = Convert.ToDecimal(dielTB.Text);
dw = Convert.ToDecimal(diewTB.Text);
ds = Convert.ToDecimal(dieshTB.Text);break;
default:
dl = 0;
dw = 0;
ds = 0;
checkLB.Items.Add("YOU MUST SELECT A DIE UNIT OF MEASURE");break;
}

And in debugging what was the value of say "bunitsCB.Text"

And in debugging what was the value of say "bunitsCB.Text"

Thats just the problem. Im not getting a value for bunitsCB.Text. The value is visible on the screen but I cannot figure out how to call it for the switch statement. It is in the database, it shows in the combo box but unless I reselect it it does not get assigned to bunitsCB.Text.

HELPPPPPPP

Then dont use the .Text but the selected item... and yes I know you had an error before but it was the right thing just in the wrong way

When you set the DataSource or load the Items manually, you could just set the SelectedIndex property to 0. That should keep you from getting errors about nothing being selected. I guess whether or not you can do this would have to depend on any Business rules in place or requirements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.