Results 1 to 13 of 13

Thread: Move selected combobox item to the text

  1. #1

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Move selected combobox item to the text

    Hi all

    The scenario is that we want to be able to select an entry from a drop-down and then that value is moved from the list (removed) to the text field section of the combobox.

    When I use the Removeat(index) method to remove the selected item it removes it no problem, but I cannot for the life of me get the text from what I selected into the textbox section of the combobox.

    Any help/advice would be much appreciated, thanks

    VB
    Life is one big rock tune

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Move selected combobox item to the text

    Maybe I'm missing something here, but if you select an item in the usual combobox, the dropdown will close, and the text of the selected item will appear in the textbox area.

    Is it not doing that? Is it also not doing that if you don't remove the item?

    EDIT
    Do you want to remove the item from the list AND display it in the text area? So when you click item "ABC", the text area will display "ABC", but if you next use the dropdown, then item "ABC" will no longer exist? In that case it may get a little trickier. When removing the item it is also no longer shown in the text area. You could do something like 'suspending' the deletion of the item until the next time the user drops down the combobox, but I am pretty sure there is a better way. You could also get into trouble using that if you do things like looping through the items, in which case the "ABC" item would still exist while you already 'removed' it.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Move selected combobox item to the text

    You just set the Text property. Of course, you have to have set the DropDownStyle of the control to DropDown. If it's set to DropDownList then it's not possible to have a value in the box that is not in the list.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: Move selected combobox item to the text

    jmc's right (sorry, was tinkering with code and didn't post sooner). Here's what I was able to come up with for you.

    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                this.comboBox1.Items.AddRange(new string[]{"Value1", "Value2", "Value3", "Value4", "Value5"});
            }
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                MoveItem(this.comboBox1, this.comboBox2);
            }
            private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                MoveItem(this.comboBox2, this.comboBox1);
            }
            private static void MoveItem(ComboBox From, ComboBox @To)
            {
                To.Text = (string)From.SelectedItem;
                To.Items.Add(From.SelectedItem);
                From.Items.Remove(From.SelectedItem);
            }

  5. #5

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Move selected combobox item to the text

    Thanks for response guys
    jmcilhinney: Have you tried this first? You'll notice that if you remove the item from the dropdown list then change the text property, it appears briefly and then disappears.

    syntaxeater: I'm not moving it between two comboboxes but in itself. I essentially want to select one of the items. It removes that item from the list and puts the item's text into the combobox text property
    Life is one big rock tune

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Move selected combobox item to the text

    Hmmm... I see. It's always a good idea to let us know what you've done so we know what not to suggest.

    Before looking for a solution to this issue I would like to point out a serious flaw in your design. Even if you can get this to work, what happens if someone uses the arrow keys to move down the list? They would end up removing every item in the list. The user should be able to navigate from the first item to the last using the keyboard if they want, which would be a disaster with your design.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Move selected combobox item to the text

    Yes it does do that but we want to remove the item also
    Life is one big rock tune

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Move selected combobox item to the text

    After 760 posts you know you're not supposed to post the same question twice.

    http://www.vbforums.com/showthread.php?t=577424
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Move selected combobox item to the text

    True.
    We were hoping to use the "delete" key, similar to how you can delete your browsing history in Firefox so that would be the ideal solution but we couldn't get it to work either
    Life is one big rock tune

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Move selected combobox item to the text

    It can be done with the Delete key but not with the Delete key on its own. That's because the Delete key obviously has another purpose that you can't really stop. So does Ctrl+Delete and so does Shift+Delete. Alt+Delete, on the other hand, has no effect on the control by default so you can use that:
    csharp Code:
    1. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    2. {
    3.     if (e.KeyData == (Keys.Alt | Keys.Delete))
    4.     {
    5.         string text = this.comboBox1.GetItemText(this.comboBox1.SelectedItem);
    6.  
    7.         this.comboBox1.Items.Remove(this.comboBox1.SelectedItem);
    8.         this.comboBox1.Text = text;
    9.     }
    10. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Move selected combobox item to the text

    Legend as always dude!
    Cheers mate
    Life is one big rock tune

  12. #12

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Move selected combobox item to the text

    lol, I know. Sorry dude but hopefully someone who looks only in the VB.NET forum will find the solution they need thanks to you mate
    Life is one big rock tune

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Move selected combobox item to the text

    Duplicate threads merged - please post each question (or variation of it) only once.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width