How to stop shuffle of images using timers in C# - c#

I'm creating a simple slot machine. One button for the start, and three stop buttons on each tile of a Picturebox. My problem is every time I click each of the stop buttons, the Picturebox won't stop.
I need help in which, if I click the stop button on a corresponding Picturebox, it will stop and the two will continue to shuffle pictures. Then if I clicked the other stop button, another will stop and so on.
Here's what I have for now:
namespace SlotMachine
{
class SlotMac
{
private Form f;
Button btn1 = new Button(); // First stop
Button btn2 = new Button(); // Second stop
Button btn3 = new Button(); // Third stop
Button btn4 = new Button(); // Start
Timer Clock; // Tick
Timer Clock1; // Tick
Timer Clock2; // Tick
Int32 tick = 0;
public SlotMac()
{
f = new Form();
f.Text = "Slot Machine";
//f.Size = new Size(800, 700);
f.FormBorderStyle = FormBorderStyle.FixedSingle;
}
PictureBox[] pics = new PictureBox[7];
PictureBox pb = new PictureBox();
public void Launch()
{
int i = 0;
Clock = new Timer();
Clock.Interval = 1000;
Clock.Tick += new EventHandler(Clock_Tick);
Clock1 = new Timer();
Clock1.Interval = 1000;
Clock1.Tick += new EventHandler(Clock1_Tick);
Clock2 = new Timer();
Clock2.Interval = 1000;
Clock2.Tick += new EventHandler(Clock2_Tick);
int x = 50;
for (i = 0; i < pics.Length; i++)
{
pics[i] = new PictureBox();
pics[i].Image = Image.FromFile(i+".jpg");
pics[i].SetBounds(x, 100, 100, 100);
x += 150;
f.Controls.Add(pics[i]);
}
f.SetBounds(10, 20, 500, 500);
// STOP
btn1.Location = new Point(50, 250);
btn1.Height = 40;
btn1.Width = 100;
f.Controls.Add(btn1);
btn1.Text = "STOP";
this.btn1.Click += new EventHandler(this.MyButtonClick);
// STOP
btn2.Location = new Point(200, 250);
btn2.Height = 40;
btn2.Width = 100;
btn2.Text = "STOP";
f.Controls.Add(btn2);
this.btn2.Click += new EventHandler(this.MyButtonClick);
// STOP
btn3.Location = new Point(350, 250);
btn3.Height = 40;
btn3.Width = 100;
btn3.Text = "STOP";
f.Controls.Add(btn3);
this.btn3.Click += new EventHandler(this.MyButtonClick);
// START
btn4.Location = new Point(200, 370);
btn4.Height = 40;
btn4.Width = 100;
btn4.Text = "START";
f.Controls.Add(btn4);
this.btn4.Click += new EventHandler(btn4_Click);
f.ShowDialog();
}
public void Stop_Click(object sender, EventArgs e)
{
}
public void Clock_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
pics[0].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[1].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[2].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[3].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[4].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[5].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[6].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
}
public void Clock1_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
pics[0].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[1].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[2].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[3].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[4].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[5].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[6].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
}
public void Clock2_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
pics[0].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[1].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[2].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[3].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[4].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[5].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[6].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
}
public void MyButtonClick(object sender, EventArgs e)
{
// I am having troubles in this part
if (sender == btn1)
{
Clock.Stop();
}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
}
public void btn4_Click(object sender, EventArgs e)
{
Clock.Start();
Clock1.Start();
Clock2.Start();
}
}
}

Each ticker sets all of the seven images in pics. It won't help to stop one of them if the others still update all of the pictures.

I think you need to create a separate PictureBox arrays for each clock. Otherwise, the PictureBox for what is getting shown will continually reflect that changes made by the other two timers.
Basically, if they are all based on the same array, then when the other clocks change the array, it doesn't matter if the first clock changes it or not; it still gets changed.

Related

Change image in picturebox array by button click

I want to generate and change the image in the PictureBox array by button click.
Here is what I have tried so far.
public Form1() {
InitializeComponent();
}
PictureBox[] boxes = new PictureBox[6];
private void GenerateButton1_Click(object sender, EventArgs e) {
for (int i = 0; i < boxes.Length; i++) {
boxes[i] = new PictureBox(); //set the pointer to a new PictureBox instance
if (i == 0) boxes[i].Location = new System.Drawing.Point(3, 3);
if (i == 1) boxes[i].Location = new System.Drawing.Point(221, 3);
if (i == 2) boxes[i].Location = new System.Drawing.Point(439, 3);
if (i == 3) boxes[i].Location = new System.Drawing.Point(3, 210);
if (i == 4) boxes[i].Location = new System.Drawing.Point(221, 210);
if (i == 5) boxes[i].Location = new System.Drawing.Point(439, 210);
boxes[i].Size = new System.Drawing.Size(200, 200);
boxes[i].Image = Image.FromFile(Application.StartupPath + "\\red.PNG"); //for setting its image
}
this.Controls.AddRange(boxes);
}
private void button1_Click(object sender, EventArgs e) {
int i = 3;
int signal = 1;
boxes[i].SizeMode = PictureBoxSizeMode.StretchImage;
if (signal == 0) boxes[i].Image = Image.FromFile(Application.StartupPath + "\\red.PNG");
if (signal == 1) boxes[i].Image = Image.FromFile(Application.StartupPath + "\\green.PNG");
if (signal == 2) boxes[i].Image = Image.FromFile(Application.StartupPath + "\\grey.PNG");
}
}
}
by doing button1 click, the image will change from red.PNG to green.PNG or grey.PNG depending on the condition, however, I have to redo the image Properties declaration, for example, boxes[i].SizeMode = PictureBoxSizeMode.StretchImage;. Otherwise the PictureBox lose its properties.
Is there any simpler way to do this.
Thank you in advance.

Delete 2 Dynamic Buttons from one Dynamic Button

So I have an Image Map and on it I want to appear 3 buttons each time I clicked on a location. Those 3 button would be : hotspot, delete hotspot, save hotspot. These Buttons are Dynamically generated. The question is, how can I the hotspot from delete hotspot Button and also close the other 2 Buttons.
Some code for a little bit of understanding of what am I doing:
private void PictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//Locatia
PictureBox C = new PictureBox();
int i = 0;
C.Location = new Point(e.X-13, e.Y-30);
C.Name = "Problema_" + (i + 1).ToString();
C.ImageLocation = #"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_GPS_500px.png";
C.Size = new Size(26, 30);
C.SizeMode = PictureBoxSizeMode.StretchImage;
C.BackColor = Color.Transparent;
C.Cursor = Cursors.Hand;
// C.Click += new EventHandler(this.StartRecordingToolStripMenuItem_Click_1);;
PictureBox1.Controls.Add(C);
//salveaza Locatia
PictureBox S = new PictureBox();
S.Name = "Salveaza_" + (i + 1).ToString();
S.Location = new Point(e.X - 45, e.Y+10);
S.ImageLocation = #"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_Checked_Checkbox_500px.png";
S.Size = new Size(35, 35);
S.SizeMode = PictureBoxSizeMode.StretchImage;
S.BackColor = Color.Transparent;
S.Cursor = Cursors.Hand;
PictureBox1.Controls.Add(S);
//sterge Locatia
PictureBox St = new PictureBox();
St.Name = "Sterge_" + (i + 1).ToString();
St.Location = new Point(e.X +10, e.Y+10);
St.Cursor = Cursors.Hand;
St.ImageLocation = #"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_Close_Window_500px.png";
St.Size = new Size(35, 35);
St.SizeMode = PictureBoxSizeMode.StretchImage;
St.BackColor = Color.Transparent;
PictureBox1.Controls.Add(St);
S.Click += new EventHandler(this.stergeAprob);
C.Click += new EventHandler(this.clickHotspot);
}
The solution could be that when creating dynamically you also delete it dynamically using anonymous functions
S.Click += (o, e) => {
//....actions
PictureBox1.Controls.remove(C)
//... other actions
}
we can have access to dynamically created variables since the compiler creates inline functions

C# winforms dynamically created button controls

I have an add user button which dynamically adds a remove button and a username textbox when the user clicks it. The user can click the button as many times as they like and the controls will continue to add.
I am having trouble with the remove button that is created dynamically. It should removed the itself and the username textbox next to it. Instead it will always remove the top row that was added. Also when you click add a new user after you have clicked remove it doesn't automatically fill the blank space - it moves the new textbox and button to the bottom line.
Here is my code:
private void AddUserbtn_Click_1(object sender, EventArgs e)
{
TextBox[] Username = new TextBox[n];
Button[] Remove = new Button[n];
int UsernameX, UsernameY, RemoveX, RemoveY;
UsernameX = 346;
UsernameY = 45;
RemoveX = 946;
RemoveY = 45;
for (int i = 0; i < n; i++)
{
Username[i] = new TextBox();
Username[i].Size = new Size(233, 26);
Username[i].Location = new Point(UsernameX, UsernameY + space);
Username[i].Font = new Font("Arial", 10);
Username[i].Name = "Username" ;
Remove[i] = new Button();
Remove[i].Location = new Point(RemoveX, RemoveY + space);
Remove[i].Text = "Remove";
Remove[i].Font = new Font("Arial", 10);
Remove[i].Size = new Size(95, 23);
Remove[i].UseVisualStyleBackColor = true;
Remove[i].Click += new EventHandler(Remove_Click);
Remove[i].Name = "Remove";
space += 35;
}
for (int i = 0; i < n; i++)
{
CaeUsersPanel.Controls.Add(Username[i]);
CaeUsersPanel.Controls.Add(Remove[i]);
}
}
private void Remove_Click(object sender, EventArgs e)
{
CaeUsersPanel.Controls.Remove(CaeUsersPanel.Controls[("Username")]);
CaeUsersPanel.Controls.Remove(CaeUsersPanel.Controls[("Remove")]);
}
Put a class variant to store Button - TextBox pair, and delete respectively. Quick tested code below - not fine tuned for memory leak / logic etc, just a sample code that fulfills your requirement.
Dictionary<Button, TextBox> pair = new Dictionary<Button, TextBox>(); // A dictionary to store the Button - TextBox pair
private void button1_Click(object sender, EventArgs e) {
int n = 3; // Added for testing
int space = 0; // Added for testing
int UsernameX, UsernameY, RemoveX, RemoveY;
UsernameX = 100; // Modified for testing
UsernameY = 45;
RemoveX = 400; // Modified for testing
RemoveY = 45;
for (int i = 0; i < n; i++) {
var Username = new TextBox();
Username.Size = new Size(233, 26);
Username.Location = new Point(UsernameX, UsernameY + space);
Username.Font = new Font("Arial", 10);
Username.Name = "Username";
var Remove = new Button();
Remove.Location = new Point(RemoveX, RemoveY + space);
Remove.Text = "Remove";
Remove.Font = new Font("Arial", 10);
Remove.Size = new Size(95, 23);
Remove.UseVisualStyleBackColor = true;
Remove.Click += new EventHandler(Remove_Click);
Remove.Name = "Remove";
Controls.Add(Username);
Controls.Add(Remove);
pair.Add(Remove, Username);
space += 35;
}
}
private void Remove_Click(object sender, EventArgs e) {
Controls.Remove((Button)sender); // Removes the delete button
Controls.Remove(pair[(Button)sender]); // Removes the textbox
pair.Remove((Button)sender); // Removes the entry in dictionary
}
Update: Another version for better memory and logic matters. Also shifts up the rest of controls up if it's the OP's desire.
int n = 3; // Added for testing, probably you already have it somewhere
int space = 0; // Added for testing, probably you already have it somewhere
// Moved for logic
// Value modified for testing
int UsernameX = 100;
int UsernameY = 45;
int RemoveX = 400;
int RemoveY = 45;
int SpaceDelta = 35; // Added for logic
List<Button> RemoveButtons = new List<Button>();
List<TextBox> UsernameTextBoxes = new List<TextBox>();
private void button1_Click(object sender, EventArgs e) {
Random rnd = new Random((int)DateTime.Now.Ticks); // Added for testing
for (int i = 0; i < n; i++) {
var Username = new TextBox();
Username.Size = new Size(233, 26);
Username.Location = new Point(UsernameX, UsernameY + space);
Username.Font = new Font("Arial", 10);
Username.Name = "Username";
Username.Text = $"{(int)(rnd.NextDouble() * 100000)}"; // Added for testing
var Remove = new Button();
Remove.Location = new Point(RemoveX, RemoveY + space);
Remove.Text = $"{(int)(rnd.NextDouble() * 100000)}"; // Modified for testing
Remove.Font = new Font("Arial", 10);
Remove.Size = new Size(95, 23);
Remove.UseVisualStyleBackColor = true;
Remove.Click += new EventHandler(Remove_Click);
Remove.Name = "Remove";
Controls.Add(Username);
Controls.Add(Remove);
RemoveButtons.Add(Remove);
UsernameTextBoxes.Add(Username);
space += SpaceDelta;
}
}
private void Remove_Click(object sender, EventArgs e) {
int idx = RemoveButtons.IndexOf((Button)sender);
// Remove button
RemoveButtons[idx].Dispose();
RemoveButtons.RemoveAt(idx);
// Remove textbox
UsernameTextBoxes[idx].Dispose();
UsernameTextBoxes.RemoveAt(idx);
// Shift controls up
for (int i = idx; i < RemoveButtons.Count; i ++) {
RemoveButtons[i].Top -= SpaceDelta;
UsernameTextBoxes[i].Top -= SpaceDelta;
}
space -= SpaceDelta;
}
Remove the two last created :
private void Remove_Click(object sender, EventArgs e)
{
this.Controls.Remove(this.Controls[this.Controls.Count - 1]);
this.Controls.Remove(this.Controls[this.Controls.Count - 1]);
}

getting ffmpeg.dll failed to load message in my c# video application?

I developed a Winform application which has a Panel as Main Screen and two other panels on each side for previous and next video. and Two buttons which helps the Application to traverse through different videos and set it to the main panel. I have 21 videos now......
This is My code....
public void loadvideo2(int a)
{
int width = viewscreen.Width;
int height = viewscreen.Height;
int width1 = nxtpnl.Width;
int height1 = nxtpnl.Height;
int width2 = prepnl.Width;
int height2 = prepnl.Height;
video = new Video(vpath[a]);
video.Owner = viewscreen;
video.Stop();
viewscreen.Size = new Size(width, height);
video1 = new Video(vpath[a + 1]);
video1.Owner = nxtpnl;
video1.Stop();
nxtpnl.Size = new Size(width1, height1);
video2 = new Video(vpath[a - 1]);
video2.Owner = prepnl;
video2.Stop();
prepnl.Size = new Size(width2, height2);
plystpBtn.BackgroundImage = Video_Project.Properties.Resources.Style_Play_icon__1_;
plystpBtn.BackgroundImageLayout = ImageLayout.Stretch;
trckstatus.Minimum = Convert.ToInt32(video.CurrentPosition);
trckstatus.Maximum = Convert.ToInt32(video.Duration);
duration = CalculateTime(video.Duration);
playposition = "0:00:00";
posdurtrclbl.Text = playposition + "/" + duration;
b = a;
vlbl.Text = "Video" + Convert.ToString(b);
}
private void preBtn_Click(object sender, EventArgs e)
{
videono += 1;
if (videono <= vcount-1)
{
loadvideo2(videono);
}
else
MessageBox.Show("File Not Found!!!");
}
private void nxtBtn_Click(object sender, EventArgs e)
{
videono -= 1;
if (videono >= 0)
{
loadvideo2(videono);
}
else
MessageBox.Show("FIle Not Found!!!");
}
now while i am traversing through the videos by pressing buttons its working fine till the 16th video where i am getting an error message
ffmpeg.dll failed to load
can any one help me to solve this
solved it. It was probably a memory consumption issue.
public void loadvideo2(int a)
{
int width = viewscreen.Width;
int height = viewscreen.Height;
int width1 = nxtpnl.Width;
int height1 = nxtpnl.Height;
int width2 = prepnl.Width;
int height2 = prepnl.Height;
video.Dispose();
video = new Video(vpath[a]);
video.Owner = viewscreen;
video.Stop();
viewscreen.Size = new Size(width, height);
video1 = new Video(vpath[a + 1]);
video1.Owner = nxtpnl;
video1.Stop();
nxtpnl.Size = new Size(width1, height1);
video2 = new Video(vpath[a - 1]);
video2.Owner = prepnl;
video2.Stop();
prepnl.Size = new Size(width2, height2);
plystpBtn.BackgroundImage = Video_Project.Properties.Resources.Style_Play_icon__1_;
plystpBtn.BackgroundImageLayout = ImageLayout.Stretch;
trckstatus.Minimum = Convert.ToInt32(video.CurrentPosition);
trckstatus.Maximum = Convert.ToInt32(video.Duration);
duration = CalculateTime(video.Duration);
playposition = "0:00:00";
posdurtrclbl.Text = playposition + "/" + duration;
b = a;
vlbl.Text = "Video" + Convert.ToString(b);
video1.Dispose();
video2.Dispose();
}

How to enable a start button after clicking stop buttons

I want to enable my start button after clicking all the three stop buttons.
I tried to place the button btn4.enabled = false inside the (sender == btn3), but the start button will be enabled if I first clicked on that button.
The three stop buttons can be clicked in random order.
Here's my code so far:
namespace SlotMachine
{
class SlotMac
{
private Form f;
Button btn1 = new Button(); // First stop
Button btn2 = new Button(); // Second stop
Button btn3 = new Button(); // Third stop
Button btn4 = new Button(); // Start
Timer Clock; // Tick
Timer Clock1; // Tick
Timer Clock2; // Tick
Int32 tick = 0;
Label tb = new Label();
int[] nNum = new int[3];
public SlotMac()
{
f = new Form();
f.Text = "Slot Machine";
//f.Size = new Size(800, 700);
f.FormBorderStyle = FormBorderStyle.FixedSingle;
}
PictureBox[] pics = new PictureBox[7];
PictureBox[] pics1 = new PictureBox[7];
PictureBox[] pics2 = new PictureBox[7];
//PictureBox pb = new PictureBox();
public void Launch()
{
Random r = new Random();
int i = 0;
//int x = 0;
//int x = 50;
tb.Location = new Point(205,20);
f.Controls.Add(tb);
Clock = new Timer();
Clock.Interval = 800;
Clock.Tick += new EventHandler(Clock_Tick);
Clock1 = new Timer();
Clock1.Interval = 800;
Clock1.Tick += new EventHandler(Clock1_Tick);
Clock2 = new Timer();
Clock2.Interval = 800;
Clock2.Tick += new EventHandler(Clock2_Tick);
for (i = 0; i < pics.Length; i++)
{
pics[i] = new PictureBox();
pics[0].Image = Image.FromFile(i + ".jpg");
pics[i].SetBounds(50, 100, 100, 100);
//x += 150;
f.Controls.Add(pics[i]);
}
for (i = 0; i < pics1.Length; i++)
{
pics1[i] = new PictureBox();
pics1[i].Image = Image.FromFile(i + ".jpg");
pics1[i].SetBounds(200, 100, 100, 100);
//x += 50;
f.Controls.Add(pics1[i]);
}
for (i = 0; i < pics2.Length; i++)
{
pics2[i] = new PictureBox();
pics2[i].Image = Image.FromFile(i + ".jpg");
pics2[i].SetBounds(350, 100, 100, 100);
//x += 50;
f.Controls.Add(pics2[i]);
}
f.SetBounds(10, 20, 500, 500);
// STOP
btn1.Location = new Point(50, 250);
btn1.Height = 40;
btn1.Width = 100;
f.Controls.Add(btn1);
btn1.Text = "STOP";
this.btn1.Click += new EventHandler(this.MyButtonClick);
// STOP
btn2.Location = new Point(200, 250);
btn2.Height = 40;
btn2.Width = 100;
btn2.Text = "STOP";
f.Controls.Add(btn2);
this.btn2.Click += new EventHandler(this.MyButtonClick);
// STOP
btn3.Location = new Point(350, 250);
btn3.Height = 40;
btn3.Width = 100;
btn3.Text = "STOP";
f.Controls.Add(btn3);
this.btn3.Click += new EventHandler(this.MyButtonClick);
// START
btn4.Location = new Point(200, 370);
btn4.Height = 40;
btn4.Width = 100;
btn4.Text = "START";
f.Controls.Add(btn4);
this.btn4.Click += new EventHandler(btn4_Click);
f.ShowDialog();
}
public void Clock_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
nNum[0] = r.Next(0, 6);
for (int i = 0; i < pics.Length; i++)
{
pics[0].Image = Image.FromFile(nNum[0] + ".jpg");
pics[1].Image = Image.FromFile(nNum[0] + ".jpg");
pics[2].Image = Image.FromFile(nNum[0] + ".jpg");
pics[3].Image = Image.FromFile(nNum[0] + ".jpg");
pics[4].Image = Image.FromFile(nNum[0] + ".jpg");
pics[5].Image = Image.FromFile(nNum[0] + ".jpg");
pics[6].Image = Image.FromFile(nNum[0] + ".jpg");
}
}
public void Clock1_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
nNum[1] = r.Next(0, 6);
for (int i = 0; i < pics.Length; i++)
{
pics1[0].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[1].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[2].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[3].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[4].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[5].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[6].Image = Image.FromFile(nNum[1] + ".jpg");
}
}
public void Clock2_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
nNum[2] = r.Next(0, 6);
for (int i = 0; i < pics.Length; i++)
{
pics2[0].Image = Image.FromFile(nNum[2] + ".jpg");
pics1[1].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[2].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[3].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[4].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[5].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[6].Image = Image.FromFile(nNum[1] + ".jpg");
}
}
public void MyButtonClick(object sender, EventArgs e)
{
if (sender == btn1)
{
Clock.Stop();
}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
Finish();
}
public void btn4_Click(object sender, EventArgs e)
{
Clock.Start();
Clock1.Start();
Clock2.Start();
btn4.Enabled = false;
}
public void Finish()
{
if (nNum[0] == nNum[1] && nNum[0] == nNum[2])
{
this.tb.Text = "Congratulations!";
}
}
}
}
You can check if all timers already stopped, like this:
public void MyButtonClick(object sender, EventArgs e)
{
if (sender == btn1)
{
Clock.Stop();
}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
Finish();
if(!Clock.Enabled && !Clock1.Enabled && !Clock2.Enabled)
btn4.Enabled = true;
}
Try this for method MyButtonClick:
public void MyButtonClick(object sender, EventArgs e)
{
if (sender == btn1)
{
Clock.Stop();
}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
Finish();
if (!Clock.Enabled && !Clock1.Enabled && !Clock2.Enabled) btn4.Enabled = true;
}
You could disable each button as they are clicked (which would make sense) and then after they are all disabled, enable the Start button.
if (sender == btn1)
{
Clock.Stop();
btn1.Enabled = false; // disable the buttons after they are clicked.
}
if (sender == btn2)
{
Clock1.Stop();
btn2.Enabled = false;
}
if (sender == btn3)
{
Clock2.Stop();
btn3.Enabled = false;
}
// If all buttons are disabled then enable the Start button.
if (!btn1.Enabled && !btn2.Enabled && !btn3.Enabled)
{
btn4.Enabled = true;
}
Finish();

Categories

Resources