//This Method is used to change the shortcut. It will show the form and allow the user enter a shortcut. //However, if you want to do something else when the user saves the form. //Change the DialogResult of btnSave to none and write your code in the Click event. public static string ChangeShortcut(string current) { //Since this is a shared function, we'll create an instance of the form and show it to the user. SetKey ThisForm = new SetKey(); ThisForm.txtButton.Text = current; //Set the textbox text to the current global shortcut. DialogResult UserResponce = ThisForm.ShowDialog(); //display the form as a dialog. if (UserResponce != DialogResult.Cancel) // The user did not press cancel. { if (ThisForm.txtButton.Text == Keys.None.ToString()) //The user did not enter a shortcut. { DialogResult MessageResult = MessageBox.Show( "You have not specified a global shortcut." + "\nPress Yes to keep it this way." + "\nPress No to specify a shortcut." + "\nPress Cancel to use the default shortcut.", "Global Shortcut Example.", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); //The dialog to display to the user. if (MessageResult == DialogResult.Yes) //The user does not want to specify a shortcut. { return(Keys.None.ToString()); } else if (MessageResult == DialogResult.No) // The user wants to go back and specify a shortcut. { ChangeShortcut(current); // Call the method again. } else // The user wants to use the default global shortcut. { return("Shift + Control + Alt + T"); } } else //The user entered a shortcut. { return(ThisForm.txtButton.Text); } } else { //Enter code here for if the user pressed cancel. That is if in case you do not want that. //You could show the form again by uncommenting the line below. or display a message. //ChangeShortCut() } return(current);; //Returns the current shortcut. }
//This Method is used to change the shortcut. It will show the form and allow the user enter a shortcut. //However, if you want to do something else when the user saves the form. //Change the DialogResult of btnSave to none and write your code in the Click event. public static string ChangeShortcut(string current) { //Since this is a shared function, we'll create an instance of the form and show it to the user. SetKey ThisForm = new SetKey(); ThisForm.txtButton.Text = current; //Set the textbox text to the current global shortcut. DialogResult UserResponce = ThisForm.ShowDialog(); //display the form as a dialog. if (UserResponce != DialogResult.Cancel)// The user did not press cancel. { if (ThisForm.txtButton.Text == Keys.None.ToString()) //The user did not enter a shortcut. { DialogResult MessageResult = MessageBox.Show( "You have not specified a global shortcut." + "\nPress Yes to keep it this way." + "\nPress No to specify a shortcut." + "\nPress Cancel to use the default shortcut.", "Global Shortcut Example.", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); //The dialog to display to the user. if (MessageResult == DialogResult.Yes) //The user does not want to specify a shortcut. { return Keys.None.ToString(); } else if (MessageResult == DialogResult.No)// The user wants to go back and specify a shortcut. { ChangeShortcut(current); // Call the method again. } else // The user wants to use the default global shortcut. { return "Shift + Control + Alt + T"; } } else //The user entered a shortcut. { return ThisForm.txtButton.Text; } } else { //Enter code here for if the user pressed cancel. That is if in case you do not want that. //You could show the form again by uncommenting the line below. or display a message. //ChangeShortCut() } return current; ; //Returns the current shortcut. }
private void btnModify_Click(object sender, EventArgs e) { lblShortcut.Text = SetKey.ChangeShortcut(lblShortcut.Text); UpdateCustomShortcut(lblShortcut.Text); }