Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close WPF window (dialog box) when Enter key is pressed?

I have a WPF window which opens as a modal dialog.

On dialog, I have OK & Cancel buttons with IsDefault & IsCancel properties set to True for them respectively. Both the buttons have Click event handlers which close the dialog box.

Here is the relevant XAML:

<StackPanel Orientation="Horizontal" Grid.Row="1"  Height="45" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="190">
    <Button Content="OK"
                Height="25" Margin="10,10,10,10" Width="75" Name="btnOK" TabIndex="1600" IsDefault="True" Click="btnOK_Click"                       
                VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
    <Button Content="Cancel"
                Height="25" Margin="10,10,10,10" Width="75" Name="btnCancel" TabIndex="1700" IsCancel="True"
                VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Click="btnCancel_Click" />
</StackPanel>

Here is the code behind:

private void btnOK_Click(object sender, RoutedEventArgs e)
{
    // My some business logic is here                
    this.Close();
}

private void btnCancel_Click(object sender, RoutedEventArgs e)
{
    this.Close();
}

When I press Esc button on the keyboard (even when focus is not on the Cancel button), the dialog box gets closed as expected. However, when I press Enter key when focus is NOT on the OK button, nothing happens.

I have a DataGrid on the dialog. I want to close the dialog when I select any row in the data grid and press enter.

How to make this thing happen?

Some additional information: I have a text box on the dialog. And it has the event handler for the Keyboard.PreviewKeyDown event. When I am in the text box and I press enter, the dialog box should not be closed. But I can remove this handler. Important thing is to get above question resolved.

private void tbxSearchString_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        this.Search(); // Does some searching
    }
}
like image 351
Learner Avatar asked Sep 22 '11 05:09

Learner


People also ask

How to close dialog box in WPF?

Pressing ALT + F4 . Pressing the Close button. Pressing ESC when a button has the IsCancel property set to true on a modal window.

How do I close all windows in WPF?

The proper way to shutdown a WPF app is to use Application. Current. Shutdown() . This will close all open Window s, raise some events so that cleanup code can be run, and it can't be canceled.


2 Answers

Your code is working fine for me. it close dialog when I press enter. You can write e.Handled = true; line after your search functionality in tbxSearchString_PreviewKeyDown event. So it will not close dialog.

<Grid>
        <TextBox Name="tbxSearchString" HorizontalAlignment="Left" Width="100" Height="30" Grid.Row="0" PreviewKeyDown="tt_PreviewKeyDown"></TextBox>
        <StackPanel Orientation="Horizontal" Grid.Row="1"  Height="45" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="190">

            <Button Content="OK" 
                Height="25" Margin="10,10,10,10" Width="75" Name="btnOK" TabIndex="1600" IsDefault="True" Click="btnOK_Click"                        
                VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
            <Button Content="Cancel" 
                Height="25" Margin="10,10,10,10" Width="75" Name="btnCancel" TabIndex="1700" IsCancel="True" 
                VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Click="btnCancel_Click" />
        </StackPanel>
    </Grid>

Code behind

private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true; 
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void tbxSearchString_PreviewKeyDown(object sender, KeyEventArgs e)
        {
           if (e.Key == Key.Enter)
           {
               this.Search();
               e.Handled = true;
           }
        }
like image 101
Upendra Chaudhari Avatar answered Sep 23 '22 12:09

Upendra Chaudhari


You shouldn't be calling Close() or handling PreviewKeyDown yourself.

The proper way to do this is to have Ok/Cancel buttons, and use Button.IsDefault, Button.IsCancel, and Window.DialogResult. If the 'enter' press is unhandled in your textbox, the keypress will propagate to the Window and the default button will be pressed.


MyForm.xaml:

<Button x:Name="btnOk" Content="Ok" Click="btnOk_Click" IsDefault="True"/>
<Button x:Name="btnCancel" Content="Cancel" Click="btnCancel_Click" IsCancel="True"/>

MyForm.xaml.cs:

private void btnOk_Click(object sender, RoutedEventArgs e)
{
    DialogResult = true;
}

private void btnCancel_Click(object sender, RoutedEventArgs e)
{
    DialogResult = false;
}

Now hitting enter or escape on any textbox in the form will close the form (with the proper result)

like image 26
BlueRaja - Danny Pflughoeft Avatar answered Sep 25 '22 12:09

BlueRaja - Danny Pflughoeft