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);
...
}
Related
I am having a problem . I want to use if statement to check if a button is clicked. For Example:
public void button1_Click(object sender, EventArgs e)
{
while (1)
{
...
...
...
if (Button2 == clicked)
{
break;
}
}
}
But it's not working like this, because the ".click" can only be on the left side of "+=" or "-=". Any idea how i can check if Button2 is clicked?
the code is loking like this: and i want to check button2 to stop the "programm".
the check for the Button2 is nearly at the end of the code ;)
public void button1_Click(object sender, EventArgs e)
{
Random rnd = new Random();
int EmFilterPos;
int ExFilterPos;
string String1;
int[] EmLB = new int[126];
int[] ExLB = new int[126];
int LBEmAnzahl = 0;
int LBEmTot = 0;
int LBExAnzahl = 0;
int LBExTot = 0;
UInt32 C_Zyklen;
UInt32 Zyklen;
Roche.DetectionControl2.Device_Filterwheels.ELBPowerState LB_On = Roche.DetectionControl2.Device_Filterwheels.ELBPowerState.LBOn;
Roche.DetectionControl2.Device_Filterwheels.ELBPowerState LB_Off = Roche.DetectionControl2.Device_Filterwheels.ELBPowerState.LBOff;
Roche.DetectionControl2.Device_Filterwheels.fiweGetLBResponse LightBarrier;
string Text = String.Format("Filterrad-Dauertest\r\nGestart am {0:d} um {0:t}\r\n\r\n", DateTime.Now);
System.IO.File.WriteAllText(#"TestLogFile\Filterrad_Dauertest1.txt", Text);
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweInitFilter();
System.Threading.Thread.Sleep(50);
while (Zyklen <= 20)
{
for (int q=1;q<8;q++)
{
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweMove(q,q);
System.Threading.Thread.Sleep(50);
Zyklen++;
}
for (int w=0;w<7;w++)
{
ExFilterPos = rnd.Next(1,8);
EmFilterPos = rnd.Next(1,8);
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweMove(ExFilterPos,EmFilterPos);
System.Threading.Thread.Sleep(50);
Zyklen++;
}
C_Zyklen = Zyklen;
if ((C_Zyklen % 2) < 14)
{
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweInitFilter();
System.Threading.Thread.Sleep(50);
using (System.IO.StreamWriter file = new System.IO.StreamWriter (#"TestLogFile\Filterrad_Dauertest1.txt", true))
{
file.Write("Init bei: ");
String1 = String.Format("{0,7}",Zyklen);
file.Write(String1);
file.Write(file.NewLine);
}
ExFilterPos = 60;
EmFilterPos = 60;
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweRawMove(ExFilterPos,EmFilterPos);
System.Threading.Thread.Sleep(50);
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweSetLB(LB_On);
while (EmFilterPos != -60)
{
LightBarrier = Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweGetLB();
if (LightBarrier.LBEm == Roche.DetectionControl2.Device_Filterwheels.ELBState.LBbright)
{
LBEmAnzahl++;
LBEmTot += EmFilterPos;
}
if (LightBarrier.LBEx == Roche.DetectionControl2.Device_Filterwheels.ELBState.LBbright)
{
LBExAnzahl++;
LBExTot += ExFilterPos;
}
ExFilterPos--;
EmFilterPos--;
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweRawMove(ExFilterPos,EmFilterPos);
}
EmFilterPos = LBEmTot / LBEmAnzahl;
ExFilterPos = LBExTot / LBExAnzahl;
using (System.IO.StreamWriter file = new System.IO.StreamWriter (#"TestLogFile\Filterrad_Dauertest1.txt", true))
{
file.Write("Nullstelle Mittelposition Em-Filter: ");
file.Write(EmFilterPos);
file.Write(file.NewLine);
file.Write("Nullstelle Mittelposition Ex-Filter: ");
file.Write(ExFilterPos);
file.Write(file.NewLine);
file.Write(file.NewLine);
}
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweSetLB(LB_Off);
}
if (Button2 == clicked) // or something like this
break;
}
using (System.IO.StreamWriter file = new System.IO.StreamWriter (#"TestLogFile\Filterrad_Dauertest1.txt", true))
{
file.Write("Beendet am {0:d} um {0:t}\r\n", DateTime.Now);
}*/
}
Hm...
bool b1clicked = false, b2clicked = false;
public void button2_Click(object sender, EventArgs e)
{
b2clicked = true;
}
public void button1_Click(object sender, EventArgs e)
{
b1clicked = true;
if (b1clicked && b2clicked)
{
//...
}
}
Beside the weird behavior you want..and since you are not using Threads, you have the following options:
Local functions (.Net > 4.7)
private void B_Click(object sender, EventArgs e)
{
bool clickFlag = false;
void Click(object sender2, EventArgs e2)
{
clickFlag = true;
}
b2.Click += Click;
while (!clickFlag)
{
Thread.Sleep(1);
}
b2.Click -= Click;
//Continue with your stuff
}
Threads
Thread newThread;
private void Button1_Click()
{
newThread = new Thread(YourBreakableProcess);
newThread.Start();
}
private void Button2_Click()
{
newThread.Join();
}
private void YourBreakableProcess()
{
//Your breakable process
}
Async methods.
I hope you find a solution. Cheers.
Edit:
Since what you want is to interrupt the process of whatever you are doing, the only option you have is Local fuctions as shown above, if you are not tied to a specific framework version.
BackgroundWorker and check in every step if the button 2 was pressed with the flag thing mentioned in other answer.
Threads, and make a thread.Join when the button 2 is pressed.
Edit 2:
Updated answer with Threads, I will recommend that if you go with this option it is much better to use a BackgroundWorker instead as you will have the whole control of the process breaking it only in the place where it would be fine to break it.
You can achieve this using a flag variable. Declare and initialize flag value to false.On button2 click change flag value to true as follows,
private bool flag= false;
private void button2_Click(object sender, EventArgs e)
{
flag= true;
}
public void button1_Click(object sender, EventArgs e)
{
//Use flag to check whether button 2 has clicked or not
if (flag)
{
}
else
{
}
}
I have an application for kiosk machine that appears always on the top and fullscreen. Also, I have to turn off explorer.exe.
Therefore, I will not be able to access anything without a keyboard.
I'm thinking to make gestures or invincible buttons so that I can turn on explorer.exe without keyboard.
I would like to know if there is a way to detect if two buttons are clicked at the same time. I've tried using the following code but it is not working.
PS: I can't debug it line by line as my PC do not have touchscreen.
Therefore, I cannot find out which line causes the problem.
private bool button1WasClicked = false;
private bool button2WasClicked = false;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
button1WasClicked = true;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
button1WasClicked = false;
}
private void button2_MouseUp(object sender, MouseEventArgs e)
{
button2WasClicked = false;
}
private void button2_MouseDown(object sender, MouseEventArgs e)
{
if (button1WasClicked == true)
{
Process.Start(Path.Combine(Environment.GetEnvironmentVariable("windir"), "explorer.exe"));
Application.Exit();
button1WasClicked = false;
}
}
You can't click two buttons at once with a mouse or keyboard, and if you're talking about using a touchscreen, the WinForms framework doesn't support them (taps will simply be interpreted as individual mouse clicks at best). You'll want to look at using the Surface SDK or something else instead.
I've found a different solution where the buttons(panels) have to be clicked in a certain sequence to achieve what I wanted. I've also added a timer. Below is my code.
private bool panel1WasClicked = false;
private bool panel2WasClicked = false;
int second = 0;
private void panel1_Click(object sender, EventArgs e)
{
MaintenanceTimer.Interval = 500;
MaintenanceTimer.Start();
second = 0;
if (panel1WasClicked == false)
{
panel1WasClicked = true;
}
else
{
panel1WasClicked = false;
}
}
private void panel2_Click(object sender, EventArgs e)
{
if (panel2WasClicked == false && panel1WasClicked == true)
{
panel2WasClicked = true;
}
else
{
panel2WasClicked = false;
}
}
private void panel3_Click(object sender, EventArgs e)
{
if (panel1WasClicked && panel2WasClicked == true)
{
//Do something
}
panel1WasClicked = false;
panel2WasClicked = false;
MaintenanceTimer.Stop();
}
private void MaintenanceTimer_Tick(object sender, EventArgs e)
{
second += 1;
if (second >= 5)
{
MaintenanceTimer.Stop();
second = 0;
panel1WasClicked = false;
panel2WasClicked = false;
}
}
I am trying to create a program where a user can color cells inside of a datagridview, then save the datagridview as an image. However, I am running into a problem with scope when I generate my datagridview the way I need to.
I can't think of a good (and quick) way of restructuring this in a way that makes sense. How can I avoid my datagridview from going out of scope in this context?
Thanks!
public partial class Form2 : Form
{
bool erasing = false;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
dataGridView1.ColumnCount = Properties.Settings.Default.Width;
dataGridView1.RowCount = Properties.Settings.Default.Height;
dataGridView1.RowHeadersVisible = false;
dataGridView1.ColumnHeadersVisible = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dataGridView1.AutoSize = true;
dataGridView1.ClearSelection();
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
}
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (erasing)
{
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
}
else
{
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Black;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
erasing = true;
}
else
{
erasing = false;
}
}
private void savegviewImg()
{
Bitmap bmap = new Bitmap(DataGridView1.Bounds.Width, DataGridView1.Bounds.Height);
DataGridView1.DrawToBitmap(bmap, new Rectangle(1, 1, DataGridView1.Width, DataGridView1.Height));
bmap.Save("C:\\HelpMehStackOFlowYoureMyOnlyHope.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
It should be dataGridView1 and not DataGridView1 in the savegviewImg() method, no? That's how you refer to it everywhere else.
I have 5 Ellipse Circle with a color red, the user will select one at a time. now i want to change the selected ellipse to green after i have saved. how do i do that?
private void Right1_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
nfingerIndex = 1;
}
private void Right2_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
nfingerIndex = 2;
}
private void Right3_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
nfingerIndex = 3;
}
private void Right4_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
nfingerIndex = 4;
}
private void Right5_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
nfingerIndex = 5;
}
Ellipses dont have Background properties. They have Fill properties and you can change them with using this code.
private void Right1_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
nfingerIndex = 1;
((Ellipse)sender).Fill = new SolidColorBrush(Colors.Green);
}
this code will change the Fill property of the Ellipse which is clicked.
And if i'm not wrong, you have added a Click event to Ellipses. Ellipses dont have Click property. You can try MouseLeftButtonUp event.
This is the solution to my problem. i managed to do it like this
private void invokeControlColour(int FingerIndex)
{
try
{
switch (FingerIndex)
{
case 1:
{
Right1.Fill = Brushes.Green;
break;
}
case 2:
{
Right2.Fill = Brushes.Green;
break;
}
case 3:
{
Right3.Fill = Brushes.Green;
break;
}
case 4:
{
Right4.Fill = Brushes.Green;
break;
}
case 5:
{
Right5.Fill = Brushes.Green;
break;
}
and the invoke it like this
Dispatcher.Invoke(new Action(() => invokeControlColour(nfingerIndex)));
Something like this:
private void secondTabPageInTabControl_Click(object sender, System.EventArgs e)
{
this.myTreeView.Enable = false;
} //then I chose other tabpages and it becomes enable
Use the TabControl's SelectedIndexChanged event instead. It tells you when a new tab is selected. Use code similar to this:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
this.myTreeView.Enable = tabControl1.SelectedTab != tabPage2;
}
Or by index, less readable:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
this.myTreeView.Enable = tabControl1.SelectedIndex != 1;
}
Another Solution, but note that this could become bloated if you use too many tabpages:
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
UpdateUI(e.TabPageIndex);
}
public void UpdateUI(int index)
{
switch (index)
{
case 0:
treeView1.Enabled = true;
break;
case 1:
treeView1.Enabled = false;
break;
case 2:
treeView1.Enabled = false;
break;
default:
treeView1.Enabled = false;
break;
}
}