Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why doesn't my code find the .SelectedIndex?

Why doesn't my code find the .SelectedIndex?

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rfresh
    wrote on last edited by
    #1

    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++;
    }

    B OriginalGriffO 2 Replies Last reply
    0
    • R rfresh

      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++;
      }

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • R rfresh

        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++;
        }

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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 and i 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...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        R 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          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 and i 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...

          R Offline
          R Offline
          rfresh
          wrote on last edited by
          #4

          Thanks for taking the time to detail your response Griff...!!!

          OriginalGriffO 1 Reply Last reply
          0
          • R rfresh

            Thanks for taking the time to detail your response Griff...!!!

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            You're welcome!

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups