How to Cancel Devexpress Winforms GridControl CellValueChanged event? - winforms

private void MyGrid_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
//IS_CHECK is a checkbox
if (e.Column.FieldName == "IS_CHECK")
{
if (XMsgBx.ShowInfoYesNo("Asking a Approval") == System.Windows.Forms.DialogResult.Yes)
{
//if a user clicks ok then it's ok
}
else
{
//I want to do something like e.Cancel
//Want to Cancel the input data from the user
}
}
}
I searched that the CellValueChanging() and the CellValueChanged() event in Devexpress GridControl doesn't allow a programmer to unable user typing when the events are called. I found that using ShowingEditor, ValidatingEditor, RepositoryItem, dosen't fit to my faced problem. Is there a way to Cancel the input data from user in any ways uppon the given code? please help..
2022-02-04 edited below
To clarify my question, I have a checkbox name called "IS_CHECK". When I first click, I want to show the messagebox to ask a User whether to save a something menu or not. If the User clicks 'No' then the checkbox shouldn't be checked. If the User clicks 'Yes' then the something menu should be saved. I have already known that the e.Cancel thingy doesn't exists.

You can handle the grid view's ShowingEditor event. This event occurs when a cell's editor is about to open, and allows you to cancel this action.
private void gridView1_ShowingEditor(object sender, CancelEventArgs e) {
if(gridView1.FocusedColumn.FieldName == "YOUR_FIELD_NAME")
if(MessageBox.Show("Do you want to change the value?", "Warning", MessageBoxButtons.YesNo) == DialogResult.No)
e.Cancel = true; }

Related

Closing a Windows Form, using the Close Window button, when a Validation Message is showing

I have a Windows form that has a validation event on a textBox so that if the value of that TextBox is a value that already exists it triggers a validation error.
private void txtUsername_Validating(object sender, CancelEventArgs e)
{
var alreadyExists = _logic.UserIdExists(txtUsername.Text.Trim());
if(alreadyExists)
{
errorProvider1.SetError(txtUsername, "This Userid already exists, please choose an alternative");
e.Cancel = true;
}
}
private void txtUsername_Validated(object sender, EventArgs e)
{
errorProvider1.SetError(txtUsername, "");
}
this.txtUsername.Validating += new System.ComponentModel.CancelEventHandler(this.txtUsername_Validating);
this.txtUsername.Validated += new System.EventHandler(this.txtUsername_Validated);
This results in an error image appearing next to that textBox along with a tooltip error message.
If I try and close the application, using the Close button at the top of the window, at this time I cannot as the above Event keeps firing even when I try and close the window (due to me taking focus away from the Text box).
Is there a way of closing the window, without resorting to creating an additional Close button on the form?
Based on your description, you want to maintain the default auto-validation behavior yet allow the Form to be closed using the title bar close button. I have observed that the Form.Closing event is raised in such a circumstance, however its argument Cancel property is preset to true. A simple solution is to handle this event and set e.Cancel = false. Implement any logic in the handler that you deem necessary.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing) e.Cancel = false;
}

closing a win form when datagridview has validation errors

I'm using typed dataset in winforms app, .net 3.5 (VS 2010). A form has DataGridView. In FormClosing event I ask user to save changes. If user doesn't want to save I'd like to let the from close. However, when DataGridView has validation errors (I validate dataset in datatables's ColumnChanging event) the form won't close. Even if I do not catch FormCLosing event the form refuses to close. I guess I have to clear validation errors in datagridvIew somehow. Can someone suggest a solution?
Edit: One more detail: the form is mdi child form. Needless to say, mdi parent won't close also.
You may override validation and (force close) close the form the by setting false to FormClosingEventArgs.Cancel property of Closing handler argument.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult res = MessageBox.Show("Close it?", "Close", MessageBoxButtons.YesNo);
if (res == DialogResult.No)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}
Ok, it was my mistake. mdi parent had some handlers for mdi child events, but when the child form closed not all handlers were removed.

How to give an initial value to textBox?

I want my C# program to have initial values for its textboxes. For example, in one of the textboxes, it should say "Please enter your name".
When you click (or tabStop) on the textbox, the initial value should disappear and the user will be able to enter their input to the textbox.
I can do all this with click_event, but using this method the initial text would not have less opacity. How am I able to achieve this?
This is how I finally did it:
Boolean first_time_click = true;
private void Form1_Load(object sender, EventArgs e)
{
textBox1.ForeColor = System.Drawing.Color.Gray;
textBox1.Text = "Enter the Text";
}
private void For_First_Click()
{
if (first_time_click)
{
textBox1.Clear();
textBox1.ForeColor = textBox1.ForeColor = SystemColors.WindowText;
}
first_time_click = false;
}
private void textBox1_Click(object sender, EventArgs e)
{
For_First_Click();
}
I assume you are talking about winform (tabstop) you have to handle it within the event key-press. you can use the below code:
TextBox1.Select(0, TextBox1.Text.Length);
this will select the text and window will remove it for you as soon as the user start to typing
you can use the same code to have this behavior also for TabStop
All you need to do is set the Textbox's .Text property and use GotFocus event to clear the box when the person clicks (or tabs) into it to start typing.
Always remember that there are more ways than the mouse to navigate a form, so use the GotFocus event to determine when the user enters a control, and use the Validated event to determine when they've changed data and exited the control.
For this type of effect you need java script.Because java script provide you functionality of mouse hover and mouse out these are the functions which provide you the same functionality which u seeing in this page of search bar. If you need code reply me i can give you.

canceling remove action - NotifyCollectionChangedAction

I am using the following code in my viewmodel to delete items out of a collection:
UnitMeasureCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(ListOfUnitMeasureCollectionChanged);
void ListOfUnitMeasureCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
{
if (NavigationActions.DeleteConfirmation("Delete Item.", "Are you sure you want to delete this item? This action cannot be undone."))
{
foreach (UnitMeasureBO item in e.OldItems)
{
UnitMeasureBO unitMeasureBO = item as UnitMeasureBO;
bool inUse = unitMeasureRepository.UnitMeasureInUse(unitMeasureBO.UnitMeasureValue);
if (inUse == true)
{
NavigationActions.ShowError("Cannot delete item", "This item cannot be deleted because it is used elsewhere in the application.");
}
else
{
unitMeasureRepository.DeleteUnitMeasure(unitMeasureBO.UnitMeasureValue);
}
}
}
}
}
I have a datagrid that is bound to the collection. I am wondering if there is anyway of canceling the remove action based on the confirmation prompt? I noticed NotifyCollectionChangedEventArgs does not have a cancel method. What happens is when a user deletes an item out of the datagrid but chooses 'no' on the confirmation, the item is still removed from the datagrid. It isn't deleted from the database and if the datagrid is refreshed it will appear again. I am using the mvvm pattern and I prefer to do this without having to code my datagrid. Any help is appreciated.
Well, you can't cancel a remove action during a CollectionChanged event.
My suggestion: if you're using MVVM, you should have a DeleteCommand somewhere that is triggered when the DeleteKey is pressed in the DataGrid. In the Execute() method of this command, you should:
Ask the confirmation.
If user chooses yes, then remove the item from the collection. This removal should directly be reflected on the DataGrid.
If user chooses no, do nothing.
This means, though that the DataGrid.CanUserDeleteRows is set to False since you basically have to control when the rows get deleted.
Hope this helps.

Click event in UserControl- WPF

I have a UserControl in my WPF application.
I want to call a click event and do some things when the user clicked the UserControl.
The problem is- the UserControl doesn't have a click event.
I searched on the web and found that you can use the MouseLeftButtonUp event.
I tried it- but it doesn't respond to my clicks.
You didn't write what you are trying to do but if you need a click event maybe you are writing some kind of button (the Button class is actually "something you can click" with the visual representation in a control template you can replace)
If you need a button with complex content inside - put your user control inside a button
If you need a button that doesn't look like a button write a custom control template for button
If you need a button with extra functionality subclass button, add the extra data/behavior in code and put the display XAML in a style.
I think for your needs PreviewMouseLeftButtonUp(Down) event is more suitable. Then you need to handle ClickCount for counting amount of clicks and then raise your own event, on which other controls will know, that your control is clicked. There are much more methods on handling click event. You should look at this msdn article and this
UPDATE to handle both Click and DoubleClick
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_myCustomUserControl.MouseLeftButtonUp += new MouseButtonEventHandler(_myCustomUserControl_MouseLeftButtonUp);
_myCustomUserControl.MouseDoubleClick += new MouseButtonEventHandler(_myCustomUserControl_MouseDoubleClick);
}
bool _doubleClicked;
void _myCustomUserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
_textBlock.Text = "Mouse left button clicked twice";
_doubleClicked = true;
e.Handled = true;
}
void _myCustomUserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_doubleClicked)
{
_doubleClicked = false;
return;
}
_textBlock.Text = "Mouse left button clicked once";
e.Handled = true;
}
}
To test this example name your control as _myCustomUserControl and add a TextBlock named _textBlock to your MainWindow.xaml
Why not just use MouseDown?
Put the event in the User Control and simply do this:
private void MyControl_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
MessageBox.Show("Clicked!");
}
}

Resources