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. Chnging a combo box entry

Chnging a combo box entry

Scheduled Pinned Locked Moved C#
help
8 Posts 5 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.
  • G Offline
    G Offline
    gwithey
    wrote on last edited by
    #1

    I have been trying to change an entry in a combo box for a while now and have got the text to change but when i click out of do something else it hasnt actually changed the entries value in the list.

           case (char)Keys.Enter:
               comboBox1.Text.Remove(0, comboBox1.Text.Length);
               imageViewer1.RenameShape("test");
               comboBox1.SelectedText = "test";
               comboBox1.Refresh();
               break;
    

    Above is my current attempt which changes the text but does not store this change. So my shape renames but the shapes name in the combo box does not. Cant help feeling i need to do more than :

    comboBox1.SelectedText = "test";

    thanx in advance

    C T L M 4 Replies Last reply
    0
    • G gwithey

      I have been trying to change an entry in a combo box for a while now and have got the text to change but when i click out of do something else it hasnt actually changed the entries value in the list.

             case (char)Keys.Enter:
                 comboBox1.Text.Remove(0, comboBox1.Text.Length);
                 imageViewer1.RenameShape("test");
                 comboBox1.SelectedText = "test";
                 comboBox1.Refresh();
                 break;
      

      Above is my current attempt which changes the text but does not store this change. So my shape renames but the shapes name in the combo box does not. Cant help feeling i need to do more than :

      comboBox1.SelectedText = "test";

      thanx in advance

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Have you tried setting the selected index ?

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      G 1 Reply Last reply
      0
      • G gwithey

        I have been trying to change an entry in a combo box for a while now and have got the text to change but when i click out of do something else it hasnt actually changed the entries value in the list.

               case (char)Keys.Enter:
                   comboBox1.Text.Remove(0, comboBox1.Text.Length);
                   imageViewer1.RenameShape("test");
                   comboBox1.SelectedText = "test";
                   comboBox1.Refresh();
                   break;
        

        Above is my current attempt which changes the text but does not store this change. So my shape renames but the shapes name in the combo box does not. Cant help feeling i need to do more than :

        comboBox1.SelectedText = "test";

        thanx in advance

        T Offline
        T Offline
        The Man from U N C L E
        wrote on last edited by
        #3

        Set the Text property, not the SelectedText. SelectedText is the string within the displayed text that is currently highlighted for cut/copy/paste actions. Setting the Text property will automatically select the correct item in the combo box if it is present. There is also no need to call Refresh.

        If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

        G 1 Reply Last reply
        0
        • T The Man from U N C L E

          Set the Text property, not the SelectedText. SelectedText is the string within the displayed text that is currently highlighted for cut/copy/paste actions. Setting the Text property will automatically select the correct item in the combo box if it is present. There is also no need to call Refresh.

          If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

          G Offline
          G Offline
          gwithey
          wrote on last edited by
          #4

          Thanx have tried this and the text does change but when i drop down the list test is not there.

          case (char)Keys.Enter:
          comboBox1.Text.Remove(0, comboBox1.Text.Length);
          imageViewer1.RenameShape("test");
          comboBox1.Text = "test";
          break;

          It just displays shape1 , shape2 .........

          1 Reply Last reply
          0
          • C Christian Graus

            Have you tried setting the selected index ?

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            G Offline
            G Offline
            gwithey
            wrote on last edited by
            #5

            I have tried this but cannot find a way of getting it to work for me : I dont know how to set the selected index's text property

            1 Reply Last reply
            0
            • G gwithey

              I have been trying to change an entry in a combo box for a while now and have got the text to change but when i click out of do something else it hasnt actually changed the entries value in the list.

                     case (char)Keys.Enter:
                         comboBox1.Text.Remove(0, comboBox1.Text.Length);
                         imageViewer1.RenameShape("test");
                         comboBox1.SelectedText = "test";
                         comboBox1.Refresh();
                         break;
              

              Above is my current attempt which changes the text but does not store this change. So my shape renames but the shapes name in the combo box does not. Cant help feeling i need to do more than :

              comboBox1.SelectedText = "test";

              thanx in advance

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Use this:

              ComboBox1.Items.Add("test");
              ComboBox1.SelectedIndex = ComboBox1.Items.Count-1;

              1 Reply Last reply
              0
              • G gwithey

                I have been trying to change an entry in a combo box for a while now and have got the text to change but when i click out of do something else it hasnt actually changed the entries value in the list.

                       case (char)Keys.Enter:
                           comboBox1.Text.Remove(0, comboBox1.Text.Length);
                           imageViewer1.RenameShape("test");
                           comboBox1.SelectedText = "test";
                           comboBox1.Refresh();
                           break;
                

                Above is my current attempt which changes the text but does not store this change. So my shape renames but the shapes name in the combo box does not. Cant help feeling i need to do more than :

                comboBox1.SelectedText = "test";

                thanx in advance

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #7

                I think your problem is that you want to change the value of an entry by typing over it. The problem being that once you start typing you loose any reference to the item you want to change. Perhaps an option would be to handle to OnEnter event and store the selected index, then when you detect the 'Enter' key you can use that index to change the value of the correct item. something like...

                int currentIndex = -1;
                void OnEnter(object sender, EventArgs e)
                {
                currentIndex = comboBox1.SelectedIndex;
                }

                void KeyDown(object sender, KeyDownEventArgs e)//or whatever the correct event args is
                {
                //some code
                case (char)Keys.Enter:
                imageViewer1.RenameShape("test");
                comboBox1.Items[currentIndex] = comboBox1.Text;
                comboBox1.Text = "";
                break;
                }

                Life goes very fast. Tomorrow, today is already yesterday.

                G 1 Reply Last reply
                0
                • M musefan

                  I think your problem is that you want to change the value of an entry by typing over it. The problem being that once you start typing you loose any reference to the item you want to change. Perhaps an option would be to handle to OnEnter event and store the selected index, then when you detect the 'Enter' key you can use that index to change the value of the correct item. something like...

                  int currentIndex = -1;
                  void OnEnter(object sender, EventArgs e)
                  {
                  currentIndex = comboBox1.SelectedIndex;
                  }

                  void KeyDown(object sender, KeyDownEventArgs e)//or whatever the correct event args is
                  {
                  //some code
                  case (char)Keys.Enter:
                  imageViewer1.RenameShape("test");
                  comboBox1.Items[currentIndex] = comboBox1.Text;
                  comboBox1.Text = "";
                  break;
                  }

                  Life goes very fast. Tomorrow, today is already yesterday.

                  G Offline
                  G Offline
                  gwithey
                  wrote on last edited by
                  #8

                  Thank you for all of the replies. I got everything working now there was alot to it as the shapes can be selected by the mouse too so this also had to change the comboBox :

                  void imageViewer1_Clicked(object sender, EventArgs e)
                  {
                  // When shape picked select appropriate entry in the combo box
                  if (imageViewer1.ListOfShapes.Count > 0)
                  {
                  for (int i = 0; i < comboBox1.Items.Count; i++)
                  {
                  if ((string)comboBox1.Items[i] == imageViewer1.ListOfShapes[imageViewer1.SelectedShapeIndex].Name) // imageViewer1.ListOfShapes[imageViewer1.SelectedShapeName].Name)
                  {
                  comboBox1.SelectedIndex = i;
                  m_comboBoxCurrentIndex = i;
                  }
                  }
                  }
                  }

                  Above and below (solution) may not be pretty but it works

                  case (char)Keys.Enter:
                  if (comboBox1.SelectedIndex != -1)
                  {
                  // Set current selected index and clear text
                  m_comboBoxCurrentIndex = comboBox1.SelectedIndex;
                  comboBox1.Text.Remove(0, comboBox1.Text.Length);

                                // Rename shape in image viewer
                                imageViewer1.RenameShape("test");
                  
                                // Set text for selected item
                                comboBox1.Items\[m\_comboBoxCurrentIndex\] = "test";
                                comboBox1.Text = "";
                             }
                  

                  Thanx George

                  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