I have a windows form which is a countdown Timer. What is the best way for me to pause the timer by pressing the spacebar on the keyboard. I have three buttons on the form for start, pause and stop and res-set. However I also want to pause, restart on press off spacebar. I tried to add a fourth button and then made it hidden on the form and added the following code (changed the EventArgs to KeyEventArgs:
private void button1_Click(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
// Pause the timer.
timer1.Enabled = false;
paused = true;
Start.Enabled = true;
Pause.Enabled = false;
}
However when i try to run this i get the error - No overload for 'button1_Click matches
delegate System.EventHandler
Is there something I have missed or a better way off doing this.
Any Help/Advice would be appreciated.
The Click event doesn't take a KeyEventArgs.
You're looking for the form's KeyPress event.
Related
The requirement is for a user to enter a number in a textbox and if they press F5 the code does something with the value in the textbox. I have set the form's this.KeyPreview = true; When user hits F5 the keyup event fires and processes correctly, BUT now every time I enter a character in the textbox the keyup event also fires. Is there a way to turn this off? or is it just something I have to deal with in debugging? If I don't have breakpoint there it's fine, but do I want that event to fire for every character?
Subscribe to the textbox event Keyup
In the event handler check the KeyCode. If it equals Keys.F5 do what you want to do when F5 is pressed. Also inform the system that you handled the event. In all other cases do what you want to do if F5 is not pressed.
private void OnKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F5)
{
this.HandleF5();
e.Handled = true;
}
else
{
// not F5
...
}
}
private void HandleF5()
{
// On F5 clear the textbox and sound a beep
this.textBox1.Text = String.Empty;
System.Media.SystemSounds.Beep.Play();
}
Consider to also check properties Alt and Control if you want to do simething different on Alt-F5 / Ctrl-F5.
First, I have to use windows forms I know it is bad for making games, but its for school.
I have designed a pause menu for my game using a panel with buttons on it. Here is the code for When I press escape to open the pause menu.
private void Game_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
if (paused == true)
{
paused = false;
pnlPaused.Visible = false;
tmrAnimations.Start();
tmrGame.Start();
tmrJump.Start();
tmrPlayerMovement.Start();
tmrLeftMovement.Start();
tmrRightMovement.Start();
}
else
{
pnlPaused.BringToFront();
pnlPaused.Visible = true;
pnlPaused.BackColor = Color.FromArgb(80, 79, 249, 249);
paused = true;
tmrAnimations.Stop();
tmrGame.Stop();
tmrJump.Stop();
tmrPlayerMovement.Stop();
tmrLeftMovement.Stop();
tmrRightMovement.Stop();
}
}
}
This code works fine until I click one of the buttons. When any button is clicked, even the ones with no code, it is impossible to press escape to unpause again. With this resume button the timers all start and the game starts working again but the form doesn't register any key clicks.
private void btnResume_Click(object sender, EventArgs e)
{
paused = false;
pnlPaused.Visible = false;
tmrAnimations.Start();
tmrGame.Start();
tmrJump.Start();
tmrPlayerMovement.Start();
tmrLeftMovement.Start();
tmrRightMovement.Start();
}
I have tried adding this code into the button click code
pnlPaused.Enabled = false;
This code works in re-enabling user input but every time a key is pressed the windows error sound plays, the one when you have to dismiss an alert etc but try clicking off (hopefully that makes sense). Does anyone know another way to fix the panel still being active after being hidden or preventing the windows sound?
I have realized all I needed to do was unfocus the button with
this.Focus();
I am making a simple calculator app where the DEL button removes the last digit in the viewport, but if held down for one second it deletes everything in the viewport. Right now If I press the button once, it deletes the last digit whether I hold it down or not. If I press it down again (held down or just a regular tap) it clears everything in the viewport and I cannot type anything else in.
Firstly I create the timer like so:
public System.Timers.Timer timer = new System.Timers.Timer(1);
and then I have a function for when the 'DEL' button is pressed:
private void DeletePressed(object sender, EventArgs args)
{
timer.Stop();
timer.Start();
timer.Elapsed += AllClear;
}
a function to stop the timer and delete the last character from the viewport when the button is released:
private void DeleteReleased(object sender, EventArgs args)
{
timer.Stop();
if (CalculatorString.Length > 0)
{
CalculatorString = CalculatorString.Remove(CalculatorString.Length - 1);
}
viewport.Text = CalculatorString;
}
and finally the procedure that is called when the timer finishes:
private void AllClear(Object Source, System.Timers.ElapsedEventArgs e)
{
timer.Stop();
CalculatorString = "";
viewport.Text = CalculatorString;
}
however none of what I expected tohas happened. I appreciate any help in advance :)
There is no direct way to listen long press in Xamarin.Forms(PCL). You have to write separate custom renderers for each platform to Listen long press and communicate it to the PCL.
You can refer this link for code example for doing the same.
After a button is clicked in a Windows form application written in C#, how to wait for another button to be clicked? Meanwhile I am updating a datagridview dynamically by current information.
EDIT
After button1 is clicked, I want to repeatedly update a dataGridView with current information and when button2 is clicked I want to stop updating the dataGridView.
Use Timer Class.
public partial class Form1 : Form
{
System.Windows.Forms.Timer timer;
public Form1()
{
InitializeComponent();
//create it
timer = new Timer();
// set the interval, so it'll fire every 1 sec. (1000 ms)
timer.Interval = 1000;
// bind an event handler
timer.Tick += new EventHandler(timer_Tick);
//...
}
void timer_Tick(object sender, EventArgs e)
{
//do what you need
}
private void button1_Click(object sender, EventArgs e)
{
timer.Start(); //start the timer
// switch buttons
button1.Enabled = false;
button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer.Stop(); //stop the timer
// switch buttons back
button1.Enabled = true;
button2.Enabled = false;
}
From MSDN:
A Timer is used to raise an event at user-defined intervals. This
Windows timer is designed for a single-threaded environment where UI
threads are used to perform processing. It requires that the user code
have a UI message pump available and always operate from the same
thread, or marshal the call onto another thread.
When you use this timer, use the Tick event to perform a polling
operation or to display a splash screen for a specified period of
time. Whenever the Enabled property is set to true and the Interval
property is greater than zero, the Tick event is raised at intervals
based on the Interval property setting.
So you have button A and button B. When button A is pressed you want to wait for button B to be pressed, then do something special? Without more information the simplest way is something like this:
private void OnButtonAClicked(object sender, EventArgs e)
{
ButtonA.Enabled = false;
ButtonA.Click -= OnButtonAClicked;
ButtonB.Click += OnButtonBClicked;
ButtonB.Enabled = true;
}
private void OnButtonBClicked(object sender, EventArgs e)
{
ButtonB.Enabled = false;
ButtonB.Click -= OnButtonBClicked;
// Do something truly special here
ButtonA.Click += OnButtonAClicked;
ButtonA.Enabled = true;
}
This code will toggle(initial state; button A is enabled, button B is disabled), when button A is pressed, button B becomes enabled and processes events, etc.
Use the BackgroundWorker http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
It doesn't freeze the UI, support ProgressBar, also it can be async. At the link you will see a good example with same functional that you want (start some task by click on one button and cancel it by click on another button).
I was wondering if anyone knows how to use a dialog box to create a hold down button event. Here is the scenerio:
a user would like to shutdown their system, but because it is critical that they confirm, that user must hold the button for 5 seconds before the action can be done.
I am trying to do it in a yes no scenario ie.
To confirm shutdown please hold "Yes" for 5 seconds.
Anyone done this before able to offer a little help/insight?
Try using a button's Mouse_Down & Mouse_Up event, and a timer (this assumes you're using WinForms).
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (this.timer1.Enabled == false)
{
this.timer1.Interval = 5000;
this.timer1.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Enabled = false;
MessageBox.Show("Shutdown!");
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Enabled = false;
}
You could capture the button press on 'mousedown', and start a 5-second timer. Once the timer completes, shutdown is initiated. If a 'mouseup' event happens, it could stop and reset the timer.
Sure, handle BOTH the mousedown event and the mouseup event. Start a timer on the mousedown and see how long it has run on the mouseup. Done!
You could do this any number of ways. The first that comes to my mind would be to spin off a thread that waits 5 seconds and is simply aborted if the user's mouse comes back up.
Thread shutdown;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
shutdown = new Thread(()=>ShutDown());
shutdown.Start();
}
private void ShutDown()
{
Thread.Sleep(5000);
Console.Write("5 seconds has elapsed");
// Do something.
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (shutdown != null)
{
shutdown.Abort();
shutdown = null;
}
}
Low overhead and you're not adding additional supporting controls for something this simple.
Why bother when you can just use getAsyncKeyState()? Tell them to hold down 'y' for 5 seconds. You can find a reference here: http://www.pinvoke.net/default.aspx/user32.getasynckeystate
Or you can do it your way and start a timer on MouseDown, then on MouseUp, end the timer and then see if it's more or less than 5 seconds. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousedown%28VS.71%29.aspx
You can use the Form.MouseDown events do detect that the user has pressed the mouse button. In the event handler, check to see if cursor is over the button or not (the event is passed in the coordinates of the cursor). You can then enable a timer which will tick in 5 seconds, and perform the shutdown when the timer ticks.
When the user first clicks YES, start a timer that repeatedly checks if the mouse location is inside of the button. After 5 seconds has elapsed, proceed with the shutdown. If the user moves the mouse out of the button, stop the timer.
private DateTime mouseDownTime;
private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
mouseDownTime = DateTime.Now;
}
private void Button_MouseUp(object sender, MouseButtonEventArgs e)
{
if (mouseDownTime.AddSeconds(5) < DateTime.Now)
MessageBox.Show("You held it for 5 seconds!");
}
You can set up a timer on the MouseDown event, and if the mouse capture changes (check the MouseCaptureChanged event) to false before the timer event fires, cancel the timer.