How i can delete the key which presente "Enter"?? I want the programme show "Plase enter Espace or Tab" when the user tape Enter and then delete this Enter Key. Thank you!
private: System::Void richTextBoxCommentaire_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
if (e->KeyValue == (char)13)
{
MessageBox::Show ("Please enter Espace or Tab");
//To delete this Key??
}
}
sorry, using the previewKeyDown should have worked but try the next method :
Use the KeyDown event to filter the desired keys by setting the suppressKeyPress to true as you can see in the example below (the example is in C# for readability but conversion to C++ CLI should be trivial :) :
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = e.KeyCode == Keys.Enter;
}
Related
I'm looking for a best way to implement common Windows keyboard shortcuts (for example Ctrl+F, Ctrl+N) in my Windows Forms application in C#.
The application has a main form which hosts many child forms (one at a time). When a user hits Ctrl+F, I'd like to show a custom search form. The search form would depend on the current open child form in the application.
I was thinking of using something like this in the ChildForm_KeyDown event:
if (e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control)
// Show search form
But this doesn't work. The event doesn't even fire when you press a key. What is the solution?
You probably forgot to set the form's KeyPreview property to True. Overriding the ProcessCmdKey() method is the generic solution:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.F)) {
MessageBox.Show("What the Ctrl+F?");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
On your Main form
Set KeyPreview to True
Add KeyDown event handler with the following code
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.N)
{
SearchForm searchForm = new SearchForm();
searchForm.Show();
}
}
The best way is to use menu mnemonics, i.e. to have menu entries in your main form that get assigned the keyboard shortcut you want. Then everything else is handled internally and all you have to do is to implement the appropriate action that gets executed in the Click event handler of that menu entry.
You can even try this example:
public class MDIParent : System.Windows.Forms.Form
{
public bool NextTab()
{
// some code
}
public bool PreviousTab()
{
// some code
}
protected override bool ProcessCmdKey(ref Message message, Keys keys)
{
switch (keys)
{
case Keys.Control | Keys.Tab:
{
NextTab();
return true;
}
case Keys.Control | Keys.Shift | Keys.Tab:
{
PreviousTab();
return true;
}
}
return base.ProcessCmdKey(ref message, keys);
}
}
public class mySecondForm : System.Windows.Forms.Form
{
// some code...
}
If you have a menu then changing ShortcutKeys property of the ToolStripMenuItem should do the trick.
If not, you could create one and set its visible property to false.
From the main Form, you have to:
Be sure you set KeyPreview to true( TRUE by default)
Add MainForm_KeyDown(..) - by which you can set here any shortcuts you want.
Additionally,I have found this on google and I wanted to share this to those who are still searching for answers. (for global)
I think you have to be using user32.dll
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312)
{
/* Note that the three lines below are not needed if you only want to register one hotkey.
* The below lines are useful in case you want to register multiple keys, which you can use a switch with the id as argument, or if you want to know which key/modifier was pressed for some particular reason. */
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed.
KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); // The modifier of the hotkey that was pressed.
int id = m.WParam.ToInt32(); // The id of the hotkey that was pressed.
MessageBox.Show("Hotkey has been pressed!");
// do something
}
}
Further read this http://www.fluxbytes.com/csharp/how-to-register-a-global-hotkey-for-your-application-in-c/
Hans's answer could be made a little easier for someone new to this, so here is my version.
You do not need to fool with KeyPreview, leave it set to false. To use the code below, just paste it below your form1_load and run with F5 to see it work:
protected override void OnKeyPress(KeyPressEventArgs ex)
{
string xo = ex.KeyChar.ToString();
if (xo == "q") //You pressed "q" key on the keyboard
{
Form2 f2 = new Form2();
f2.Show();
}
}
In WinForm, we can always get the Control Key status by:
bool IsCtrlPressed = (Control.ModifierKeys & Keys.Control) != 0;
The VB.NET version of Hans' answer.
(There's a ProcessCmdKey function template in Visual Studio.)
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If (keyData = (Keys.Control Or Keys.F)) Then
' call your sub here, like
SearchDialog()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class
I have a Windows Form application I am creating in C++/CLR Visual Studio 2012.
The goal is to have the user input values into the form named Home Page. Then once all the info is filled in they click a button and the Home Page form is hidden and then second form called Setup Info is shown.
The part I need help with is the info from Home Page needs to be accessible in Setup Info. To get an idea of how my files are setup this is the youtube video I followed to create my C++ Windows Form Application Click Here.
In my HomePage.h
// Button that will hide the Home Page Form and then show the SetupInfo Form.
private: System::Void Start_Click(System::Object^ sender, System::EventArgs^ e) {
this->Hide();
SetupInfo^ SetupInfo = gcnew ExcelToPPT::SeuUpInfo();
SetupInfo->Show();
}
In my Setup Info.H
// When Setup Info is loaded button1 will have the text of textbox1 from Home Page Form.
private: System::Void SetupInfo_Load(System::Object^ sender, System::EventArgs^ e) {
button1->Text = HomePage->Textbox1->Text;
}
This is the general idea but it doesn't work. How do I get this to work?
Let me know if you need any more information.
[EDIT]
I am able to do this through extern global variables, but is there another way to directly access the text text boxes?
Also wen I exit out of my Setup Information it doesn't seem to kill my program how do I solve this problem?
The simplest thing would probably be to just pass your Home Page form to the new SetUpInfo form.
private: System::Void Start_Click(System::Object^ sender, System::EventArgs^ e) {
this->Hide();
SetUpInfo^ setUpInfo = gcnew ExcelToPPT::SetUpInfo(this);
setUpInfo->Show(); ^^^^
}
In SetUpInfo.h:
public ref class SetUpInfo : Form
{
private:
HomePage^ homePage;
public:
SetUpInfo(HomePage^ homePage);
};
In SetUpInfo.cpp:
SetUpInfo::SetUpInfo(HomePage^ homePage)
{
this->homePage = homePage;
}
void SetUpInfo::SetUpInfo_Load(System::Object^ sender, System::EventArgs^ e)
{
button1->Text = this->homePage->Textbox1->Text;
}
I'm aware of how to handle key events, i.e.
private void Page_KeyUp(object sender, KeyRoutedEventArgs e)
{
switch (e.Key)
{
case Windows.System.VirtualKey.Enter:
// handler for enter key
break;
case Windows.System.VirtualKey.A:
// handler for A key
break;
default:
break;
}
}
But what if I need to discern between lower-case 'a' and upper-case 'A'? Also, what if I want to handle keys like the percent sign '%'?
Got an answer elsewhere. For those that are interested...
public Foo()
{
this.InitializeComponent();
Window.Current.CoreWindow.CharacterReceived += KeyPress;
}
void KeyPress(CoreWindow sender, CharacterReceivedEventArgs args)
{
args.Handled = true;
Debug.WriteLine("KeyPress " + Convert.ToChar(args.KeyCode));
return;
}
Even better, move the Window.Current.CoreWindow.CharacterReceived += KeyPress; into a GotFocus event handler, and add Window.Current.CoreWindow.CharacterReceived -= KeyPress; into a LostFocus event handler.
You can't easily get this information from KeyUp because KeyUp only knows which keys are being pressed, not which letters are being typed. You could check for the shift key being down and you could also try to track caps lock yourself. Better you use TextChanged event.
I am trying to make my first WP8 app but, i got a problem. I try to navigate to another page using the code below, but VS12 throws a error.
What am I doing wrong?
private void btnBMIBereken_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/BMI_Bereken.xaml", UriKind.Relative));
}
Your code is correct for navigating, just make sure the Page 'BMI_Bereken.xaml' actual exists at the root of your project.
Clean solution first and then rebuild again (right click on project/solution -> clean)
Then if it still crashes, try to use System.Windows.RoutedEventArgs e instead of RoutedEventArgs e
Guys i found the problem.
I use a self made button style for my buttons and i got on the visualstate pressed some wrong code. In fact I was trying to set the background collor of the button in the place of the name target name.
See the code below for what i did wrong.
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="#FF009AD3">
I know very stupid, but I want to thank everyone for the help.
Try this
private void btnBMIBereken_Click(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
this.NavigationService.Navigate(new Uri("/BMI_Bereken.xaml", UriKind.Relative));
});
}
You could do something like this:
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
if (txtDriverId.Text == "D0001" && txtPassword.Password == "open")
{
Frame.Navigate(typeof(VehicleCondition));
}
}
I have a TextBox with a two-way binding on the input. It is setup such that it fails validation if it is empty and displays a tooltip saying that it cannot be empty. My problem is that because it is failing validation, it tries to update the bindings everytime the text box changes (i.e. with every key press). I do not want it to update the source with every key press. I've narrowed it down to this code in the Silverlight 4.0 Tool kit for DataField.cs:
private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null && (ValidationUtil.ElementHasErrors(textBox) || !this._lostFocusFired[textBox]))
{
this._lostFocusFired[textBox] = false;
ValidationUtil.UpdateSourceOnElementBindings(textBox);
}
}
It is falling into the ValidationUtil.UpdateSourceOnElementBindings() because the element has errors. Is there anyway I can prevent it from doing this?
I think you want help rearranging your conditions to more accurately express your intent, but I'm not clear on what the existing code's result would be. This is why we ask for a complete, runnable (but minimal!) test case. However, if you simply don't want to update due to a failed validation, this should do the trick:
private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null && !this._lostFocusFired[textBox]))
{
ValidationUtil.UpdateSourceOnElementBindings(textBox);
}
}
You can validate the input and react to the result outside of that if statement.