Wpf - Enter on password will fire commnad button

Dani_S 4,071 Reputation points
2023-12-26T13:12:50.6933333+00:00

Hi,

I have password and button.

I want when i press enter on password it will fire the click event.

User's image

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,089 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 48,861 Reputation points Microsoft Vendor
    2023-12-27T02:03:08.43+00:00

    Hi @דני שטרית , Welcome to Microsoft Q&A,

    When TextBox receives the KeyDown event and the Enter key is pressed, it calls the TextBox_KeyDown method, which in turn calls the Button_Click method, which triggers the button's Click event. You can modify the code in the Button_Click method as needed

    <Grid>
        <TextBox Name="textBox1" KeyDown="TextBox_KeyDown" Margin="26,30,277,311" RenderTransformOrigin="0.5,0.5" >
        </TextBox>
        <Button Content="OK" Click="Button_Click" Margin="30,178,285,187" Height="69" d:LayoutOverrides="VerticalAlignment" />
    </Grid>
    
    private void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            Button_Click(sender, e);
        }
    }
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Button Clicked!");
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.