|
-
Jul 22nd, 2009, 10:53 AM
#1
Thread Starter
Fanatic Member
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 
-
Jul 22nd, 2009, 11:52 AM
#2
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.
Last edited by NickThissen; Jul 22nd, 2009 at 12:12 PM.
-
Jul 22nd, 2009, 07:56 PM
#3
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.
-
Jul 22nd, 2009, 08:10 PM
#4
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);
}
-
Jul 23rd, 2009, 02:39 AM
#5
Thread Starter
Fanatic Member
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 
-
Jul 23rd, 2009, 02:50 AM
#6
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.
-
Jul 23rd, 2009, 02:52 AM
#7
Thread Starter
Fanatic Member
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 
-
Jul 23rd, 2009, 03:04 AM
#8
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
-
Jul 23rd, 2009, 03:05 AM
#9
Thread Starter
Fanatic Member
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 
-
Jul 23rd, 2009, 03:16 AM
#10
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:
private void comboBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == (Keys.Alt | Keys.Delete)) { string text = this.comboBox1.GetItemText(this.comboBox1.SelectedItem); this.comboBox1.Items.Remove(this.comboBox1.SelectedItem); this.comboBox1.Text = text; } }
-
Jul 23rd, 2009, 03:31 AM
#11
Thread Starter
Fanatic Member
Re: Move selected combobox item to the text
Legend as always dude!
Cheers mate
 Life is one big rock tune 
-
Jul 23rd, 2009, 03:32 AM
#12
Thread Starter
Fanatic Member
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 
-
Jul 23rd, 2009, 04:36 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|