Uses: VS 2012;
I've a combobox attached to a datasource in my form. And things work fine. When I run the form, again everything works fine; I can select an item in the dropdown list and it updates to the datasource as well. My problem comes when I need to deselect/revert what I have selected after I saved or Remove what I have select (basically should go as null for that field value).
Our legacy system was built in Delphi 3 & 5, and users got a feature of right-clicking on the dropdown list and get a small popup like button named
Blank
which blanks what have been selected. I could not find anything that will do the same what ever user have selected in .NET's combo box.
You can add a new item in dropdown named -Select-( or something similar name) by using following code:
drp.DataSource = dataSet;
drp.DataBind();
// do it after binding
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
If you are binding in xaml then on page_load event you can write only this line
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
Now if user want to deselect choice, he/she will simply select -Select- item.
Whilst thanking all of your Answers and suggestions, I used #V4Vendetta's idea and composed my solution.
Similarly to deleting a record in a datagridview where you click Delete key, I took the same concept and associated my solution with Delete Key.
What I did was created a handler for ComboBox's Keypress Event like:
private void comboBox_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
(sender as ComboBox).SelectedIndex = -1;
}
}
And linked to every ComboBox's available
ComboBox1.KeyDown += new KeyEventHandler(comboBox_KeyPress);
ComboBox2.KeyDown += new KeyEventHandler(comboBox_KeyPress);
Now when user clicks Delete key while the ComboBox selected/active, it gets blank.
Related
Using WinForms. I have a combobox with DropDownStyle DropDown. In the items I put a single item "XA". When the user enters "X" into the ComboBox (not yet dropped down) and then presses the drop down button "X" is automatically replaced with "XA". How can I stop this happening? I would like the user to be able to keep text as "X" and only change the text to "XA" if "XA" was clicked in the drop down list.
To recreate create a new WinForms application and add a comboBox then add the following code
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.Items.Add("XA");
}
Note that if the user does not press the dropdown then "X" stays in the combobox.
Note that there is a similar sounding question here but it is actually different.
How do I set a ComboBox default *not in the drop down* when a list item begins with the same text as a drop down item?
I think this solution should help you:
Winforms combobox bug - 2 items with the same value but different key
It changes if (m.Msg == LB_FINDSTRING) to m.Msg = LB_FINDSTRINGEXACT;, which should prevent the behavior you describe.
I have a simple combobox in c# forms which is populated from an array.
I have set AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems. This allows me to filter through the list quickly by typing a string into the combobox and matching items are displayed as I type along. This works great.
However, when the drop down list is open and I start typing, the filtered list appears on top of the dropdown list but I cannot select from the filtered list but only from the drop down.
How to disable drop down list while open as soon as user enters a character into the combobox.
Currently only have one method for the combobox
private void SelectJobDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
//plenty of code here
}
I have tried adding other methods for the combobox such as KeyPress or Keydown but none seems to be working for as I'm very likely doing something wrong
Using Visual Studio 2015
If I understood you correctly you don't like the overlapping list over the old drop down. Since you type letters into the ComboBox I would suggest to use the comboBox1_TextUpdate event. This nice line of code should fix your problem:
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.Simple;
Setting the ComboBox.DropDownStyle property, which
specifies whether the list is always displayed or whether the list is displayed in a drop-down[...]
to ComboBoxStyle.Simple, which
Specifies that the list is always visible and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.
will remove the original dropdown (long list) and only the filtered results remain.
I have a Combo Box that I can already add items to and I want to be able to remove the item I have selected when I hit the delete key.
Here is the code I am using now.
private void commandComboBox_KeyDown(object sender, KeyEventArgs e)
{
var myComboBox = (ComboBox)sender;
string text = myComboBox.Text;
if (e.KeyCode == Keys.Enter)
{
myComboBox.Items.Add(myComboBox.Text); // Add
}
if (e.KeyCode == Keys.Delete)
{
myComboBox.Items.Remove(myComboBox.SelectedItem);
}
}
When I click in the combobox and start typing and then hit enter I hear a windows sound (not sure which one) and then the item is added to the list.
When I hit the dropdown button I see the item there with the text I entered above. When I hit delete the item goes away (at least I think it does) and then when I click somewhere else I get this exception
System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
Also when I hit the dropdown button I still see the empty spaces
So my question is how do I properly delete items from a ComboBox :)
Also if there is something better then a ComboBox for this kind of thing plz mention them as well, TY
The Windows sound you hear is actually an error beep. Combo boxes do not accept enter key presses, so it's beeping at you "no!" Your code also runs, of course, adding the item, but that doesn't change the fact that the combo box considers you pressing Enter when it has the focus to be an error. If you are intent on the current design, you need to eat the Enter key press after you've received it so that the combo box doesn't go on to try to process it. To do so, set e.SuppressKeyPress to true.
The exception you get is because you've deleted all of the items in the combo box, but some other section of your code tries to get the text of item #0 (the first item). There is no first item because you deleted it, so an exception is thrown. I'm not sure what code it is that's responsible for this, since I can't see it, but I'm guessing you have written a handler for something like the SelectedIndexChanged event.
Indeed, this is a very unusual interface. The purpose of a combo box is to present the user with a list of choices, not to allow them to type in multiple choices. If you want that, use a multi-line text box. At least that way, they'll be able to see all the things that they've entered.
Or, you could use the classic interface idiom for this, where there is a textbox to type into that works with an Add button to add the typed text to a ListBox control. A Delete button deletes the currently selected item in the ListBox. A Clear button clears all of the items in the ListBox. Yes, it is as confusing to use as it is to explain. Avoid these whenever possible. They were more popular in the bad old days of UI design.
Also when I hit the dropdown button I still see the empty spaces
These aren't actually empty spaces. Well, they are, but not really. :-) What I mean is that they are not placeholders representing individual "empty" items. That's just what you see when the entire combo box is empty (contains no items). Because it contains no items, it can't auto-size the height of its drop-down window, so it uses a fixed size.
I had same problem with ComboBox. Noticed that error occurs only if user hits somewhere else after removing the item but not selecting a new one. Solved it by adding selection of new item after removal. Also handled last item as special case. See working code below:
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
ComboBox comboBox = sender as ComboBox;
switch (e.KeyCode)
{
case Keys.Delete:
if ((comboBox.DroppedDown) && (comboBox.SelectedItem != null))
{
if (comboBox.Items.Count == 1) // Removing Last Item
{
comboBox.DroppedDown = false;
comboBox.Text = string.Empty;
comboBox.Items.Clear();
}
else
{
comboBox.Items.Remove(comboBox.SelectedItem);
comboBox.SelectedIndex = comboBox.Items.Count - 1;
}
e.Handled = true;
}
break;
}
}
I am setting up a ComboBox to use the AutoComplete feature with AutoCompleteMode = Append and AutoCompleteSource = ListItems. When I load my form the Item list of the ComboBox is empty, and then I keep adding new values from the ComboBox.Text on a specified event (when a specific button is pressed).
Bottom line is that when the ComboBox's Items property is populated dynamically from an event, the AutoCompletion does not work as expected. My first entry into ComboBox.Items, always completes properly, but the later ones don't. If I click the arrow to dropdown the list, then all the Items so far entered autocomplete properly. If I Alt-Tab into another application and come back to the combobox, all the Items entered so far autocomplete properly.
It comes across to me that internally the combobox reloads its autocomplete list based on some event, but so far I have tried calling
ResumeLayout(true);
Refresh();
Invalidate(true);
Update();
DroppedDown = true;
DroppedDown = false;
but to no avail
Can someone enlighten me on how to dynamically add entries in the ComboBox.Items list and still have auto-complete with ComboBox.AutoCompleteSource = ListItems work correctly.
btnExecute is the default button on the form which contains the combobox. So the below function is invoked on pressing enter on the combobox
private void btnExecute_Click( object sender, EventArgs e ) {
cboCommand.SuspendLayout();
cboCommand.Items.Add(cboCommand.Text);
cboCommand.Text = "";
// Make some call here, so combobox reloads its cache for autocompletion
}
relevant portion from the auto generated winforms code
this.cboCommand.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append;
this.cboCommand.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.cboCommand.FormattingEnabled = true;
I am using .NET4 Client profile on Windows Vista. May be it plays a role here
Anyone?
I really didnt know what to put to the title so sorry about that. I have 2 colums where i list files from 2 folders. Now what i would like to do is to give a user ability to rearrange the files by clicking on one file in one column and then on the other file in other column. The application merges these two files together.
Just onother option would be just use datagrid and its drag and drop functionality (one column is static and one rearrangable with drag and drop or something) but doing it this way isnt really what i would like..
So, all options are welcome..
EDIT:
Using WinForms, it doesnt have to be gridview, just i couldnt think of anything else..
I would just create two DataGridView's (or ListView if you prefer), the first listing the files in the folder A and the second listing the files in the folder B.
Then allow to select just one row at a time in both grids (MultiSelect = false, SelectionMode = FullRowSelect) and add a button called "Merge Selected", that simply merges the file selected in the first grid with the one selected in the second grid.
Use 2 list boxes linked to each folder so that the use can scroll up/down to select the file they need
Have button "Merge Files" which gets enabled when an item from both listboxes are selected.
When user clicks on "Merge Files" have a confirmation box to make sure its not clicked my mistake.
I'll assume you're talking about DataGridView, as DataGrid has been deprecated.
In the designer, make sure SelectionMode on the DataGridView is set to CellSelect. Then in properties --> events, double-click the SelectionChanged event to create a new method which handles that event.
Add this code to the method:
private DataGridViewCell _lastCellSelected = null;
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
if(dataGridView.SelectedCells.Count == 0)
{
_lastCellSelected = null;
return;
}
DataGridViewCell selectedCell = dataGridView.SelectedCells[0];
if(_lastCellSelected == null || selectedCell.ColumnIndex == _lastCellSelected.ColumnIndex)
{
//User clicked first cell
_lastCellSelected = selectedCell;
}
else
{
//User has clicked two cells from different columns
string filename1 = _lastCellSelected.Value;
string filename2 = selectedCell.Value;
//TODO: "Merge" files here
_lastCellSelected = null;
}
}