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();
Related
I am tired of trying to deactivate the button highlight when it's activated with "istab" set to true.
Basically, I want to deactivate the back highlight of a button (if it's activated) when I click on the picture box. The method partially works. The istab deactivation works when I click on the picture box and the highlight goes away. But I made a method so that when the mouse hovers over the button, "istab" automatically reactivates. But it doesn't seem to work. The "istab" remains deactivated.
Here is what I have tried so far.
private void LogoImage_Click(object sender, EventArgs e)
{
if (btnEssentialRules.IsTab == true)
btnEssentialRules.IsTab = false;
}
private void btnEssentialRules_MouseHover(object sender, EventArgs e)
{
if (btnEssentialRules.IsTab == false)
btnEssentialRules.IsTab = true;
}
Where the btnEssentialRules is the button name.
If you have another solution or a fix to my code. please help me.
I have an application which runs mostly within the taskbar, and its icon may be left clicked in order to open a Windows form.
The NotifyIcon also has a few MenuItems which can be accessed by right clicking, and each have their own click events to serve their functionality.
private void menuItem4_Click(object Sender, EventArgs e)
{
if (programEnabled == false)
{
programEnabled = true;
Console.WriteLine("Enabled!");
}
else
{
programEnabled = false;
Console.WriteLine("Disabled!");
}
}
//Icon clicked!
private void notifyIcon1_MouseClick(object Sender, EventArgs e)
{
//Reopen the form
Form f = new TemperForm();
}
Every time a MenuItem click event is activated however, NotifyIcon_Click also runs and I need that event for opening the Windows form. I don't want every time I click any MenuItem to also run the code that I wish to run when the icon is clicked.
How can I still use the NotifyIcon_Click event while not having it run every time I click a MenuItem?
I've been at this for hours and it's driving me up the wall!
Is there a way i can "suspend" a UWP app by pressing the back button.
I'm not even sure suspending is the right term but what i want to do is to close the app when the user presses the back button rather than going back to the Signup/Login page in my app.
I want the app go close but also still be shown when the user opens the recent apps menu by holding the back button.
Heres my code in the App.Xaml.Cs
private void OnBackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame.CurrentSourcePageType == typeof(Pages.Home))
{
App.Current.Exit();
}
if (rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
}
The Problem is with the App.Current.Exit();
it terminates the app where what i want to do is just close and and not go back to the MainPage.
How can I do that?
To leave your app without terminating it, just remove the App.Current.Exit(); and ignore the back event (e.Handled = false;). You will then get the default behavior which is to leave the app on mobile devices. On desktop, your app will stay on the page it is. You will then have to do anything that fit your need to save/restore your app in its appropriate state using the suspending/resuming event handlers.
void OnBackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame.CurrentSourcePageType == typeof(Pages.Home))
{
// ignore the event. We want the default system behavior
e.Handled = false;
}
else if (rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
}
I'm trying to figure out how I can reset my windows form application once the option to do so has been selected from a choice of to radio buttons. eg:.
So once "Yes" is selected the program resets. Here is what I have been trying.
private void EndOptions() {
if (anotheryes.Checked) {
RestartForm();
}
if (anotherno.Checked) {
//todo
}
}
The restartform method is just:
private void RestartForm() {
Application.Restart();
Application.ExitThread();
}
Right now, this only works if I press the "calculate ratio" button again. On click, the application will restart. I'm wanting it so that it resets just by clicking the Yes radiobutton.
The logic behind the button is as follows:
private void DisplayLogic() {
double height = 0, waist = 0;
if (WaistNumericCheck(out waist) && HeightNumericCheck(out height)) { //check if input is numeric, if true, move on to next check
if (CheckDimensionHeight(height) == true && CheckDimensionsWaist(waist) == true) { //check if dimensions are higher than the lower limits, if BOTH true, move on the next
ShowResult(height, waist); //shows results
DisableInputs(); //disables inputs while results are being shows
AnotherGroupBoxVisible();//makes the restart selection groupbox visible
EndOptions(); //check to see which option is selected
}
}
}
Any tips/help would be greatly appreciated thanks!
I have to say its an odd thing to have the entire app restart, just so the user could do another calculation.. The API you are using i.e. Application.Restart is documented for use with ClickOnce Applications
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.restart(v=vs.110).aspx
I don't know if your app is click once or not.. but anyway, hoping you'd learn something new regardless, here is literal answer to your question..
http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.checkedchanged(v=vs.110).aspx
This event occurs when the value of the Checked property of a radio button changes.
Anyway, I do strongly recommend, that as suggested in comment above, you should reconsider how you want your app to provide a better user experience here..
If its just for learning, the event should help.. and feel free to explore other events on the control which might be of interest.
Handle the CheckedChanged event of the RadioButton, and then you can immediately restart as soon as it is clicked:
private void AnotherYes_CheckedChanged(object sender, EventArgs e)
{
if (AnotherYes.Checked)
{
Application.Restart();
}
}
http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.checkedchanged(v=vs.110).aspx
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.