Disable checklistbox checkbox on click but not from code behind - c#

I am creating a winforms application with a Checkedlistbox which contains some names. What i need is for the checkboxes to be disabled from being checked by clicking, but still be checkable from the code.
I tried setting the .CheckOnClick to false, but then the checkbox still checks on the second click.
I've tried the solution from the following question:
How to disable a checkbox in a checkedlistbox?
But this resulted in disabling the ability of checking from the code as well..
Disabling the entire box is not an option, this will disable all events including the selecting and doublemouseclick which are crucial in my application.
Anyone that knows a solution for this?

Ok, what you need to do is handle the ItemCheck event for your CheckedListBox, like so:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
e.NewValue = e.CurrentValue;
}
If you want to change the state of a checkbox in code, then you will have to remove the event handle temporarily:
checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
checkedListBox1.SetItemChecked(1, true);
checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
It's not elegant, but it is a possible solution.

Set the Enabled property to False:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.enabled.aspx

Related

Winform Checkbox MouseDown event handler not allowing CheckedChanged event to fire

I have a WinForm which contains a multitude of controls interdependent on each other for their visibility and content.
I have a pair of radio buttons, controlling a combobox's (ComboBoxA) enable/disable flag and content. The selection on this combobox controls the visibility of a checkbox. The checking of this checkbox controls another combobox's (ComboBoxB) visibility and content. Business requirements are quite complicated around these controls. As a result, I require the ability to fire of the events programmatically and through user action, doing different things in each case.
In the checkbox's case, I check it programmatically while loading data (if needed), which fires the CheckedChanged event which in turn does additional action controlling ComboBoxB. The code for this is pretty vanilla, nothing special, but my question is more theoretical than practical. Please keep reading.
Due to this requirement, I need a way to distinguish between programmatic checking and user action. I tried using the Click event and CheckedChanged event, setting a flag in the click event, signifying user action. Unfortunately, the CheckedChanged event fires before the Click event, dead-ending this trick.
Now, I tried using the MouseDown event to capture user action. But funnily enough, once the event fires, checkbox remains unchecked and the CheckedChanged event doesnt fire.
Now, I have managed to use a flag in the code to determine programmatic checking and use that to distinguish between the two, but I was curious as to why the MouseDown event didnt allow the checkbox to be checked. Any ideas? I searched online but either I didnt do a thorough job of it, or google is not returning the right results for me. I apologize if anybody is actually able to find a google result for this problem.
It's something else in your code, not the MouseDown event that's preventing the CheckChanged to be fired.
Here is how I know this:
I've added a checkbox and a button to an empty form, and added event handlers to Click on the button, and on the checkbox CheckedChanged, KeyDown and MouseDown events. I've also added to the form a string variable called LastEventRaised, and in the CheckedChanged I've simply shown a MessageBox:
string LastEventRaised = string.Empty;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show("Checked changed " + LastEventRaised);
LastEventRaised = string.Empty;
}
private void checkBox1_KeyDown(object sender, KeyEventArgs e)
{
LastEventRaised = "KeyDown";
}
private void checkBox1_MouseDown(object sender, MouseEventArgs e)
{
LastEventRaised = "MouseDown";
}
private void button1_Click(object sender, EventArgs e)
{
LastEventRaised = "programmatically";
checkBox1.Checked = !checkBox1.Checked;
}
Each time the message box popped up I've got the correct message.

C# autocomplete combobox trigger SelectionChangeCommited

I'm having problems with the autocomplete property of a combobox. I want to trigger the SelectionChangeCommited event every time I choose an item using the autocomplete but it's not working. The only way the event is triggered is when I use the mouse click and select an option or when the combobox is focused and I use arrow keys on the keyboard. How do I achieve this behaviour using the autocomplete property?
My combo has these properties set:
AutoCompleteMode = SuggestAppend
AutoCompleteSource = ListItems
FormattingEnabled = True
The items in my combo are set with a datasource.
Any ideas?
Thanks
If you mean that you want it to register a change when you start typing:
Call the SelectionChangeCommited event from the TextChanged event.
If you've never done this, the most basic example I could find was on the .net forums here. Granted, the methods shown there are generalities, but is very simple to understand and apply to your code.
EDIT FIXED (as of most recent comment):
Still tie the events together, but instead of using TextChanged, which would occur ever time you type, use the SelectedIndexChanged, which occurs when you use the mouse to select an auto suggested item.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1_SelectionChangeCommitted(sender, e);
}
you canuse a trick and call comboBox1_SelectionChangeCommitted
in Validated event
when ever text in combobox changes and user leave the combo box it will be fired
private void comboBox1_Validated(object sender, EventArgs e)
{
comboBox1_SelectionChangeCommitted(sender, e);
}

Disabling the onSelectedIndex changed event of WPF dropdown combo on navigating through arrrow keys

When I am having a value item selected in my WPf DropDown Combo Box then navigating using keys Left and right arrow keys result in firing of selected changed event for each item.
How to overcome this problem
The most easy and suitable way I found to overcome this problem is as follows:
rather than using SelectedIndexChanged event I used on DropDownClosed event and all code that is wriiten earlier inside selected index changed moved to this event under a if condition that checks whether a item is selected or not. Like this.
private void OnCmbOperatorsListDropDownClosed(object sender, EventArgs e)
{
if (cmbOperatorsList.SelectedIndex != -1)
InsertText(cmbOperatorsList.SelectedValue.ToString());
//Do whatever u want with selected item
}
So in this way when i navigate through Arrow keys SelectedIndexChagned event will not fired or since i haven't used that event so it will not create any problem.
As per my knowledge this is not possible straight away. I could have implemented this in a kind of "selection simulated" manner.
Handle arrow keys on combobox dropdown in PreviewKeyDown event by setting e.Handled = true. So that usual navigation based selection wont happen.
Inthese handlers based on Keys, change the Background and Foreground of the previous or next item from the drop down list so that it will look as if its selected and highlighted.
Then perform selection of the item which curently has the "simulated selection background - foreground" when dropdown closes. After dropdown closure, revert the background and foreground style.
But thats just my way of doing it.
You can use the PreviewKeyDown event like
private void combo_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Left) || (e.Key.Equals(Key.Right)))
{
((ComboBox)sender).SelectionChanged -= combo_SelectionChanged;
}
}
and if u want to attach that event you can add this PreviewMouseDown event.
This is what i tried and may not be a proper method of doing such cases

How to change the behaviour of the DataGridViewComboBoxColumn in C#?

I'm working with a data grid that contains a Combo Box Column, but editing this Combo Box (by simply clicking on it) gets annoying sometimes, since one must click at least twice to change the value of that field. I want to change that behaviour, so I thought it would be very simple: just create a OnMouseOver event to make the mouse-overed combo box be selected, but the only available event is the Disposed one.
Is there any way to change this behaviour?
I just dealt with the same problem, and solved it by setting the DataGridView.EditMode to EditOnEnter.
If you don't like that behaviour for all your other columns, I found this suggestion for placing in the CellEnter event:
if (DataGridView1.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn)
{
((DataGridViewComboBoxEditingControl)DataGridView1.EditingControl).DroppedDown = true;
}
I haven't tried it, but it looks promising. The same technique is discussed on this question.
In Winforms, there's a CellMouseEnter event (and a CellEnter event for non-mouse navigation) on the DataGridView. You can use that to set the selected cell.
The reason you are getting only the Disposed event (I think) is because you are trying to go too deep. I get only the Disposed event when I try to go all the way to dataGridView1.Columns["Column1"]...
Instead, as KeithS mentioned, you can assign the CellMouseEnter event to your DataGridView.
From there, you can do the following...
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex==0 || e.ColumnIndex==2)
{
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
dataGridView1.BeginEdit(true);
}
}
That if-statement is just there to show how to restrict this functionality to certain columns, in case you want that.
The first line inside the if-statement sets the current cell and the second line begins the edit process.
This is a general process that should work with any type of column you can throw into a DataGridView. The DataGridView's EditMode shouldn't matter.
This works really good.
On the CellClick event of the datagridview:
void datagridview1_CellClick(object sender, .Windows.Forms.DataGridViewCellEventArgs e){if (e.ColumnIndex > 0)
{
W1.dGVReports.CurrentCell = W1.dGVReports.Rows[e.RowIndex].Cells[e.ColumnIndex];
W1.dGVReports.BeginEdit(true);
(W1.dGVReports.EditingControl as System.Windows.Forms.DataGridViewComboBoxEditingControl).DroppedDown = true;
}
}

How to ignore a checkbox change in listview?

I have a listview with a the property checkbox = true.
When the user clicks on the checkbox and changes its state (checked -> unchecked or unchecked -> checked), I catch the ItemCheck event and do some DB implementation.
I want to ask the user for confirmation before working with the DB.
When I the user cancel it's command, I want that the checkbox will return to it's status.
How can I tell the listview to ignore the state change of the checkbox?
Thank,
Mattan
In the ItemCheck event, set the NewValue to the CurrentValue:
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (MessageBox.Show(this, "Change?", "Test", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
e.NewValue = e.CurrentValue;
}
Check out the OnClientClick attribute of the checkBox:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=93&AspxAutoDetectCookieSupport=1
Using this you can cancel the postback, as well as set the value of the checkbox back to what it was before. If you use a templatefield instead of the checkbox=true property, you can add the OnClientClick attribute to the checkbox there; otherwise you need to add it dynamically in the ListView ItemDataBound event.
EDIT Oops, didn't see the "winforms" tag; thought this was ASP.Net (which also has a ListView control). Kindly disregard.
I used the OnChecked event instead of OnCheck.
To cancel, I just set the value to the !value.

Categories

Resources