KeyDown Delete from ComboBox - c#

I have a combo box and want to add a key down function so that when Delete pressed then it will delete the Item in the combo box and sends a null value to the database:
private void comboBox_KeyDown(object sender, KeyEventArgs e)
{
ComboBox cmbx = (ComboBox)sender;
if (e.KeyCode == Keys.Delete)
{
cmbx.SelectedIndex = -1;
cmbx.SelectedValue = DBNull.Value;
}
}
But it's not working properly. Any suggestions

Your code doesn't really make any sense.
cmbx.SelectedIndex = -1; removes the selection that from the combo box, it doesn't remove the selected item.
Here's a few different methods of removing specific elements of your combobox.
// To remove item with index 0:
cmbx.Items.RemoveAt(0);
// To remove currently selected item:
cmbx.Items.Remove(cmbx.SelectedItem);
// To remove "Value1" item:
cmbx.Items.Remove("Value1");
Reference: http://msdn.microsoft.com/en-us/library/19fc31ss.aspx

Related

Key Up/Down Selection of Autocomplete list in Textbox C#

In C# Winforms, I have a textbox with AutoCompleteMode. When the user types some letters the suggestion list populates Correctly. However, if an item in list is selected using (Keyboard) UP and Down key it could not navigate through the list of items. it just picks up the first item shown in the list.
On the other hand selection using mouse click works fine. here is my code
private void txtQryName_TextChanged(object sender, EventArgs e)
{
List<string> fullName = _customerBll.NameSuggestor(txtQryName.Text);
AutoCompleteStringCollection source = new AutoCompleteStringCollection();
source.AddRange(fullName.ToArray());
txtQryName.AutoCompleteMode = AutoCompleteMode.Suggest;
txtQryName.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtQryName.AutoCompleteCustomSource = source;
}
The problem with having a key based selection is that it is very sensitive to the number of items it the stack contains. A possible solution could to be to set that KEY_UP and KEY_DOWN only increase/decreases or selects a specific type within the list.
Additionally I believe you can set the textbox to have a specific responds when you press it, you should consider that if for example the user has not choose an item within ( BOOLEAN: false or true) than no items are selected
An example
private void textBox1_KeyPress
(object sender,System.Windows.Forms.KeyPressEventArgs e)
{
// Check for the flag being set in the KeyDown event.
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown(v=vs.110).aspx
Here is some useful stuff for you if you want to expand upon this idea. Hope my answer helps

C# Set ListView item selected after inserting a new Item in winform

I've the following code that Clones/makes a copy the selected ListView item, removes the Selected item, and then re-inserts the copied Item in a new position in the ListView.
private void btnUp_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 1)
{
int iIndex = listView1.FocusedItem.Index;
if (iIndex > 0)
{
ListViewItem oListViewItem = (ListViewItem)listView1.FocusedItem.Clone();
listView1.Items.Remove(listView1.FocusedItem);
listView1.Items.Insert(iIndex - 1, oListViewItem);
}
}
}
The code works fine and the item is moved and the list updated. However, I'm wanting the newly inserted Item to remain selected. I tried
listView1.Items[iIndex - 1].Selected = true;
but this doesn't have the desired affect.
What else can I try ?
If you add the Selected = true to the newly inserted item index then your code should work as expected. But when you click on the button, the focus goes to the pressed button and under the default properties the ListView.HideSelection is set to True. So you don't see any item selected. If you press TAB on your form until the ListView is again the focused control, your ListViewItem should be showed as selected.
If you want to show some form of (dimmed) selection even when the control is not focused then set
listView1.HideSelection = false;
However, if I understand what you are trying to do (moveup an item) then you should change your code to use the SelectedItems[0] element instead of the FocusedItem
if (listView1.SelectedItems.Count == 1)
{
int iIndex = listView1.SelectedItems[0].Index;
if (iIndex > 0)
{
ListViewItem oListViewItem = (ListViewItem)listView1.SelectedItems[0].Clone();
listView1.SelectedItems[0].Remove();
listView1.Items.Insert(iIndex -1, oListViewItem);
listView1.Items[iIndex -1].Selected = true;
}
}
You might want to try using the IndexOf method to get the index of the inserted item.
listView.Items[listView.Items.IndexOf(oListViewItem)].Selected = true;
Hope this helps.

datagrid selected index gives troubles

I have a datagrid that I fill with data from a database.
When I click on a row, I call the GotFocus method and try to make a button visible if certain requirements are met.
private void dtgVerkoopsdocumenten_GotFocus(object sender, RoutedEventArgs e)
{
DataItem row = (DataItem)dtgVerkoopsdocumenten.SelectedItems[0];
if (row.soort2 == "Factuur")
{
btnBoeking.IsHitTestVisible = true;
btnBoeking.Opacity = 1;
}
else
{
btnBoeking.IsHitTestVisible = false;
btnBoeking.Opacity = 0.5;
}
}
This gives me an error.
Index was out of range. Must be non-negative and less than the size of the collection.
Now when I call the code but from a button click it does it how it's supposed to work.
private void tester_Click(object sender, RoutedEventArgs e)
{
DataItem row = (DataItem)dtgVerkoopsdocumenten.SelectedItems[0];
test.Content = row.soort2;
if (row.soort2 == "Factuur")
{
btnBoeking.IsHitTestVisible = true;
btnBoeking.Opacity = 1;
}
else
{
btnBoeking.IsHitTestVisible = false;
btnBoeking.Opacity = 0.5;
}
}
Why is this?
Why dont you use DataGrid SelectedIndexChanged event?
Wyy use GotFocus that doesnt tell you if user even made a selection to start with,
DataItem row = (DataItem)dtgVerkoopsdocumenten.SelectedItems[0];
Called from gotfocus will fail as you have nothing selected besides having no error check in place to check if selection,
If you use Selection changed events you know the user has made selection changes there are a number of events for selection
before access selected items by index you need to check selected item count is grater than zero condition.
Because dtgVerkoopsdocumenten.SelectedItems are empty and GotFocus event raise before SelectedItemChanged event so we are not sure the dtgVerkoopsdocumenten.SelectedItems have any item or not.
You can check dtgVerkoopsdocumenten.SelectedItems before do anything.
if (dtgVerkoopsdocumenten.SelectedItems != null &&
dtgVerkoopsdocumenten.SelectedItems.Count > 0)
{
DataItem row = (DataItem)dtgVerkoopsdocumenten.SelectedItems[0];
...
}

How to remove ComboBox Item from Collection by pressing Del Key?

I'm trying to remove selected item from ComboBox Collection:
I wrote a buttonClick:
cb01.Items.Remove(cb01.SelectedItem);.
This deletes the item, but next time I open the Form - the item appears again.
Please help.
Add KeyDown event for your ComboBox and then
private void cb01_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
if(cb01.SelectedIndex != -1)
cb01.Items.Remove(cb01.SelectedItem);
}
}
Above will remove items from comboBox but if you add items on designed time when you load the application again you can see all the items again.
check your InitializeComponent() method. you can see something like below.
this.cb01.Items.AddRange(new object[] {
"item1",
"item2",
"item13"});
when you load the application again, it will call InitializeComponent and call above method to add items.
To avoid this issue. You can use bound data source. e.g you can take items from database. and when you delete you can delete it from database. next time you load the application it only show the items in the database.
How about
if(cb01.SelectedItem != null)
cb01.Items.Remove(cb01.SelectedItem);
Why i did checking?
Since in last line you said
cb01.Items.RemoveAt(cb01.SelectedIndex); // error: Value of '-1' is not valid...
-1 is the index of combo when no item is selected. So i checked for selected item first. If found will go in if statement.
Replace comboBox1 with the name of your combobox and bind its KeyDown event
void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
int currentItem = comboBox1.SelectedIndex;
if (e.KeyCode == Keys.Delete && currentItem != -1)
{
comboBox1.Items.RemoveAt(currentItem);
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = (currentItem > 0 ? currentItem - 1 : currentItem);
}
}
This will select the next item in list after removing it, or do nothing if there are either no items in the comboBox or no item is selected.

Select WinForm ListView Item, Press Delete: Trigger Code

Let's say I have a list item populated with a few items, I select one and press delete.
I want something to happen when delete is pressed (and I'd like to know which item or items are selected). If this is possible, I'd like to know how to do this.
Thanks!
Set up your ListView to have an event handler for the KeyDown event. Then check that the key that was pressed was the delete key. Then use SelectedItems to see which items are selected and remove them. Make sure to go from the bottom up because your SelectedItems collection will be constantly changing.
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Delete)
{
for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem li = listView1.SelectedItems[i];
listView1.Items.Remove(li);
}
}
}

Categories

Resources