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. index += 1 equals index + 2 ? [modified]

index += 1 equals index + 2 ? [modified]

Scheduled Pinned Locked Moved C#
databasehelpquestion
4 Posts 2 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.
  • L Offline
    L Offline
    Lodeclaw
    wrote on last edited by
    #1

    private void ComboBox_KeyDown(object sender, KeyEventArgs e)
    {
    this.ComboBox.DroppedDown = true;
    if (e.KeyCode == Keys.Up && this.ComboBox.SelectedIndex != 0)
    {
    this.ComboBox.SelectedIndex -= 1;
    return;
    }
    if (e.KeyCode == Keys.Down && this.ComboBox.SelectedIndex != this.ComboBox.Items.Count - 1)
    {
    this.ComboBox.SelectedIndex += 1;
    return;
    }
    this.ComboBox.SelectedItem = this.ComboBox.Text + "%";
    }

    Hello folks. I have this event set up to search/skip to the contents of a ComboBox by the text entered, however I want to make sure I can still use the up and down keys functionality... My problem is when I run this code in my project and press the up or down keys in the ComboBox my selection moves up or down two spaces rather than one. What am I missing? Thanks in advance! EDIT: Sorry, the code actually was slightly wrong due to my experimenting before posting. It's fixed how I meant it to be and the problem persists. The old code was as follows and has the same outcome:

    private void ComboBox_KeyDown(object sender, KeyEventArgs e)
    {
    this.ComboBox.DroppedDown = true;
    if (e.KeyCode == Keys.Up && this.ComboBox.SelectedIndex != 0)
    {
    this.ComboBox.SelectedIndex = this.ComboBox.SelectedIndex - 1;
    return;
    }
    if (e.KeyCode == Keys.Down && this.ComboBox.SelectedIndex != this.ComboBox.Items.Count - 1)
    {
    this.ComboBox.SelectedIndex = this.ComboBox.SelectedIndex + 1;
    return;
    }
    this.ComboBox.SelectedItem = this.ComboBox.Text + "%";
    }

    modified on Wednesday, February 25, 2009 6:10 PM

    L 1 Reply Last reply
    0
    • L Lodeclaw

      private void ComboBox_KeyDown(object sender, KeyEventArgs e)
      {
      this.ComboBox.DroppedDown = true;
      if (e.KeyCode == Keys.Up && this.ComboBox.SelectedIndex != 0)
      {
      this.ComboBox.SelectedIndex -= 1;
      return;
      }
      if (e.KeyCode == Keys.Down && this.ComboBox.SelectedIndex != this.ComboBox.Items.Count - 1)
      {
      this.ComboBox.SelectedIndex += 1;
      return;
      }
      this.ComboBox.SelectedItem = this.ComboBox.Text + "%";
      }

      Hello folks. I have this event set up to search/skip to the contents of a ComboBox by the text entered, however I want to make sure I can still use the up and down keys functionality... My problem is when I run this code in my project and press the up or down keys in the ComboBox my selection moves up or down two spaces rather than one. What am I missing? Thanks in advance! EDIT: Sorry, the code actually was slightly wrong due to my experimenting before posting. It's fixed how I meant it to be and the problem persists. The old code was as follows and has the same outcome:

      private void ComboBox_KeyDown(object sender, KeyEventArgs e)
      {
      this.ComboBox.DroppedDown = true;
      if (e.KeyCode == Keys.Up && this.ComboBox.SelectedIndex != 0)
      {
      this.ComboBox.SelectedIndex = this.ComboBox.SelectedIndex - 1;
      return;
      }
      if (e.KeyCode == Keys.Down && this.ComboBox.SelectedIndex != this.ComboBox.Items.Count - 1)
      {
      this.ComboBox.SelectedIndex = this.ComboBox.SelectedIndex + 1;
      return;
      }
      this.ComboBox.SelectedItem = this.ComboBox.Text + "%";
      }

      modified on Wednesday, February 25, 2009 6:10 PM

      L Offline
      L Offline
      Lodeclaw
      wrote on last edited by
      #2

      Well now I feel stupid. But I guess it's the learning process. Turns out the built-in up/down key functionality was still running, so I essentially was repeating the process with redundant code. Removing the redundant code and leaving the return function to prevent the pseudo-search function on up/down works fine. Thanks to anyone who read and considered this. EDIT: Hahaha! Now I think I just realized this entire process was useless since the up/down keys don't act any differently without the extra code. I need more sleep, maybe? Someone put me out of my misery! :laugh:

      C 1 Reply Last reply
      0
      • L Lodeclaw

        Well now I feel stupid. But I guess it's the learning process. Turns out the built-in up/down key functionality was still running, so I essentially was repeating the process with redundant code. Removing the redundant code and leaving the return function to prevent the pseudo-search function on up/down works fine. Thanks to anyone who read and considered this. EDIT: Hahaha! Now I think I just realized this entire process was useless since the up/down keys don't act any differently without the extra code. I need more sleep, maybe? Someone put me out of my misery! :laugh:

        C Offline
        C Offline
        Calin Tatar
        wrote on last edited by
        #3

        ps. index += 1 equals index = index + 1 :)

        L 1 Reply Last reply
        0
        • C Calin Tatar

          ps. index += 1 equals index = index + 1 :)

          L Offline
          L Offline
          Lodeclaw
          wrote on last edited by
          #4

          Yeah, I know, which is why I was so confused by it. :(

          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