简体   繁体   English

退出按钮以取消操作

[英]Escape button to cancel action

Hi currently i have 2 buttons, Update and Modify. 您好,目前我有2个按钮,“更新”和“修改”。 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? 当我第一次单击“修改”按钮时,我可以更新文本框值,并且在这种状态下,如果我按“ ESC”按钮,我将隐藏“更新”按钮,并且文本框将是只读的?

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;
            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. 抱歉,我将项目号更改为majorversion。

You can write OnKeyPress Event for the window and trace the ESC button click. 您可以为窗口编写OnKeyPress事件并跟踪ESC按钮的单击。 Inside that you can write the logic to toggle the visibility of the controls. 在其中,您可以编写逻辑来切换控件的可见性。

您可以挂钩/处理KeyDown事件,检查所按下的键是否为Escape按钮,然后从此处对代码中的按钮和文本框进行更改。

You can set focus on text box when modify button clicked and then use KeyDown event on text box: 您可以在单击修改按钮时将焦点设置在文本框上,然后在文本框上使用KeyDown事件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM