开发者

Escape button to cancel action

Hi currently i have 2 buttons, Update and Modify. Update button is set to be hidden initially.

When i click Modify button, Modify button hides, Update button appears, Textbox becomes non read only. Then Clicking update button will hide update button and modify buttons appear and textbox will be hidden and label will appear.

How can i change the code so that:

When i first click modify button, and i can get to update the textbox values and in this state if i press "ESC" button, i will hide "update" button and textbox will be read only?

The following is the current code:

private void ProjectnumberupdateButton_Click(object sender, RoutedEventArgs e)
        {
            ProjectnumberresultLabel.Content = ProjectnumberTextBox.Text;
            ProjectnumberupdateButton.Visibility = Visibility.Hidden;
            ProjectnumberTextBox.Visibility = Visibility.Hidden;
            ProjectnumbermodifyButton.Visibility = Visibility.Visible;
   开发者_StackOverflow         PreviousbuildversionresultLabel.Content = "" + MajorversionresultLabel.Content + "." + MinorversionresultLabel.Content + "." + ProjectnumberresultLabel.Content + "." + BuildnumberresultLabel.Content;
        }


private void ProjectnumbermodifyButton_Click(object sender, RoutedEventArgs e)
{
    ProjectnumberupdateButton.Visibility = Visibility.Visible;
    ProjectnumberTextBox.Visibility = Visibility.Visible;
    ProjectnumbermodifyButton.Visibility = Visibility.Hidden;
}

EDIT: This is what i have done so far:

    private void MajorversionTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Escape)
        {
            MajorversionupdateButton.Visibility = Visibility.Hidden;
            MajorversionTextBox.Visibility = Visibility.Hidden;
            MajorversionmodifyButton.Visibility = Visibility.Visible;
        }
    }

    private void MajorversionmodifyButton_Click(object sender, RoutedEventArgs e)
    {
        MajorversionupdateButton.Visibility = Visibility.Visible;
        MajorversionTextBox.Visibility = Visibility.Visible;
        MajorversionmodifyButton.Visibility = Visibility.Hidden;
        Keyboard.Focus(MajorversionTextBox);
        MajorversionTextBox_KeyDown(); // this is the line. i have trouble hooking this up
    }

sorry, i changed the project number to majorversion.


You can write OnKeyPress Event for the window and trace the ESC button click. Inside that you can write the logic to toggle the visibility of the controls.


You can hook / handle the KeyDown event, check if the key pressed was the Escape button and make your changes to you buttons and text boxes in the code from there.


You can set focus on text box when modify button clicked and then use KeyDown event on text box:

private void ProjectnumberTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
    {
       ProjectnumberTextBox.ReadOnly = true;
       ProjectnumbermodifyButton.Visibility = Visibility.Hidden;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜