windows form keyup event is not working c# [duplicate] - c#

This question already has an answer here:
keydown in c# doesn't work for some reason
(1 answer)
Closed 5 years ago.
This code is not working in my main form
void main_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key pressed");
e.SuppressKeyPress = true;
}
}

Try to set Form property: main.KeyPreview = true;

Related

C# WPF How to use only numeric value in a textbox [duplicate]

This question already has answers here:
How do I get a TextBox to only accept numeric input in WPF?
(33 answers)
Closed 5 years ago.
I use a WPF application I would like to allow only numbers in a textbox. In a WindowsForm application I would know how I would have to make it.
Unfortunately, there is a KeyPress not in WPF and the "code" functioned also no longer.
How is it right and what is the event?
Here you can see the old code that has worked in Windows Form:
private void tbKreisdurchmesser_KeyPress(object sender, KeyEventArgs e)
{
if (char.IsNumber(e.Key) || e.KeyChar==".")
{
}
else
{
e.Handled = e.KeyChar != (char)Keys.Back;
}
}
You can add Previewtextinput event for textbox and validate the value inside that event using Regex,
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
var textBox = sender as TextBox;
e.Handled = Regex.IsMatch(e.Text, "[^0-9]+");
}

C# - Hide window on input from user [duplicate]

This question already has answers here:
Best way to implement keyboard shortcuts in a Windows Forms application?
(9 answers)
Closed 7 years ago.
I have put this code in my application, I want the window to hide when control + Q is clicked, why does this not work?
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Q)
{
this.Hide();
}
}
You can check the Key modifier in order to handle Keys combination
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q && e.Modifiers == Keys.Control)
{
//do stuff
}
}

C# How to detect kill event sender [duplicate]

This question already has answers here:
C# Cancel Windows Shutdown
(3 answers)
How to detect Windows shutdown or logoff
(3 answers)
Closed 8 years ago.
I have a FormClosing method for my program to ask the user if he want to exit the program before closing the form. This works great, but I don't want the dialog to show up when the program closes because of a system shutdown / logout. How can I detect that the kill command is sent by the system and not by the user clicking the x of my form? Envrionment.HasShutdownStarted does not work.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Need to check for system shutdown here before the next if is activated
if (MessageBox.Show(...) == DialogResult.No)
{
e.Cancel = true;
this.Activate();
}
}
Try checking e.CloseReason, e.g.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.WindowsShutDown)
{
if (MessageBox.Show(...) == DialogResult.No)
{
e.Cancel = true;
this.Activate();
}
}
}

C# WPF Disable the exit/close button [duplicate]

This question already has answers here:
How to hide close button in WPF window?
(23 answers)
Closed 9 years ago.
Is it possible to disable the close button in a WPF form?
How can I disable the close button?
I have been searching around and found the solution below. But that works only in Windows Form!
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
in wpf this event called Closing :
public Window4()
{
InitializeComponent();
this.Closing += new System.ComponentModel.CancelEventHandler(Window4_Closing);
}
void Window4_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
You need to implement a windows hook to accomplish that. See this MSDN post for details.

C# - preparing event after pushing two buttons on keyboard [duplicate]

This question already has answers here:
How to use multiple modifier keys in C#
(9 answers)
Closed 8 years ago.
i've got problem. If I push one key I can get event for example:
if (e.KeyCode == Keys.F4)
{
Method();
}
How could I do the same if I push two keys? For example Enter + F4?
FormLoad()
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
//Works for Ctrl+F4
if (e.Control && e.KeyCode == Keys.F4)
{
//Do something
}
}
See if this work for you.

Categories