Window Form Closing Event - winforms

I am using C# for creating a window form application. In which I am using the MDI interface.
But I want to make like that:
private void Earnings_Leave(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure to Quite this form","confirmation Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if(result==DialogResult.Yes)
{
this.Close();
}
else if (result == DialogResult.No)
{
Earnings sibling = new Earnings();
sibling.MdiParent = this.MdiParent;
sibling.Show();
}
}
But this is not working the form will close in both conditions please help me.

Your code looks correct, however WinForms have the tendency to close itself when hits an error.
You must ensure that the reason of closing is the DialogResult.
Please change the part of your code with the following:
private void Earnings_Leave(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure to Quite this form","confirmation Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if(result==DialogResult.Yes)
{
MessageBox.Show("You selected Yes", "Yes Message", MessageBoxButtons.OK);
this.Close();
}
else if (result == DialogResult.No)
{
MessageBox.Show("You selected No", "No Message", MessageBoxButtons.OK);
Earnings sibling = new Earnings();
sibling.MdiParent = this.MdiParent;
sibling.Show();
}
}
If you see the dialogboxes, you will know the code works properly or not.

Related

WPF Form Validation

private void StartDate_LostFocus(object sender, RoutedEventArgs e)
{
if (!validate())
{
Dispatcher.BeginInvoke(
DispatcherPriority.ContextIdle,
new Action(delegate()
{
StartDate.Focus();
})
});
}
I'm validating a date in the lost focus of a textbox of a WPF application. Currently, I do some date validation - if it fails validation I reset the focus to the textbox. Is this the correct approach?
It seems to be working fine, I'm hoping I'm not creating any problems or memory leaks with the BeginInvoke.
Thanks in advance
As Adi suggested, please consider using more common approach to validation. However, if the application you're working on is something just for fun or just to get to know how WPF works, please consider:
validating asynchronously
interacting with UI synchronously in the UI thread
Also, validate before losing the focus (TextChanged might be an option). Something like this:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox validatedTextbox = sender as TextBox;
validatedTextbox.IsEnabled = false;
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (s, ea) => { Thread.Sleep(1000); }; // just to simulate really long validation
worker.RunWorkerCompleted += (s, ea) =>
{
if (validatedTextbox.Text.Contains('a'))
{
validatedTextbox.Background = new SolidColorBrush(Colors.Red);
}
else
{
validatedTextbox.Background = new SolidColorBrush(Colors.Green);
}
validatedTextbox.IsEnabled = true;
};
worker.RunWorkerAsync();
}

How to Cancel Datagrid selection changed event in WPF?

I know this question is asked before, but I couldnt find what I am looking for.
private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (oOrdItem.ItemNo == 0)
{
e.Handled = true;
MessageBox.Show("Please save the order item", "Save");
return;
}
}
Even if I call e.Handled = true;
it will select the datagrid row. I dont want to call dataGrid1.SelectedIndex =-1; because it will trigger selectionchanged event again. I also tried dataGrid1.UnSelectAll();
Any other way to cancel the selectionchanged event?
I used a variety of methods to try to cancel the selection changed event, including the method from the selected answer, but none of them worked. This, however, worked great for me:
Using the PreviewMouseDown event-handler for the datagrid:
private void dataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
//get the item I am clicking on (replace MyDataClass with datatype in datagrid)
var myItem = (e.OriginalSource as FrameworkElement).DataContext as MyDataClass;
//check if item is different from currently selected item
if (myItem != dataGrid.SelectedItem)
{
//save message dialog
MessageBoxResult result = MessageBox.Show("Changes will be lost. Are you sure?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
//if click no, then cancel the event
if (result == MessageBoxResult.No)
{
e.Handled = true;
}
else
{
//otherwise, reinvoke the click event
dataGrid.Dispatcher.BeginInvoke(
new Action(() =>
{
RoutedEventArgs args = new MouseButtonEventArgs(e.MouseDevice, 0, e.ChangedButton);
args.RoutedEvent = UIElement.MouseDownEvent;
(e.OriginalSource as UIElement).RaiseEvent(args);
}),
System.Windows.Threading.DispatcherPriority.Input);
}
}
}
}
This successfully keeps the current row selected if the user clicks "No", and if they click "Yes", then execution will continue as normal. Hopefully this helps someone in the future, because it took a long time to find something that would work for a seemingly simple problem.
Did you think about an alternative implementation? I'm thinking on Binding and a check method before changing the SelectedItem. An illustration:
<DataGrid ItemsSource="..." SelectedItem="{Binding SelectedEntry}" />
and the underlying VM could look like this:
public class SampleVm : ViewModelBase//assuming that you are using such a base class
{
private object _selectedEntry;
public object SelectedEntry
{
get { return _selectedEntry; }
set
{
if (!SavePrevItem())
return;
_selectedEntry = value;
RaisePropertyChanged("SelectedItem"); // or something similar
}
}
private bool SavePrevItem()
{
// your logic here
}
}

Capturing WndProc message of a certain button click

I have a cancel button on my form. I want to determine inside the WndProc method that this Cancel button is clicked and write some code for it. This is absolutely necessary because otherwise I'm not able to cancel all other control validation events that are yet to be performed.
Please help.
.NET - 2.0, WinForms
This is how you could parse the WndProc message for a left-click on a child control:
protected override void WndProc(ref Message m)
{
// http://msdn.microsoft.com/en-us/library/windows/desktop/hh454920(v=vs.85).aspx
// 0x210 is WM_PARENTNOTIFY
// 513 is WM_LBUTTONCLICK
if (m.Msg == 0x210 && m.WParam.ToInt32() == 513)
{
var x = (int)(m.LParam.ToInt32() & 0xFFFF);
var y = (int)(m.LParam.ToInt32() >> 16);
var childControl = this.GetChildAtPoint(new Point(x, y));
if (childControl == cancelButton)
{
// ...
}
}
base.WndProc(ref m);
}
BTW: this is 32-bit code.
And if there are controls which failed validation then CauseValidation does not help
Well, sure it does, that's what the property was designed to do. Here's an example form to show this at work. Drop a textbox and a button on the form. Note how you can click the button to clear the textbox, even though the box always fails its validation. And how you can close the form.
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
textBox1.Validating += new CancelEventHandler(textBox1_Validating);
button1.Click += new EventHandler(button1_Click);
button1.CausesValidation = false;
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
private void textBox1_Validating(object sender, CancelEventArgs e) {
// Always fail validation
e.Cancel = true;
}
void button1_Click(object sender, EventArgs e) {
// Your Cancel button
textBox1.Text = string.Empty;
}
void Form1_FormClosing(object sender, FormClosingEventArgs e) {
// Allow the form to close even though validation failed
e.Cancel = false;
}
}

How to stop the execution of DialogResult based on a condition?

I'm having a issue with the following scenario on Windows Forms:
I created a Form with two buttons, each button have been assigned with the behaviour DialogResult OK and DialogResult Cancel, respectively.
But based on certain conditions, I need to stop the execution of the OK button. The problem is that if I just made a return like this:
private void btnOk_Click(object sender, EventArgs e)
{
foreach(Control control in tblTable.Controls)
{
if (control.GetType() == typeof(TextBox))
{
if (control.Text.Trim() == "")
{
control.Focus(); return;
}
}
else
{
}
}
}
The dialog result keeps returning the OK answer to the parent form.
I need to stop the execution of the event and not return any answer until the user corrects the info on the form. In other words, the user should be taken back to the form to correct any missing or wrong data.
As Hans Passant mentions in an comment, just set the DialogResult to None!
Like this:
private void btnOk_Click(object sender, EventArgs e)
{
if(ValidationFailed())
{
this.DialogResult = DialogResult.None;
return;
}
//...
}
Personally I wouldn't use DialogResults on buttons in this scenario. I only tend to set the DialogResult when there's only distinct options that do not require any additional logic (i.e. making a custom MessageBox).
What I would do is to just send the DialogResult yourself on success:
private void btnOk_Click(object sender, EventArgs e)
{
if (allIsOK())
{
this.DialogResult = DialogResult.OK;
}
}
Consider tapping into the Forms's Closing event, and use the Cancel property of the event args to cancel form closing.
Here's a web page that discusses the idea; it's VB, but you'll get the idea:
http://www.vbinfozine.com/t_wfdlg.shtml
In Case you have used DialogResult on a button in a child Form (Which is a dialog), and want to return to form with no dialog result use a function on the form_closing() event:
private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (notValidated)
{
e.Cancel = true;
}
}

What is the "pressed the delete key" event for the WPF Datagrid?

I want to enable the user to highlight a row on the WPF DataGrid and press delete key to delete the row.
the functionality is already built into the UI of the grid, so to the user, the row disappears
I currently handle this on the SelectionChanged event (code below)
I loop through all the "e.RemovedItems" and delete them with LINQ
Problem is: even when you simply select a row and move off of it, selection change is fired and that row is in e.RemovedItems (which is odd, why would simply selecting something put it in a RemovedItems container?).
So I am looking for a DeleteKeyPressed event so I can simply handle it. What is that event called?
I am using the March 2009 toolkit.
XAML:
<Grid DockPanel.Dock="Bottom">
<toolkit:DataGrid x:Name="TheDataGrid"
SelectionChanged="TheDataGrid_SelectionChanged"
AutoGenerateColumns="True"
RowEditEnding="TheDataGrid_RowEditEnding"/>
code-behind:
private void TheDataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0)
{
Message.Text = "The following were removed: ";
foreach (object obj in e.RemovedItems)
{
Customer customer = obj as Customer;
Message.Text += customer.ContactName + ",";
_db.Order_Details.DeleteAllOnSubmit(
customer.Orders.SelectMany(o => o.Order_Details));
_db.Orders.DeleteAllOnSubmit(customer.Orders);
_db.Customers.DeleteOnSubmit(customer);
}
}
try
{
_db.SubmitChanges();
}
catch (Exception ex)
{
Message.Text = ex.Message;
}
}
ANSWER:
Thanks lnferis, that was exactly what I was looking for, here is my finished delete handling event for the datagrid, note the KeyDown event doesn't fire for some reason.
XAML:
<toolkit:DataGrid x:Name="TheDataGrid"
KeyDown="TheDataGrid_KeyDown"
PreviewKeyDown="TheDataGrid_PreviewKeyDown"
AutoGenerateColumns="True"
RowEditEnding="TheDataGrid_RowEditEnding"/>
code-behind
private void TheDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
var grid = (DataGrid)sender;
if (grid.SelectedItems.Count > 0)
{
string checkMessage = "The following will be removed: ";
foreach (var row in grid.SelectedItems)
{
Customer customer = row as Customer;
checkMessage += customer.ContactName + ",";
}
checkMessage = Regex.Replace(checkMessage, ",$", "");
var result = MessageBox.Show(checkMessage, "Delete", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
foreach (var row in grid.SelectedItems)
{
Customer customer = row as Customer;
_db.Order_Details.DeleteAllOnSubmit(
customer.Orders.SelectMany(o => o.Order_Details));
_db.Orders.DeleteAllOnSubmit(customer.Orders);
_db.Customers.DeleteOnSubmit(customer);
}
_db.SubmitChanges();
}
else
{
foreach (var row in grid.SelectedItems)
{
Customer customer = row as Customer;
LoadData();
_db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, customer); //TODO: this doesn't refresh the datagrid like the other instance in this code
}
}
}
}
}
private void TheDataGrid_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("never gets here for some reason");
}
The RemovedItems items reflects the items removed from the selection, and not from the grid.
Handle the PreviewKeyDown event, and use the SelectedItems property to delete the selected rows there:
private void PreviewKeyDownHandler(object sender, KeyEventArgs e) {
var grid = (DataGrid)sender;
if ( Key.Delete == e.Key ) {
foreach (var row in grid.SelectedItems) {
... // perform linq stuff to delete here
}
}
}
XAML
<DataGrid ItemsSource="{Binding}" CommandManager.PreviewCanExecute="Grid_PreviewCanExecute" />
Code behind
private void Grid_PreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
DataGrid grid = (DataGrid)sender;
if (e.Command == DataGrid.DeleteCommand)
{
if (MessageBox.Show(String.Format("Would you like to delete {0}", (grid.SelectedItem as Person).FirstName), "Confirm Delete", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
e.Handled = true;
}
}
What are you binding your DataGrid to?
Ideally, you should react to CollectionChanged events on the collection you are binding to. That way, your logic (deletion of removed items) will be separated from your UI.
You can build an Observable collection containing your objects and bind it to ItemsSource just for that purpose if the original collection does not have the necessary events.
It might not suit your specific setup, but that's how I usually do it.
Please follow the below code. I have succeeded with the below code.
Please let me know if changes are required.
private void grdEmployee_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Device.Target.GetType().Name == "DataGridCell")
{
if (e.Key == Key.Delete)
{
MessageBoxResult res = MessageBox.Show("Are you sure want to delete?", "Confirmation!", MessageBoxButton.YesNo,MessageBoxImage.Question);
e.Handled = (res == MessageBoxResult.No);
}
}
}
A little late to the party, but to get Inferis answer working:
Dim isEditing = False
AddHandler dg.BeginningEdit, Sub() isEditing = True
AddHandler dg.RowEditEnding, Sub() isEditing = False
AddHandler dg.PreviewKeyDown, Sub(obj, ev)
If e.Key = Key.Delete AndAlso Not isEditing Then ...
This fixes epalms comment: "if you're editing a cell and use the delete key to remove some characters in the cell, you'll end up deleting the whole row"
The cleanest solution is to use PreviewCanExecute like answered by flux, this is a completed solution to make it a bit more clear for anybody that overlooked his answer like I did:
private void Grid_PreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (e.Command == DataGrid.DeleteCommand)
{
if (MessageBox.Show($"Delete something from something else?", "Confirm removal of something", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
// Do what ever needs to be done when someone deletes the row
}
else
{
e.Handled = true;
// Handled means.. no worries, I took care of it.. and it will not delete the row
}
}
}
No need to hook on to CommandManager.Executed after this.
You want to handle the KeyUp or KeyDown event and check the pressed Key for Delete.
private void OnKeyDown(object sender, KeyEventArgs e) {
if ( Key.Delete == e.Key ) {
// Delete pressed
}
}

Resources