Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

issue with a case statement

Status
Not open for further replies.

Saturn57

Programmer
Aug 30, 2007
275
CA
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.
 
You can set the selected item in the code, after the user has chosen the item you select it again in the code. Anyway yours is a pretty weird error that shouldn't happen. Are you checking the SelectedItem of the ComboBox directly? If you are putting the value into a ComboBox_SelectedIndexChanged you may have problems. So I suggest checking the value directly (so, don't use ComboBox_SelectedIndexChanged)
 
Yes my code is as follows:

private void diesizeBN_Click(object sender, EventArgs e)
{
decimal blankl;
decimal blankw;
decimal.TryParse(blanklTB.Text, out blankl);
decimal.TryParse(blankwTB.Text, out blankw);

if (bunitsCB.Text == "English")
{
switch (dieunitsCB.Text)
{
case "English":
dielTB.Text = Convert.ToString(blankl + Convert.ToDecimal(24));
diewTB.Text = Convert.ToString(blankw + Convert.ToDecimal(24)); break;
case "Metric":
dielTB.Text = Convert.ToString(Math.Round((blankl * Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(588));
diewTB.Text = Convert.ToString(Math.Round((blankw * Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(588)); break;
default:
throw new Exception("Unknown selection");
}
}
else
{
switch (dieunitsCB.Text)
{
case "English":
dielTB.Text = Convert.ToString(Math.Round((blankl / Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(24));
diewTB.Text = Convert.ToString(Math.Round((blankw / Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(24)); break;
case "Metric":
dielTB.Text = Convert.ToString(blankl + Convert.ToDecimal(588));
diewTB.Text = Convert.ToString(blankw + Convert.ToDecimal(588)); break;
default:
MessageBox.Show("Please make sure you have entered a unit type,length,width "); break;
}
}
 
Place your code in the SelectedIndexChanged event rather than the Click event.
 
The code you posted is the actual code your having problem with? I noticed there are 2 levels of condition, and looks like both are testing values from CB controls, which I assume are comboboxes. If BN_Click() is a button handler, I don't see any immediate problems here. perhaps a problem on casing? try selecting the "English" option and see what happens.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top