Why doesn't my code find the .SelectedIndex?
-
I don't understand why my code snippet below won't find "14" and set the .SelectedIndex? The .Selectindex is always -1.
comboBoxLSKFontSize.Items.Add("10";);
comboBoxLSKFontSize.Items.Add("11";);
comboBoxLSKFontSize.Items.Add("12";);
comboBoxLSKFontSize.Items.Add("13";);
comboBoxLSKFontSize.Items.Add("14";);
comboBoxLSKFontSize.Items.Add("15";);
comboBoxLSKFontSize.Items.Add("16";);
stringLSKFontSize = "14";
i = 0;
foreach (string str in comboBoxLSKFontSize.Items)
{
if (stringLSKFontSize == str)
{
comboBoxLSKFontSize.SelectedIndex = i;
break;
}
i++;
} -
I don't understand why my code snippet below won't find "14" and set the .SelectedIndex? The .Selectindex is always -1.
comboBoxLSKFontSize.Items.Add("10";);
comboBoxLSKFontSize.Items.Add("11";);
comboBoxLSKFontSize.Items.Add("12";);
comboBoxLSKFontSize.Items.Add("13";);
comboBoxLSKFontSize.Items.Add("14";);
comboBoxLSKFontSize.Items.Add("15";);
comboBoxLSKFontSize.Items.Add("16";);
stringLSKFontSize = "14";
i = 0;
foreach (string str in comboBoxLSKFontSize.Items)
{
if (stringLSKFontSize == str)
{
comboBoxLSKFontSize.SelectedIndex = i;
break;
}
i++;
}Your code would work if you cleaned up the many obvious syntax errors in it so it would compile without error !
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
-
I don't understand why my code snippet below won't find "14" and set the .SelectedIndex? The .Selectindex is always -1.
comboBoxLSKFontSize.Items.Add("10";);
comboBoxLSKFontSize.Items.Add("11";);
comboBoxLSKFontSize.Items.Add("12";);
comboBoxLSKFontSize.Items.Add("13";);
comboBoxLSKFontSize.Items.Add("14";);
comboBoxLSKFontSize.Items.Add("15";);
comboBoxLSKFontSize.Items.Add("16";);
stringLSKFontSize = "14";
i = 0;
foreach (string str in comboBoxLSKFontSize.Items)
{
if (stringLSKFontSize == str)
{
comboBoxLSKFontSize.SelectedIndex = i;
break;
}
i++;
}As Bill says: if you fix the syntax errors by removing the spurious semicolons:
comboBoxLSKFontSize.Items.Add("10");
comboBoxLSKFontSize.Items.Add("11");
comboBoxLSKFontSize.Items.Add("12");
comboBoxLSKFontSize.Items.Add("13");
comboBoxLSKFontSize.Items.Add("14");
comboBoxLSKFontSize.Items.Add("15");
comboBoxLSKFontSize.Items.Add("16");
stringLSKFontSize = "14";
i = 0;
foreach (string str in comboBoxLSKFontSize.Items)
{
if (stringLSKFontSize == str)
{
comboBoxLSKFontSize.SelectedIndex = i;
break;
}
i++;
}Then that code should work, assuming that
stringLSKFontSize
andi
have been declared. But with the errors, it won't compile, which means that it doesn't produce an executable file, so you will be running the last version that did compile correctly and your changes do not have effect. If once it compiles you still have the problem, then use the debugger by putting a breakpoint on the line:stringLSKFontSize = "14";
And step though your code to find out exactly what it is doing. And you do realise that you don't have to do that at all, right?
comboBoxLSKFontSize.Items.Add("10");
comboBoxLSKFontSize.Items.Add("11");
comboBoxLSKFontSize.Items.Add("12");
comboBoxLSKFontSize.Items.Add("13");
comboBoxLSKFontSize.Items.Add("14");
comboBoxLSKFontSize.Items.Add("15");
comboBoxLSKFontSize.Items.Add("16");
comboBoxLSKFontSize.SelectedItem = "14";Will do exactly the same thing... BTW: It's not considered good practice to put the type of the variable as part of the variable name: you should consider using just LSKFontSize and LSKSelectedFontSize instead. Visual Studio will tell you the type of a variable if you just wave your mouse over the variable name.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
As Bill says: if you fix the syntax errors by removing the spurious semicolons:
comboBoxLSKFontSize.Items.Add("10");
comboBoxLSKFontSize.Items.Add("11");
comboBoxLSKFontSize.Items.Add("12");
comboBoxLSKFontSize.Items.Add("13");
comboBoxLSKFontSize.Items.Add("14");
comboBoxLSKFontSize.Items.Add("15");
comboBoxLSKFontSize.Items.Add("16");
stringLSKFontSize = "14";
i = 0;
foreach (string str in comboBoxLSKFontSize.Items)
{
if (stringLSKFontSize == str)
{
comboBoxLSKFontSize.SelectedIndex = i;
break;
}
i++;
}Then that code should work, assuming that
stringLSKFontSize
andi
have been declared. But with the errors, it won't compile, which means that it doesn't produce an executable file, so you will be running the last version that did compile correctly and your changes do not have effect. If once it compiles you still have the problem, then use the debugger by putting a breakpoint on the line:stringLSKFontSize = "14";
And step though your code to find out exactly what it is doing. And you do realise that you don't have to do that at all, right?
comboBoxLSKFontSize.Items.Add("10");
comboBoxLSKFontSize.Items.Add("11");
comboBoxLSKFontSize.Items.Add("12");
comboBoxLSKFontSize.Items.Add("13");
comboBoxLSKFontSize.Items.Add("14");
comboBoxLSKFontSize.Items.Add("15");
comboBoxLSKFontSize.Items.Add("16");
comboBoxLSKFontSize.SelectedItem = "14";Will do exactly the same thing... BTW: It's not considered good practice to put the type of the variable as part of the variable name: you should consider using just LSKFontSize and LSKSelectedFontSize instead. Visual Studio will tell you the type of a variable if you just wave your mouse over the variable name.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...