C# Getting the focus on a button / making it active - c#

I'm making a snake game in C# and I have a pause and play button. Problem is: these buttons are in the exact same position, so I can't click start after I have clicked pause (because it's still active)
private void picBreak_Click(object sender, EventArgs e)
{
timer1.Stop();
picBreak.Visible = false;
picStart.Visible = true;
}
private void picStart_Click(object sender, EventArgs e)
{
timer1.Start();
picBreak.Visible = true;
picStart.Visible = false;
picStart.Focus = true;
}
The .Focus does not work and gives an error :/
ERROR = Error 1 Cannot assign to 'Focus' because it is a 'method group' C:\Users\Mave\Desktop\SnakeGame\Form1.cs 271 7 SnakeGame

Control.Focus is a method, not a property. This should be:
private void picStart_Click(object sender, EventArgs e)
{
timer1.Start();
picBreak.Visible = true;
picStart.Visible = false;
picBreak.Focus(); // Focus picBreak here?
}

In your code, you are trying to give focus to a control that you made invisible in the previous line... also, the compiler is telling you that Focus is a method, and you are trying to use it as a property.
I'd do this in a different way:
Just one button called btnStartPauseResume... and in the click event:
private void btnStartPauseResume_Click(object sender, EventArgs e)
{
if (btnStartPause.Text == "start")
{
btnStartPause.Text == "pause";
// code to start the game
}
else if (btnStartPause.Text == "pause")
{
btnStartPause.Text == "resume";
// code to pause the game
}
else if (btnStartPause.Text == "resume")
{
btnStartPause.Text == "pause";
// code to resume the game
}
}

Related

Is there an event for starting timer2 once timer1 stops in C# Windows Forms application?

I'm building a Flappy Bird game in C#. So far, the code works perfectly, however I added a GIF saying Game Over, and this image only shows once timer1 is stopped.
How can I achieve that?
Below is the code snippet for the GIF image (I tried both snippets of code, and they didn't work though there was no error):
private void pictureBox5_Click(object sender, EventArgs e)
{
if (timer2.Enabled)
{
pictureBox5.Show();
}
else
{
pictureBox5.Hide();
}
}
And here's the code snippet for timer2
private void timer2_Tick(object sender, EventArgs e)
{
if (!timer1.Enabled)
{
timer2.Enabled = true;
timer2.Start();
}
}
To disable Timer1, you must first enable Timer2, then disable Timer1.
private void Timer1_Tick(object sender, EventArgs e)
{
//For example, the gamer has 20 seconds
//So, we set the value of Label1.text to 20
if (int.Parse(Label1.Text) > 0)
{
int Counter = int.Parse(Label1.Text);
Counter--;
Label1.Text = Counter.ToString();
}
else
{
Timer2.Enabled = true;
Timer1.Enabled = false;
}
}
private void pictureBox4_Click(object sender, EventArgs e)
{
if (Timer2.Enabled == true)
{
pictureBox5.Show();
}
else
{
pictureBox5.Hide();
}
}
Tested in:
Visual Studio 2017
.NET Framework 4.5.2
Windows Forms
Thanks

How to allocate two different functions to the same button?

I am working on Winforms with C#.
I have a problem with the logic, there are two different methods that I need to call, so that if I click the button, the first action should get applied and if I click the same button again, the second action should get applied.
This is not the exact code but I have an idea something like this:
private void button1_Click(object sender, EventArgs e)
{
if(button1.click==true)
{
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}
Here I have two problems regarding whether the button is already clicked:
If it's not clicked the Imgsave() should get activated.
If button is clicked the Imgsave() should get closed.
Can anyone please help me with this? Thanks.
You need to keep state somewhere. You can do this:
private bool buttonClicked = false;
private void button1_Click(object sender, EventArgs e)
{
if(!buttonClicked)
{
buttonClicked = true;
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}
This assumes you never going to click it a third time. If you are, you would need to handle that in some way.
I'd have either a class level variable track the number of times a button is clicked:
private bool _unclicked = false;
private void button1_Click(object sender, EventArgs e)
{
if(!_unclicked)
{
_unclicked = true; //toggle so next time the ELSE will be performed
fileNumber = 1;
ImgSave();
}
else
{
_unclicked = false; //toggle it off again
ImgSave.exit();
}
}
, or I'd store it in the .Tag of the button:
private void button1_Click(object sender, EventArgs e)
{
if(!button1.Tag.ToString() == "unclicked")
{
button1.Tag = "clicked"; //toggle so next time the ELSE will be performed
fileNumber = 1;
ImgSave();
}
else
{
button1.Tag = "unclicked"; //toggle it off again
ImgSave.exit();
}
}
You could also remove one event handler and add another:
private void button1_FirstClick(object sender, EventArgs e)
{
button1.Clicked -= button1_FirstClick;
button1.Clicked += button1_SecondClick;
fileNumber = 1;
ImgSave();
}
private void button1_SecondClick(object sender, EventArgs e)
{
button1.Clicked -= button1_SecondClick;
button1.Clicked += button1_FirstClick;
ImgSave.exit();
}
I've always been less of a fan of adding and removing event handlers to achieve things like this but it's quite a clean solution
You should save your state in a variable. Your state will change after first click and you can change the state of Clicking button with calling ConditionChanger() method anytime.
For example you may need change the state of variable when you clicked a second button.
private void ConditionChanger(){
myState = !myState;
}
Your variable :
private bool myState = false;
And your click event :
private void button1_Click(object sender, EventArgs e)
{
if(!myState)
{
myState = true;
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}

(C#)How do I, upon pressing a button, Make a new form window appear? One that i can drag other buttons and text boxes onto

I'm new to C# and I need this function for a program im working on for school. I need to make a new window pop up when i click a button, not a message box though like a forms window, one that i can design with text boxes and buttons. What is on the new pop up window depends on the previous window but i can figure that out.
Also I need a way to close the previous window once the new one appears
Here's my code:`
// This makes sure only one box is checked
private void MulCB_CheckedChanged(object sender, EventArgs e)
{
if( MulCB.Checked == true)
{
DivCB.Checked = false;
AddCB.Checked = false;
SubCB.Checked = false;
}
}
private void DivCB_CheckedChanged(object sender, EventArgs e)
{
if (DivCB.Checked == true)
{
MulCB.Checked = false;
AddCB.Checked = false;
SubCB.Checked = false;
}
}
private void AddCB_CheckedChanged(object sender, EventArgs e)
{
if (AddCB.Checked == true)
{
DivCB.Checked = false;
SubCB.Checked = false;
MulCB.Checked = false;
}
}
private void SubCB_CheckedChanged(object sender, EventArgs e)
{
if (SubCB.Checked == true)
{
DivCB.Checked = false;
AddCB.Checked = false;
MulCB.Checked = false;
}
}
private void oneDCB_CheckedChanged(object sender, EventArgs e)
{
if(oneDCB.Checked == true)
{
twoDCB.Checked = false;
threeDCB.Checked = false;
}
}
private void twoDCB_CheckedChanged(object sender, EventArgs e)
{
if ( twoDCB.Checked == true)
{
oneDCB.Checked = false;
threeDCB.Checked = false;
}
}
private void threeDCB_CheckedChanged(object sender, EventArgs e)
{
if (threeDCB.Checked == true)
{
oneDCB.Checked = false;
twoDCB.Checked = false;
}
}
// ends here
// Button operation
private void button8_Click(object sender, EventArgs e)
{
var form = new Form();
}
}
}
`
Thanks a lot!
Sal
The project is im supposed to make a quizzing program for kids. They should be able to choose 1 operation and the amount of digits the numbers will have. It then has to out put 10 random questions according to the selection made by the kid, then once they have completed the quiz, it should display their results and which questions they got wrong.
Assuming that the design of the window doesn't have to be completely dynamic, you can design it in Visual Studio (I'm assuming you did so with the first one). Then you can pass the results to the window. Like:
// Note: Form2 ist the name of your designed From
Form2 myform = new Form2();
this.Hide();
//You could pass the question settings like this
// 1 is for multiplication, 2 for division,3 for addition, 4 for substraction
myform.operation=1;
myform.digits=2
myform.Show();
And in the code of Form2:
namespace Yournamespace {
public partial class Form2: Form {
//Add these two lines about here
public static int operation;
public static int digits;
public Form2() {
InitializeComponent();
}
}
}
Then you can use the variables in Form2 and fill in the textbox or other elements you might design.
Also: You cloud use radio buttons instead of checkboxes as you then won't have you worry about unchecking the other checkboxes.

How to restart timer?

I have this Code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Keys.Left)
{
left = true;
right = false;
down = false;
}
}
private void timerMoving_Tick(object sender, EventArgs e)
{
if(Ghost.Bounds.IntersectsWith(panel3.Bounds))
{
timerMoving.Stop();
}
if(left)
{
Ghost.Left -= 5;
timerMoving.Start();
}
}
In the above code as my Ghost(which is moving down) hits the panel3 boundary it stops.
I want my timer to start again as i press Left,Right or up key and only stop when it's moving down,hitting panel3. I tried doing this:
if(left)
{
Ghost.Left -= 5;
timerMoving.Start();
}
but nothing happens, Why?
The simple (though not the optimal) way to achieve this goal pertinent to your task description is by adding the line shown in the following code snippet:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Left)
{
// add this line
if(!timerMoving.Enabled) timerMoving.Start();
left = true;
right = false;
down = false;
up = false;
}
}
and removing this line as shown below:
if (left)
{
Ghost.Left -= 5;
//timerMoving.Start(); - comment off
}
Hope this may help.

I need to use a if statement on a button click object within a timer tick?

Basically in the code below where i highlight, i would like to know how to use drawBricks method in the timer tick when that button(btnDisplayBricks) is pressed. Because i am using a timer tick and picturebox for paper drawings etc i cannot just simply call the method from within the button click event because the paper then clears in the timer only allowing me to display the bricks before timer1 starts any ideas.
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (btnDisplayBricks_Click[0] = true) ///code here problem
//then call method
DrawBricks(paper);
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
}
}
}
problem is in your equation, you should use == instead of =
if (btnDisplayBricks_Click[0] == true)
also move methodbtnDisplayBricks_Click outside of timer1_Tick
private bool buttonClicked = false;
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (buttonClicked)
{
DrawBricks(paper);
// maybe you want to set buttonclicked to false again, but specs are not clear to me
// buttonClicked = false;
}
}
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
buttonClicked = true;
}

Categories

Resources