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. Updating/changing Items in a ListBox with a TextBox

Updating/changing Items in a ListBox with a TextBox

Scheduled Pinned Locked Moved C#
helpquestionannouncement
3 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.
  • S Offline
    S Offline
    stephan smolek
    wrote on last edited by
    #1

    Hello, I currently have the problem that I want to update the contents (a string) of an item of a listbox with a textbox. Best would be on-the-fly so I tried listBox1.Items[listBox1.SelectedIndex] = textBox1.Text in TextChanged. However I also want to be able to select which item to update by selecting the item in that very listbox. So I do something like textBox1.Text = listBox1.Items[listBox1.SelectedIndex] in SelectedIndexChanged of the listbox. (I say "something like" because the information includes other numbers that are also stored in a list along with the text.) The problem with that is that selecting an item changes the text in the textbox and that again updates the item which implicitly calls an SelectedIndexChanged again (or so it seems). And during that updating of the listbox it happens that the selectedindex changes to -1. Which is a problem if you try to read that item... I was wondering if there is an easier/better/cleaner way to accomplish what I try? :confused: Stephan

    B 1 Reply Last reply
    0
    • S stephan smolek

      Hello, I currently have the problem that I want to update the contents (a string) of an item of a listbox with a textbox. Best would be on-the-fly so I tried listBox1.Items[listBox1.SelectedIndex] = textBox1.Text in TextChanged. However I also want to be able to select which item to update by selecting the item in that very listbox. So I do something like textBox1.Text = listBox1.Items[listBox1.SelectedIndex] in SelectedIndexChanged of the listbox. (I say "something like" because the information includes other numbers that are also stored in a list along with the text.) The problem with that is that selecting an item changes the text in the textbox and that again updates the item which implicitly calls an SelectedIndexChanged again (or so it seems). And during that updating of the listbox it happens that the selectedindex changes to -1. Which is a problem if you try to read that item... I was wondering if there is an easier/better/cleaner way to accomplish what I try? :confused: Stephan

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

      Simple fix, just add if (listBox1.SelectedIndex > -1) so that the conditional is evaluated before making any updates. This should resolve your issue and update the reruired data. For example: private void textBox1_TextChanged(object sender, EventArgs e) {     if (listBox1.SelectedIndex > -1)     {         listBox1.Items[listBox1.SelectedIndex] = textBox1.Text;     } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {     if (listBox1.SelectedIndex > -1)     {         textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();     } } -Arcond

      S 1 Reply Last reply
      0
      • B bit_cmdr

        Simple fix, just add if (listBox1.SelectedIndex > -1) so that the conditional is evaluated before making any updates. This should resolve your issue and update the reruired data. For example: private void textBox1_TextChanged(object sender, EventArgs e) {     if (listBox1.SelectedIndex > -1)     {         listBox1.Items[listBox1.SelectedIndex] = textBox1.Text;     } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {     if (listBox1.SelectedIndex > -1)     {         textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();     } } -Arcond

        S Offline
        S Offline
        stephan smolek
        wrote on last edited by
        #3

        Thanks for your answer, I found another solution myself today.:) Stephan

        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