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.
Related
Sometimes while the user is typing text in a DataGridViewTextBox you want to enable or disable a control, depending on the value being typed. For instance enable a button after you typed a correct value
Microsoft showed the way in an article about how to create a DataGridViewButtonCell that can be disabled.
This is their trick (it can also be seen in other solutions)
Make sure you get the event DataGridView.CurrentCellDirtyStateChanged
Upon receipt of this event, commit the changes in the current cell by calling:
DataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
This commit will lead to the event DataGridView.CellValueChanged
Make sure you get notified when this event is raised
In your OnCellValueChanged function, check the validity of the changed value and decide
whether to enable or disable the corresponding control (e.g. button).
This works fine, except that the CommitEdit makes that the text is selected while in OnCellValueChanged. So if you want to type 64, you get notified when you type 6 and later when you type 4. But because the 6 is selected you don't get 64, but the 6 is replaced by 4.
Somehow the code must deselect the 6 in OnCellValueChanged before interpreting the value.
The property DataGridView.Selected doesn't do the trick, it doesn't deselect the text, but it deselects the cell.
So: how to deselect the text in the selected cell?
I think you need something that when the user is typing some text into the current cell, you need to know the current text (even before committing it) to check if some button need to be disabled. So the following approach should work for you. You don't need commit any thing, just handle the TextChanged event of the current editing control, the editing control is exposed only in the EditingControlShowing event handler, here is the code:
//The EditingControlShowing event handler for your dataGridView1
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e){
var control = e.Control as TextBox;
if(control != null &&
dataGridView1.CurrentCell.OwningColumn.Name == "Interested Column Name"){
control.TextChanged -= textChanged_Handler;
control.TextChanged += textChanged_Handler;
}
}
private void textChanged_Handler(object sender, EventArsg e){
var control = sender as Control;
if(control.Text == "interested value") {
//disable your button here
someButton.Enabled = false;
//do other stuff...
} else {
someButton.Enabled = true;
//do other stuff...
}
}
Note that the conditions I used above can be modified accordingly to your want, it's up to you.
I am developing a windows forms application and load the list from this code:
private void showList()
{
TeamTableAdapter teamAdapter = new TeamTableAdapter();
lstTeamName.DataSource = teamAdapter.GetTeamsActive();
lstTeamName.DisplayMember = "TeamName";
lstTeamName.ValueMember = "TeamID";
}
I want to enable a button if the user selects one of the items. What event should I put the code into. I the following code but the event seems to fire before the user clicks on the list.
private void lstTeamName_Click(object sender, EventArgs e)
{
if (lstTeamName.SelectedIndex > -1)
btnImportXML.Enabled = true;
}
I moved my code to the SelectedIndexChange event but it still fires before the user selects an item and the selectedIndex is 0.
You dont want to bind to the Click event but to the SelectedIndexChanged event. You should be able to accomplish this by simply double clicking on the Control in designer.
I would agree that you don't want to bind to Click as that will likely fire too early.
I recommend you look into the DropDownStyle property. http://msdn.microsoft.com/en-us/library/system.windows.forms.comboboxstyle(v=vs.110).aspx. If you set that to DropDownList then the SelectedItemChanged will fire and SelectedIndex could be > -1
If you leave it as the default DropDown then you may want to use TextChanged and check the Text property.
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
Working in Windows Forms (C#), creating a Wizard, I'd like to require the user to select an option in a combobox before being allowed to click "Next" to the next page in the form.
I thought I saw where to do this in the past, but I cannot find anything now.
Thx for any help...!
J
There are multiple ways of doing this. And different application use their preferred way.
One way to have an empty or 'Select Value' option at the top of the list of your combo box. Then when the user click the 'Next' button, check whether this is the value which is selected. If so, don't allow to go next. Otherwise allow to proceed.
My way is to set 'SelectedValue' property to -1 (means select nothing) and check whether is it -1 when the user press 'Next'. (If any valid value is selected, then this property should have a value higher than -1.)
Trigger on the selection changed event for the combo box, and then set the button enabled property:
private void comboBoxSelectionChanged(obj sender, EventArgs e)
{
nextButton.enabled = true;
}
There are many ways you can validate the selection ...or force a selection ...2 off the top of my head:
set the combobox to CausesValidation (IIRC) to true and handle xxxValidating(o,e) and xxxValidated(o,e) events
handle Next button's OnClick event and check the combobox SelectedItem or SelectedIndex properties:
/* sudo */
(o, e) => {
if(fooCombo.SelectedIndex == {...}) {
// show dialog, etc.
}
}
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