To make sure that the player knows what difficulty they have chosen to proceed with onto my game, I want the current difficulty to be clearly indicated by the button's forecolour. Here's my code thus far that just changes the forecolour of each button whenever they're pressed:
public Form1()
{
InitializeComponent();
MinimizeBox = false;
MaximizeBox = false;
}
bool diffchosen = false;
public static string difficulty;
public static double multiplier;
public static int lives;
private void Form1_Load(object sender, EventArgs e)
{
}
private void easyButton_Click(object sender, EventArgs e)
{
this.ForeColor = Color.White;
diffchosen = true;
difficulty = "Easy";
multiplier = 0.8;
lives = 4;
}
private void mediumButton_Click(object sender, EventArgs e)
{
this.ForeColor = Color.White;
diffchosen = true;
difficulty = "Medium";
multiplier = 1.0;
lives = 3;
}
private void hardButton_Click(object sender, EventArgs e)
{
this.ForeColor = Color.White;
diffchosen = true;
difficulty = "Hard";
multiplier = 1.5;
lives = 2;
}
private void playButton_Click(object sender, EventArgs e)
{
if (diffchosen == false)
{
MessageBox.Show("Please choose a difficulty before proceeding");
}
else
{
Game_Screen game_Screen = new Game_Screen();
this.Hide();
game_Screen.Show();
}
}
But I had difficulty making sure that when the player decides to choose another option, the other 2 have the black forecolour property. I used this beforehand but it brought about issues like the fact that whenever I pressed a button, the Medium and Hard buttons decided to change their forecolour to white:
if (difficulty == "Hard" || difficulty = "Medium")
{
this.ForeColour = Color.Black;
}
How can I go about creating a loop that would change each button's forecolour depending on the user's choice of button?
Easiest might be just to create a toggle function e.g.
private void ToggleButton(int button)
{
easyButton.ForeColor = Color.Black;
mediumButton.ForeColor = Color.Black;
hardButton.ForeColor = Color.Black;
switch (button)
{
case 1: easyButton.ForeColor = Color.White; break;
case 2: mediumButton.ForeColor = Color.White; break;
case 3: hardButton.ForeColor = Color.White; break;
}
}
and then on every click event you just call this function with the appropriate 'button number'
e.g.
private void easyButton_Click(object sender, EventArgs e)
{
ToggleButton(1);
...
}
private void mediumButton_Click(object sender, EventArgs e)
{
ToggleButton(2);
...
}
private void hardButton_Click(object sender, EventArgs e)
{
ToggleButton(3);
...
}
I have two checkboxes on my form; chkBuried and chkAboveGround. I want to set it up so if one is checked, the other is unchecked. How can I do this?
I have tried the CheckChanged property:
private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
chkAboveGround.Checked = false;
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
chkBuried.Checked = false;
}
And it works, just not as well as I hoped. That is, when I check chkBuried, then check chkAboveGround, both boxes become unchecked before I can check another one again.
modify your code as below.
private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
chkAboveGround.Checked = !chkBuried.Checked;
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
chkBuried.Checked = !chkAboveGround.Checked;
}
I suggest you use check_click instead of check_changed
private void checkBox1_Click(object sender, EventArgs e)
{
checkBox2.Checked = false;
checkBox3.Checked = false;
}
private void checkBox2_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
checkBox3.Checked = false;
}
private void checkBox3_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
}
The reason for the behavior you have explained is that you are using CheckedChanged event, which means that when you are setting the Checked property of a CheckBox manually, the event is also fired, causing another box to react again.
Therefore, the following might help you:
private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
if (chkBuried.Checked == true) {
chkAboveGround.Checked = false;
} else {
chkAboveGround.Checked = true;
}
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
if (chkAboveGround.Checked == true) {
chkBuried.Checked = false;
} else {
chkBuried.Checked = true;
}
}
UPDATE 29.03.2020: functionally the code in my answer is the same as the answer given by Riz. Nevertheless, I am leaving the code as I put it originally since it might make the whole situation easier to understand for the people who are new to coding. If you are to implement anything similar in production code, please use the answer by Riz as an example.
I would prefer radio buttons, but you can do something like this:
public void CheckACheckBox(Checkbox ck)
{
foreach (Control ckb in this.Controls)
{
if ((ckb is CheckBox) && (ckb == ck))
ck.Checked = true;
else
ck.Checked = false;
}
}
List<CheckBox> groupOfCheckBoxes = new List<CheckBox>();
void InitFunction() {
groupOfCheckBoxes.Add(checkbox1);
groupOfCheckBoxes.Add(checkbox2);
groupOfCheckBoxes.Add(checkbox3);
foreach (CheckBox cb in groupOfCheckBoxes)
cb.Click += checkbox_Click
}
void checkbox_Click(object sender, EventArgs e)
{
foreach (CheckBox cb in groupOfCheckBoxes) {
cb.IsChecked = cb == sender;
}
}
However I would suggest radio boxes as well.
The code above is untested and may have some typos
Better version, allows the user to uncheck boxes.
private void chkOne_CheckedChanged(object sender, EventArgs e)
{
if (chkTwo.Checked == true)
{
chkTwo.Checked = !chkOne.Checked;
}
}
private void chkTwo_CheckedChanged(object sender, EventArgs e)
{
if (chkOne.Checked == true)
{
chkOne.Checked = !chkTwo.Checked;
}
}
The best option and easiest way for me was to create a Click event instead of a CheckedChanged event.
This method works perfectly with two or more checkbox and allows to have them all unchecked.
private void chkOne_Click(object sender, EventArgs e)
{
chkTwo.Checked = false;
}
private void chkTwo_Click(object sender, EventArgs e)
{
chkOne.Checked = false;
}
This will work for two unchecked boxes, since they are already unchecked it is simpler.
I had to do this myself also.
private void customer_IsCheckedChanged(object sender, EventArgs e)
{
business.IsChecked = false;
}
private void business_IsCheckedChanged(object sender, EventArgs e)
{
customer.IsChecked = false;
}
I needed to show or not show a table when activating the CheckBox, how they were two, if I activated both, everything was fine, but if I tried to deactivate one later, the other was also deactivated. PD: The default value for the tables that I used was Visible=false. The solution I got was the following:
protected void YourNameOfMethodForBothCheckBox(object sender, EventArgs e)
{
if (CheckBox_1.Checked == true)
{
Table_1.Visible = true;
if (CheckBox_2.Checked == true)
{
Table_2.Visible = true;
}
else { Table_2.Visible = false; }
}
else
{
Table_1.Visible = false;
if (CheckBox_2.Checked == false)
{
Table_2.Visible = false;
}
else
{
Table_2.Visible = true;
}
}
}
I need to implement a check box to switch between two methods enabling/disabling some control. I am using the following code, I tried also in other ways but no luck.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)//this is working
{
trackBar2.Enabled = false;
button3.PerformClick();
textBox8.Enabled = true;
}
else// this is supposed to work if checkbox is unchecked but doesn't work
{
trackBar2.Enabled = true;
textBox8.Enabled = false;
}
}
The result I get is always the same. If I check the checkbox the first condition is meet and it is fine. If I uncheck the text box, nothing happen and does not go back to the first condition either.
How can I detect the checked/unchecked condition?
You could also write that as:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
trackBar2.Enabled = !checkBox1.Checked;
textBox8.Enabled = checkBox1.Checked;
if (checkBox1.Checked)
{
button3.PerformClick();
}
}
I think you should add if(checkBox1.Checked == false) to the else :
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)//this is working
{
trackBar2.Enabled = false;
button3.PerformClick();
textBox8.Enabled = true;
}
else if(checkBox1.Checked == false)
{
trackBar2.Enabled = true;
textBox8.Enabled = false;
}
}
Looking for some help on a problem Im having
sorry if this question has already been asked, I can not find anything similar.
The idea is when a picturebox is clicked changed the image to ON.
If the picture box is held for more than 2 seconds to open a new form and leave the picturebox as OFF.
However if the picturebox is clicked ON and then held for 2 seconds and then returns i need the picturebox state to remain ON.
Here is what I have tried so far.
I believe for this to work correctly I need to stop MouseUp event from occuring.
Is there a way I can stop MouseUp when Tick occurs?
Is there a easier / better way to do this?
Any help would be appreciated.
private void time_HoldDownInternal_Tick(object sender, EventArgs e)
{
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
form1show.Visible = true;
}
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
time_HoldDownInternal.Enabled = true;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
//MessageBox.Show("mouse up");
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
}
private void pb_pictureBoxTest_Click(object sender, EventArgs e)
{
if (mainMenuVariables.mousedown == true)
{
if (mainMenuVariables.pictureBox == false)
{
mainMenuVariables.pictureBox = true;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOn);
return;
}
if (mainMenuVariables.pictureBox == true)
{
mainMenuVariables.pictureBox = false;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOff);
return;
}
}
if (mainMenuVariables.mousedown == false)
{
//nothing
}
}
Rather than starting a timer, just record the current time on mouse down. Then in mouse up, check if it has been 2 seconds. e.g:
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
mainMenuVariables.mousedowntime = DateTime.Now;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
var clickDuration = DateTime.Now - mainMenuVariables.mousedowntime;
if ( clickDuration > TimeSpan.FromSeconds(2))
{
// Do 'hold' logic (e.g. open dialog, etc)
}
else
{
// Do normal click logic (e.g. toggle 'On'/'Off' image)
}
}
I have a button on which a click and it takes a screenshot which i display in my Picture Box. I dont face issue with this code:
private void btnScreenShot_Click(object sender, EventArgs e)
{
btnSave.Visible = true;
sendto_bmpbox.Image = CaptureScreen();
}
However when i loop the entire Form freezes and i cannot click on anything:
private void btnScreenShot_Click(object sender, EventArgs e)
{
// Freezes here
btnSave.Visible = true;
while(flag == 0)
{
sendto_bmpbox.Image = CaptureScreen();
}
}
How do i fix this problem?
That's because your while() is infinite. What makes flag change from capture to capture?
In case you want to infinitely capture the screen - never use the main thread for such things, as it will cause it to hang and prevent your application from updating the UI.
Use the BackgroundWorker class for things like that, you can use this example.
private void button1_Click(object sender, EventArgs e)
{
btnSave.Visible = true;
Thread thread = new Thread(new ThreadStart(threadWork));
thread.Start();
}
int flag = 0;
private void threadWork()
{
while (flag == 0)
{
UpdateImage();
}
}
private void UpdateImage()
{
if (this.InvokeRequired)
{
this.Invoke(UpdateImage);
}
else
{
sendto_bmpbox.Image = CaptureScreen();
}
}
Try Application.DoEvents in loop. I think this can help you...
private void btnScreenShot_Click(object sender, EventArgs e)
{
// Freezes here
btnSave.Visible = true;
while(flag == 0)
{
Application.DoEvents();
sendto_bmpbox.Image = CaptureScreen();
}
}