How to use Click event in dynamic groupbox in C# - c#

I have a form like this. You can see my dynamic groupbox (30 groupbox) in Flowlayout on the right, and a picturebox to show questions on the left.
I'm worndering that How to use Click event for each groupbox to show the question equivalent in Picturebox?
Here's my code of groupbox
public GroupBox gbx(String name, int s, string i, string msch)
{
this.Name = int.Parse(i);
this.sda = s;
this.MsCauHoi = msch;
// gb
//
if (s == 2)
{
gb.Controls.Add(cb1);
gb.Controls.Add(cb2);
}
if (s == 3)
{
gb.Controls.Add(cb1);
gb.Controls.Add(cb2);
gb.Controls.Add(cb3);
}
if (s == 4)
{
gb.Controls.Add(cb1);
gb.Controls.Add(cb2);
gb.Controls.Add(cb3);
gb.Controls.Add(cb4);
}
gb.Location = new System.Drawing.Point(219, 44);
gb.Margin = new System.Windows.Forms.Padding(1, 1, 1, 1);
gb.Name = name;
gb.Padding = new System.Windows.Forms.Padding(1, 1, 1, 1);
gb.Size = new System.Drawing.Size(120, 45);
gb.TabIndex = 7;
gb.TabStop = false;
gb.Text = i;
gb.BackColor = Color.Silver;
//
// cb1
//
cb1.AutoSize = true;
cb1.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb1.Location = new System.Drawing.Point(15, 13);
cb1.Margin = new System.Windows.Forms.Padding(0);
cb1.Name = "cb1";
cb1.Size = new System.Drawing.Size(17, 31);
cb1.TabIndex = 0;
cb1.Text = "1";
cb1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb1.UseVisualStyleBackColor = true;
cb1.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
//
// cb2
//
cb2.AutoSize = true;
cb2.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb2.Location = new System.Drawing.Point(35, 13);
cb2.Name = "cb2";
cb2.Size = new System.Drawing.Size(17, 31);
cb2.TabIndex = 1;
cb2.Text = "2";
cb2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb2.UseVisualStyleBackColor = true;
cb2.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
//
// cb3
//
cb3.AutoSize = true;
cb3.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb3.Location = new System.Drawing.Point(58, 13);
cb3.Name = "cb3";
cb3.Size = new System.Drawing.Size(17, 31);
cb3.TabIndex = 2;
cb3.Text = "3";
cb3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb3.UseVisualStyleBackColor = true;
cb3.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
//
// cb4
//
cb4.AutoSize = true;
cb4.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb4.Location = new System.Drawing.Point(81, 13);
cb4.Name = "cb4";
cb4.Size = new System.Drawing.Size(17, 31);
cb4.TabIndex = 3;
cb4.Text = "4";
cb4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb4.UseVisualStyleBackColor = true;
cb4.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
return gb;
}
And here's the code of FormLoad
private void FrmThi_Load(object sender, EventArgs e)
{
droptable();
this.CauDaLam = 0;
if (dethi == null) setdethi();
Screen scr = Screen.PrimaryScreen; //đi lấy màn hình chính
this.Left = (scr.WorkingArea.Width - this.Width) / 2;
this.Top = (scr.WorkingArea.Height - this.Height) / 2;
int i = 0;
foreach (DataRow row in this.dethi.Rows)
{
String myValue = row["SoDA"].ToString();
String msch = row["MaCH"].ToString();
ptl = new FrmPhieuTraLoi();
pn_DeThi.Controls.Add(ptl.gbx("cau" + (i + 1).ToString(), int.Parse(myValue), (i + 1).ToString(), msch));
listptl.Add(ptl);
i++;
}
loadcauhoi(this.CauDangLam);
listptl[CauDangLam].setBackColorCDL();
Random r = new Random();
lbmade1.Text = r.Next(1, 4).ToString();
txt = lbSatHachBangLai.Text;
len = txt.Length;
lbSatHachBangLai.Text = "";
timer1.Start();
this.timer2.Start();
}

This is an example code:
private void Form1_Load(object sender, EventArgs e)
{
for(int idx = 0; idx < 5; idx++)
{
var gBox = new GroupBox();
gBox.Height = 50;
gBox.Width = 50;
gBox.Text = "Box: " + idx;
gBox.Tag = idx;
gBox.MouseClick += GBox_MouseClick;
this.Controls.Add(gBox);
}
}
private void GBox_MouseClick(object sender, MouseEventArgs e)
{
//var ctrl = sender as Control; -- Not required
int questionIdx = (int)(sender as Control).Tag;
}

I would extend the GroupBox control and add a property for the Question ID (Add new Item > Custom Control) and change the class to look like this:
public partial class QuestionGroupBox : GroupBox
{
public string QuestionID;
public QuestionGroupBox()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
Then populate your flowlayout and set the QuestionID and Click EventHandler, similar to this:
var gb = new QuestionGroupBox{ QuestionID = "44"};
gb.Click += new System.EventHandler(this.questionGroupBox_Click);
//add add to your flow layout
Finally create your event handler to get and display the question
private void questionGroupBox_Click(object sender, EventArgs e)
{
var questionID = ((QuestionGroupBox)sender).QuestionID;
//display your question
}

Related

Form size does not change

I have a window, which shows always smaller than in the designer. No matter how big it is in the designer and how big numbers I put in size in the properties, it always shows small. I tried to put maximum and minimum size, tried with autosize, autovalidate, autoscale options. Despite that, it was always small and the same size. I solved it by putting form.width and form.height during initialization. But I wonder what happened and how to prevent it in the future. It happened second time for me, and I do not know why. Any tips what to check more?
c# designer
namespace WindowsFormsApplication3
{
partial class Form1
{
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button1.Location = new System.Drawing.Point(77, 187);
this.button1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(200, 55);
this.button1.TabIndex = 0;
this.button1.Text = "Segment";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button2.Location = new System.Drawing.Point(77, 309);
this.button2.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(200, 55);
this.button2.TabIndex = 1;
this.button2.Text = "Triangle";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button3.Location = new System.Drawing.Point(77, 433);
this.button3.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(200, 55);
this.button3.TabIndex = 2;
this.button3.Text = "Quadrangle";
this.button3.UseVisualStyleBackColor = false;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button4.Location = new System.Drawing.Point(77, 559);
this.button4.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(200, 55);
this.button4.TabIndex = 3;
this.button4.Text = "Hexagon";
this.button4.UseVisualStyleBackColor = false;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.pictureBox1.Location = new System.Drawing.Point(101, 1027);
this.pictureBox1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(176, 119);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(320, 193);
this.label1.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 32);
this.label1.TabIndex = 5;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(320, 315);
this.label2.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(0, 32);
this.label2.TabIndex = 6;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(320, 439);
this.label3.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(0, 32);
this.label3.TabIndex = 7;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(320, 565);
this.label4.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(0, 32);
this.label4.TabIndex = 8;
//
// menuStrip1
//
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(40, 40);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(2419, 49);
this.menuStrip1.TabIndex = 10;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.newToolStripMenuItem,
this.saveToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(75, 45);
this.fileToolStripMenuItem.Text = "File";
//
// newToolStripMenuItem
//
this.newToolStripMenuItem.Name = "newToolStripMenuItem";
this.newToolStripMenuItem.Size = new System.Drawing.Size(194, 46);
this.newToolStripMenuItem.Text = "New";
this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);
//
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(194, 46);
this.saveToolStripMenuItem.Text = "Save";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 49);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.label7);
this.splitContainer1.Panel1.Controls.Add(this.label6);
this.splitContainer1.Panel1.Controls.Add(this.button4);
this.splitContainer1.Panel1.Controls.Add(this.button1);
this.splitContainer1.Panel1.Controls.Add(this.label4);
this.splitContainer1.Panel1.Controls.Add(this.button2);
this.splitContainer1.Panel1.Controls.Add(this.label3);
this.splitContainer1.Panel1.Controls.Add(this.button3);
this.splitContainer1.Panel1.Controls.Add(this.label2);
this.splitContainer1.Panel1.Controls.Add(this.pictureBox1);
this.splitContainer1.Panel1.Controls.Add(this.label1);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.pictureBox2);
this.splitContainer1.Size = new System.Drawing.Size(2419, 1402);
this.splitContainer1.SplitterDistance = 621;
this.splitContainer1.TabIndex = 11;
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(95, 927);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(309, 32);
this.label7.TabIndex = 10;
this.label7.Text = "A color for new shapes:";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(71, 98);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(120, 32);
this.label6.TabIndex = 9;
this.label6.Text = "Shapes:";
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.White;
this.pictureBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox2.Location = new System.Drawing.Point(0, 0);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(1794, 1402);
this.pictureBox2.TabIndex = 9;
this.pictureBox2.TabStop = false;
this.pictureBox2.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDoubleClick);
this.pictureBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDown);
this.pictureBox2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseMove);
this.pictureBox2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseUp);
this.pictureBox2.Resize += new System.EventHandler(this.pictureBox2_Resize);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(16F, 31F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoValidate = System.Windows.Forms.AutoValidate.EnablePreventFocusChange;
this.ClientSize = new System.Drawing.Size(2419, 1451);
this.Controls.Add(this.splitContainer1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.Name = "Form1";
this.Text = "Simple Paint";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
}
}
Form.cs deleted one function because i reached the limit.
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private bool isStrip = false;
private string NameString;
private string StorePath;
private int VertexCatch = -1;
private int button = -1;
private int checker = 0;
private int dblclck = -1;
private Bitmap graphic;
private Graphics g;
private Name NameForm;
private Point[] points;
private Pen p = new Pen(Color.Black);
private ColorDialog colorDialog1 = new ColorDialog();
private List<Label> LabelList = new List<Label>();
private List<Button> ButtonList = new List<Button>();
private List<Point[]> PointsList = new List<Point[]>();
private List<Brush> BrushList = new List<Brush>();
public Form1()
{
InitializeComponent();
graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height);
g = Graphics.FromImage(graphic);
LabelList.Add(label1);
LabelList.Add(label2);
LabelList.Add(label3);
LabelList.Add(label4);
ButtonList.Add(button1);
ButtonList.Add(button2);
ButtonList.Add(button3);
ButtonList.Add(button4);
this.Width = 1200;
this.Height = 600;
}
private void DrawFullList()
{
graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height + 100);
g = Graphics.FromImage(graphic);
pictureBox2.Image = graphic;
for (int i = 0; i < PointsList.Count; i++)
{
bool yellowframe = false;
if (i == dblclck)
yellowframe = true;
Draw(BrushList[i], PointsList[i], yellowframe);
}
}
private void Draw(Brush brush, Point[] points, bool yellowframe)
{
Pen PathPen = new Pen(brush);
if (yellowframe)
PathPen = new Pen(Color.Yellow);
PathPen.Width = 3;
g.DrawPolygon(PathPen, points);
g.FillPolygon(brush, points);
pictureBox2.Image = graphic;
}
private int showNumberForButton(int button)
{
if (button == -1)
return 0;
else if (button > 2)
return 6;
else
return button + 2;
}
private void ButtonClick(int buttonint)
{
button = buttonint;
LabelList[button].Text = "Choose " + (showNumberForButton(button)) + " more points\nto construct shape"; ;
if(showNumberForButton(button) == 2)
points = new Point[4];
else
points = new Point[showNumberForButton(button)];
for (int i = 0; i < 4; i++)
ButtonList[i].Enabled = false;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
DialogResult result = colorDialog1.ShowDialog();
if (result == DialogResult.OK)
{
p.Color = colorDialog1.Color;
pictureBox1.BackColor = colorDialog1.Color;
}
}
private void button1_Click(object sender, EventArgs e)
{
ButtonClick(0);
}
private void button2_Click(object sender, EventArgs e)
{
ButtonClick(1);
}
private void button3_Click(object sender, EventArgs e)
{
ButtonClick(2);
}
private void button4_Click(object sender, EventArgs e)
{
ButtonClick(3);
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
StorePath = dialog.SelectedPath;
NameForm = new Name();
NameForm.FormClosed += new FormClosedEventHandler(ChildFormClosed);
NameForm.ShowDialog();
}
else
MessageBox.Show("You canceled the saving prodedure.\nThe image is not saved.", "Image saving canceled", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void ChildFormClosed(object sender, FormClosedEventArgs e)
{
NameString = NameForm.PassName();
if (NameString != null && StorePath != null)
graphic.Save(StorePath + "\\" + NameString + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
else
MessageBox.Show("You canceled the saving prodedure.\nThe image is not saved.", "Image saving canceled", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
for (int i = 0; i < PointsList.Count; i++)
{
if (IsPointInPolygon4(PointsList[i], pictureBox2.PointToClient(Cursor.Position)))
{
dblclck = -1;
DrawFullList();
}
}
if (checker < showNumberForButton(button))
{
points[checker++] = pictureBox2.PointToClient(Cursor.Position);
if (showNumberForButton(button) == 2 && checker == 2)
{
points[2] = new Point(points[1].X + 3, points[1].Y + 3);
points[3] = new Point(points[0].X + 3, points[0].Y + 3);
}
if (checker == showNumberForButton(button))
{
Brush brush = new SolidBrush(colorDialog1.Color);
Draw(brush, points, false);
PointsList.Add(points);
BrushList.Add(brush);
checker = 0;
button = -1;
for (int i = 0; i < 4; i++)
{
ButtonList[i].Enabled = true;
LabelList[i].Text = string.Empty;
}
}
else
LabelList[button].Text = "Choose " + (showNumberForButton(button) - checker) + " more points\nto construct shape";
}
Point MouseCatch = pictureBox2.PointToClient(Cursor.Position);
for (int i = 0; i < PointsList.Count; i++)
{
if (IsPointInPolygon4(PointsList[i], MouseCatch))
{
for (int j = 0; j < PointsList[i].Length; j++)
{
if (MouseCatch.X >= PointsList[i][j].X - 20 && MouseCatch.X <= PointsList[i][j].X + 20 && MouseCatch.Y >= PointsList[i][j].Y - 20 && MouseCatch.Y <= PointsList[i][j].Y + 20)
{
VertexCatch = j;
Point[] temp = PointsList[i];
Brush tempB = BrushList[i];
PointsList.Remove(PointsList[i]);
BrushList.Remove(BrushList[i]);
PointsList.Add(temp);
BrushList.Add(tempB);
if (isItStrip(PointsList[PointsList.Count - 1]))
isStrip = true;
break;
}
}
}
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height);
PointsList.Clear();
BrushList.Clear();
checker = 0;
button = -1;
for (int i = 0; i < 4; i++)
{
ButtonList[i].Enabled = true;
LabelList[i].Text = string.Empty;
}
VertexCatch = -1;
g = Graphics.FromImage(graphic);
pictureBox2.Image = graphic;
}
private bool isItStrip(Point[] Points)
{
if (Points.Length == 4 && (points[2].X == (points[1].X + 3)) && (points[2].Y == points[1].Y + 3) && (points[3].X == (points[0].X + 3)) && (points[0].Y + 3 == points[3].Y))
return true;
return false;
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (VertexCatch > -1)
{
Point k = pictureBox2.PointToClient(Cursor.Position);
PointsList[PointsList.Count - 1][VertexCatch] = k;
if (isStrip)
{
if (VertexCatch == 0)
PointsList[PointsList.Count - 1][VertexCatch + 3] = new Point (k.X + 3,k.Y + 3);
else if(VertexCatch == 1)
PointsList[PointsList.Count - 1][VertexCatch + 1] = new Point(k.X + 3, k.Y + 3);
else if (VertexCatch == 2)
PointsList[PointsList.Count - 1][VertexCatch - 1] = new Point(k.X - 3, k.Y + 3);
else if (VertexCatch == 3)
PointsList[PointsList.Count - 1][VertexCatch - 3] = new Point(k.X - 3, k.Y + 3);
}
DrawFullList();
}
}
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
isStrip = false;
VertexCatch = -1;
}
private void pictureBox2_MouseDoubleClick(object sender, MouseEventArgs e)
{
Point MouseCatch = pictureBox2.PointToClient(Cursor.Position);
for (int i = PointsList.Count-1; i >=0 ; i--)
{
if (IsPointInPolygon4(PointsList[i], MouseCatch))
{
dblclck = i;
break;
}
}
DrawFullList();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (dblclck != -1) {
switch (keyData) {
case Keys.Left:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].X -= 20;
break;
case Keys.Right:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].X += 20;
break;
case Keys.Up:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].Y -= 20;
break;
case Keys.Down:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].Y += 20;
break;
case Keys.Enter:
dblclck = -1;
break;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
DrawFullList();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void pictureBox2_Resize(object sender, EventArgs e)
{
DrawFullList();
}
}
}
The form in the designer is set 2419 x 1451 pixels... if this is larger than the screen WinForms ignores the setting of that property (check this.ClientSize = new System.Drawing.Size(2419, 1451); in your Form1.designer.cs file), so the window gets a "default" size.
Set your form to a reasonable size within the designer (1200 x 600, for example, like you are doing on your constructor).
If you are setting it to 1200x600 but still is getting serialized as 2419x1451, then you may have hit one of the MANY problems the winform designer has with high-dpi displays. Set your AutoscaleMode to None and it should not try to adjust the properties to the "current" dpi upon serializing.

Multiple CheckedChanged event on programmatically added checkboxes

I want to be able to assign each checkbox to it's own richtextbox
I'm making the richtextboxes, then I'm making the checkboxes but how can I "link" them together?
for example :
// richtextbox 1 - > checkbox 1 = false
// richtextbox 2 - > checkbox 2 = true
// richtextbox 3 - > checkbox 3 = true
// richtextbox 4 - > checkbox 4 = false
this is my code:
int n = TodoItems.Count;
RichTextBox[] RichtextBoxes = new RichTextBox[n];
CheckBox[] Checkboxes = new CheckBox[n];
for (int i = 0; i < n; i++)
{
//creating the richtextbox
RichtextBoxes[i] = new RichTextBox();
RichtextBoxes[i].Name = "TB" + i.ToString();
RichtextBoxes[i].Text = TodoItems[i].ToString();
RichtextBoxes[i].Location = new System.Drawing.Point(130, (10 + (60 * i)));
RichtextBoxes[i].Size = new System.Drawing.Size(300, 50);
RichtextBoxes[i].Visible = true;
RichtextBoxes[i].ReadOnly = true;
RichtextBoxes[i].SelectionAlignment = HorizontalAlignment.Center;
RichtextBoxes[i].BackColor = Color.White;
TodoList.Controls.Add(RichtextBoxes[i]);
//creating the checkboxes
Checkboxes[i] = new CheckBox();
Checkboxes[i].Name = "CB" + i.ToString();
Checkboxes[i].Text = "";
Checkboxes[i].Location = new System.Drawing.Point(440, (30 + (60 * i)));
Checkboxes[i].Size = new System.Drawing.Size(18, 17);
Checkboxes[i].Visible = true;
Checkboxes[i].CheckedChanged += new EventHandler(this.CheckedChange);
TodoList.Controls.Add(Checkboxes[i]);
}
I have modified your code please see below, you can access your desired rich text box on checkbox click.
RichTextBox[] RichtextBoxes { get; set; }
CheckBox[] Checkboxes { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
int n = TodoItems.Count;
RichtextBoxes = new RichTextBox[n];
Checkboxes = new CheckBox[n];
for (int i = 0; i < n; i++)
{
//creating the richtextbox
RichtextBoxes[i] = new RichTextBox();
RichtextBoxes[i].Name = "TB-" + i.ToString();
RichtextBoxes[i].Text = TodoItems[i].ToString();
RichtextBoxes[i].Location = new System.Drawing.Point(130, (10 + (60 * i)));
RichtextBoxes[i].Size = new System.Drawing.Size(300, 50);
RichtextBoxes[i].Visible = false;
RichtextBoxes[i].ReadOnly = true;
RichtextBoxes[i].SelectionAlignment = HorizontalAlignment.Center;
RichtextBoxes[i].BackColor = Color.White;
TodoList.Controls.Add(RichtextBoxes[i]);
//creating the checkboxes
Checkboxes[i] = new CheckBox();
Checkboxes[i].Name = "CB-" + i.ToString();
Checkboxes[i].Text = "";
Checkboxes[i].Location = new System.Drawing.Point(440, (30 + (60 * i)));
Checkboxes[i].Size = new System.Drawing.Size(18, 17);
Checkboxes[i].Visible = true;
Checkboxes[i].CheckedChanged += checkBox1_CheckedChanged;
TodoList.Controls.Add(Checkboxes[i]);
}
}
void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
string cbName = cb.Name;
int sbNumber = int.Parse(cbName.Split('-')[1]);
RichtextBoxes[sbNumber].Visible = true; // you can get desired richtextbox here and can any thing with it :)
}
I have found the answere, and I'll post it to make your life easyer
when I made the richtextboxes and the checkboxes I set there names to a number
Checkboxes[i].Name = i.ToString();
then I used the event that was provided for me by Mirza Danish Baig
Create and event void checkBox_CheckedChanged(object sender, EventArgs e) { } and then assign this event name Checkboxes[i].CheckedChanged += checkBox_CheckedChanged; –
and after that I started to try things eventualy I came to this :
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox ThisCheckbox = (CheckBox)sender;
if (ThisCheckbox.Checked == true)
{
//finding the richtextbox by id...
RichTextBox ThisRichtextbox = this.Controls.Find("TB" + ThisCheckbox.Name, true).FirstOrDefault() as RichTextBox;
try//try and catch for testing, this can be removed later.
{
MessageBox.Show(ThisRichtextbox.Text);
}
catch (Exception Exc)
{
MessageBox.Show(Exc.Message);
}
}
}

FlowLayoutPanel line between rows

I am trying to build a grid of companies generated from my database.
I set my flowlayout as topdown. Is it possible to put a line between rows like this http://data.worldbank.org/country
If needed, my code posted below.
public void createLinks(string[] groupNames)
{
for (int i = 0; i < groupNames.Length; i++)
{
LinkLabel obj = new LinkLabel();
obj.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
obj.LinkColor = Color.Black;
obj.Name = groupNames[i];
obj.Text = groupNames[i];
obj.Click += delegate(object sender, EventArgs e)
{LinkLabel ss = sender as LinkLabel;
frmCompanyReport test = new frmCompanyReport(ss.Name);
test.Show();
};
flowLayoutPanel1.Controls.Add(obj);
}
}
One solution is to use a Label to act as a line. Set AutoSize to False, Height to 1, and BorderStyle to FixedSingle. Then set the Width to the same as the FlowLayoutPanel.
Something like:
public void createLinks(string[] groupNames)
{
for (int i = 0; i < groupNames.Length; i++)
{
LinkLabel obj = new LinkLabel();
obj.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
obj.LinkColor = Color.Black;
obj.Name = groupNames[i];
obj.Text = groupNames[i];
obj.Click += delegate(object sender, EventArgs e)
{
LinkLabel ss = sender as LinkLabel;
frmCompanyReport test = new frmCompanyReport(ss.Name);
test.Show();
};
flowLayoutPanel1.Controls.Add(obj);
Label line = new Label();
line.AutoSize = false;
line.BorderStyle = BorderStyle.FixedSingle;
line.Height = 1;
line.Width = flowLayoutPanel1.Width;
flowLayoutPanel1.Controls.Add(line);
}
}

button event with validation and actions

I need help with the Check button. After a user adds all the 42 numbers in the textbox and enter a number from 0-9 in the "Enter number" area and clicks on the start button, next what he should do is run through the array of labels with the red label or lblCovece and he should collect the same values like the number enters before. And after he clicks on the Check button, the programme should first validate if the value that is selected with the red label is the same as the number entered. If is valid the label should turn green and than the result should appear in the label lblResultE(the result for example should be like this: if the number entered is 2, the result it is 2+2+2...)and if is not valid in the lblResultE we take out 10 points. That's what i did by now with some help.:) thank you.
namespace Seminarska
{
public partial class Form1 : Form
{
private Label l,l2,lblCovece,l4,lblResultE;
private Button bUp, bRight, bLeft, bDown, bCheck,bStart, bReset;
private TextBox txtVnes, txtGoal;
private Label[] pole;
public Form1()
{
InitializeComponent();
l2 = new Label();
l2.Text = " Enter one number";
l2.Location = new Point(230, 200);
l2.AutoSize = true;
l4 = new Label();
l4.Text = "Score";
l4.Location = new Point(240, 260);
l4.AutoSize = true;
lblResultE = new Label();
lblResultE.Location = new Point(350, 260);
lblResultE.AutoSize = true;
bLeft = new Button();
bLeft.Location = new Point(0, 250);
bLeft.Width=75;
bLeft.Height = 25;
bLeft.Text = "LEFT";
bCheck = new Button();
bCheck.Location = new Point(75, 250);
bCheck.Width = 75;
bCheck.Height = 25;
bCheck.Text = "Check";
bRight = new Button();
bRight.Location = new Point(150, 250);
bRight.Width = 75;
bRight.Height = 25;
bRight.Text = "RIGHT";
bUp = new Button();
bUp.Location = new Point(75, 220);
bUp.Width = 75;
bUp.Height = 25;
bUp.Text = "UP";
bDown = new Button();
bDown.Location = new Point(75, 280);
bDown.Width = 75;
bDown.Height = 25;
bDown.Text = "DOWN";
bStart = new Button();
bStart.Location = new Point(240, 165);
bStart.Width = 75;
bStart.Height = 25;
bStart.Text = "START";
bReset = new Button();
bReset.Location = new Point(320, 165);
bReset.Width = 75;
bReset.Height = 25;
bReset.Text = "RESET";
txtVnes = new TextBox();
txtVnes.Location = new Point(240, 10);
txtVnes.Width = 160;
txtVnes.Height = 130;
txtVnes.Multiline = true;
txtGoal = new TextBox();
txtGoal.Width = 75;
txtGoal.Height = 25;
txtGoal.Location = new Point(330, 200);
lblCovece = new Label();
lblCovece.Location = new Point(160,165);
lblCovece.Width = 20;
lblCovece.Height = 20;
lblCovece.TextAlign = ContentAlignment.MiddleCenter;
lblCovece.Text = "O";
lblCovece.BackColor = Color.FromArgb(255, 0, 0);
int a = 0;
pole = new Label[42];
this.Controls.Add(lblCovece);
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j <= 7; j++)
{
l = new Label();
l.Name = "label" + i.ToString() + j.ToString();
l.Text = "Z";
l.Width = 20;
l.Height = 20;
l.TextAlign = ContentAlignment.MiddleCenter;
l.Parent = this;
l.BackColor = Color.FromArgb(100, 149, 237);
l.Location = new Point(10 + (j - 1) * 25, 15 + (i - 1) * 25);
pole[a] = l;
this.Controls.Add(l);
a++;
}
}
this.Width = 460;
this.Height = 380;
this.Controls.Add(l2);
this.Controls.Add(l4);
this.Controls.Add(lblResultE);
this.Controls.Add(lblTimeE);
this.Controls.Add(bStart);
this.Controls.Add(bReset);
this.Controls.Add(txtGoal);
this.Controls.Add(txtVnes);
this.Controls.Add(bUp);
this.Controls.Add(bLeft);
this.Controls.Add(bRight);
this.Controls.Add(bDown);
this.Controls.Add(bCheck);
bStart.Click+=new EventHandler(bStart_Click);
bUp.Click+=new EventHandler(bUp_Click);
bDown.Click+=new EventHandler(bDown_Click);
bLeft.Click+=new EventHandler(bLeft_Click);
bRight.Click+=new EventHandler(bRight_Click);
bCheck.Click+=new EventHandler(bZemaj_Click);
bReset.Click+=new EventHandler(bReset_Click);
}
private void bStart_Click(object sender, EventArgs e)
{
string Str = txtGoal.Text.Trim();
int Num;
bool isNum = int.TryParse(Str, out Num);
if (isNum && Str.Length == 1)
{
string[] ts = txtVnes.Text.Split(
new string[] { "\r\n" },
StringSplitOptions.RemoveEmptyEntries);
int row = 0;
for (int i = 0; i < ts.Length && row < 6; i++)
{
if (LineIsValid(ts[i]))
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = ts[i][2 * col].ToString();
}
row++;
}
}
for (; row < 6; row++)
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = "Z";
}
}
}
else
{
MessageBox.Show("Invalid Input");
}
}
private static Regex regex = new Regex(#"^(\s)*(\d ){6}\d(\s)*$");
private static bool LineIsValid(string line)
{
return regex.IsMatch(line);
}
private void bReset_Click(object sender, EventArgs e)
{
txtVnes.Clear();
string[] ts = txtVnes.Text.Split(new string[] { "\r\n" },
StringSplitOptions.RemoveEmptyEntries);
int row = 0;
for (int i = 0; i < ts.Length && row < 6; i++)
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = "Z";
pole[row * 7 + col].BackColor=Color.FromArgb(100, 149, 237);
}
row++;
}
for (; row < 6; row++)
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = "Z";
pole[row * 7 + col].BackColor = Color.FromArgb(100, 149, 237);
}
}
txtGoal.Clear();
lblCovece.Location=new Point(160,165);
}
private void bUp_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X, lblCovece.Location.Y -
25);
}
private void bDown_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X, lblCovece.Location.Y +
25);
}
private void bLeft_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X - 25,
lblCovece.Location.Y);
}
private void bRight_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X + 25,
lblCovece.Location.Y);
}
private void bCheck_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
What makes your programm complicated and difficult to understand, is that you mix game logic with display logic.
I suggest you to redesign your game. It could look something like this:
public enum State
{
Empty, // Displays "Z"
Neutral, // Blue
Good, // Green
Bad // Red
}
public class Square
{
public int Number { get; set; }
public State State { get; set; }
}
public class Game
{
public const int Width = 7, Height = 6;
public Game()
{
Board = new Square[Width, Height];
}
public event EventHandler GameChanged;
public Square[,] Board { get; private set; }
public int CurrentX { get; private set; }
public int CurrentY { get; private set; }
public void Reset()
{
for (int x = 0; x < Width; x++) {
for (int y = 0; y < Height; y++) {
Board[x, y].State = State.Empty; // Always displayed in blue as "Z"
}
}
OnGameChanged();
}
public void MoveLeft()
{
if (CurrentX > 0) {
CurrentX--;
OnGameChanged();
}
}
public void MoveRight()
{
if (CurrentX < Width - 1) {
CurrentX++;
OnGameChanged();
}
}
// and so on
private void OnGameChanged()
{
EventHandler eh = GameChanged;
if (eh != null) {
eh(this, EventArgs.Empty);
}
}
}
In the form I would define pole to be a matrix as well (like the board). I show only a few relevant parts of the form code, to give you an idea of what I mean:
public class Form1 : Form
{
private Game game;
private Label[,] pole;
public Form1()
{
game = new Game();
game.GameChanged += new EventHandler(Game_GameChanged);
pole = new Label[Game.Width, Game.Height];
// Intialize pole.
// ...
}
void Game_GameChanged(object sender, EventArgs e)
{
for (int x = 0; x < Game.Width; x++) {
for (int y = 0; y < Game.Height; y++) {
Square square = game.Board[x, y];
Label label = pole[x,y];
switch (square.State) {
case State.Empty:
label.BackColor = Color.Blue;
label.Text = "Z";
break;
case State.Neutral:
label.BackColor = Color.Blue;
label.Text = square.Number.ToString();
break;
case State.Good:
label.BackColor = Color.Green;
label.Text = square.Number.ToString();
break;
case State.Bad:
label.BackColor = Color.Red;
label.Text = square.Number.ToString();
break;
default:
break;
}
}
}
// Place lblCovece according to game.CurrentX and game.CurrentY
// ...
}
private void bReset_Click(object sender, EventArgs e)
{
game.Reset();
}
private void bLeft_Click(object sender, EventArgs e)
{
game.MoveLeft();
}
private void bRight_Click(object sender, EventArgs e)
{
game.MoveRight();
}
}
Note how the Game class tells the form when changes happen through the event GameChanged. The form then updates the game display. In the game class, you can now concentrate on the game logic and do not have to deal with buttons and labels. You can also work with logical coordinates in the range [0..6] and [0..5] of the game board. This is easier than working with pixels. You delegate all the pixel calculations to the form.
My example is not complete, but when you try to implement it, you will see that it will be much easier to think about how the logic of the game should work.
Add an int score;
private void bCheck_Click(object sender, EventArgs e)
{
bool found = false;
foreach (Label l in pole)
{
if (l.Location == lblCovece.Location && l.Text == txtGoal.Text)
{
l.BackColor = Color.Green;
score += int.Parse(l.Text);
lblResultE.Text = score.ToString();
found = true;
}
}
if (!found)
{
score -= 10;
lblResultE.Text = score.ToString();
}
}

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