I move three controls using this code on MouseMove Event :
if (inHareket)
{
GelenResimcik.Left = e.X + GelenResimcik.Left -MouseDownLocation.X;
GelenResimcik.Top = e.Y + GelenResimcik.Top - MouseDownLocation.Y;
GelenResimcik.BackColor = Color.Transparent;
ResimHareket.Left = e.X + ResimHareket.Left - MouseDownLocation.X;
ResimHareket.Top = e.Y + ResimHareket.Top - MouseDownLocation.Y;
Yonlendir.Left = e.X + Yonlendir.Left - MouseDownLocation.X;
Yonlendir.Top = e.Y + Yonlendir.Top - MouseDownLocation.Y;
Aktar.Left = e.X + Aktar.Left - MouseDownLocation.X;
Aktar.Top = e.Y + Aktar.Top - MouseDownLocation.Y;
Vazgec.Left = e.X + Vazgec.Left - MouseDownLocation.X;
Vazgec.Top = e.Y + Vazgec.Top - MouseDownLocation.Y;
}
and if I try move them faster the result is :
Notice : When i stop moving, the glow is not visible, it is only moving faster.
What shall I do?
EDIT :
My Controls
this.Yonlendir = new System.Windows.Forms.PictureBox();
this.Vazgec = new System.Windows.Forms.PictureBox();
this.Aktar = new System.Windows.Forms.PictureBox();
this.ResimHareket = new System.Windows.Forms.PictureBox();
this.GelenResimcik = new System.Windows.Forms.PictureBox();
My Init :
((System.ComponentModel.ISupportInitialize)(this.Yonlendir)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.Vazgec)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.Aktar)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.ResimHareket)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.GelenResimcik)).BeginInit();
this.SuspendLayout();
//
// Yonlendir
//
this.Yonlendir.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
this.Yonlendir.Cursor = System.Windows.Forms.Cursors.Arrow;
this.Yonlendir.Image = global::Surface.Properties.Resources.rotate;
this.Yonlendir.Location = new System.Drawing.Point(26, 74);
this.Yonlendir.Name = "Yonlendir";
this.Yonlendir.Padding = new System.Windows.Forms.Padding(5);
this.Yonlendir.Size = new System.Drawing.Size(30, 30);
this.Yonlendir.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.Yonlendir.TabIndex = 14;
this.Yonlendir.TabStop = false;
this.Yonlendir.Click += new System.EventHandler(this.Yonlendir_Click);
//
// Vazgec
//
this.Vazgec.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
this.Vazgec.Cursor = System.Windows.Forms.Cursors.Arrow;
this.Vazgec.Image = global::Surface.Properties.Resources.iptal;
this.Vazgec.Location = new System.Drawing.Point(93, 8);
this.Vazgec.Name = "Vazgec";
this.Vazgec.Padding = new System.Windows.Forms.Padding(5);
this.Vazgec.Size = new System.Drawing.Size(30, 30);
this.Vazgec.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.Vazgec.TabIndex = 13;
this.Vazgec.TabStop = false;
this.Vazgec.Click += new System.EventHandler(this.buton1_Click);
//
// Aktar
//
this.Aktar.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
this.Aktar.Cursor = System.Windows.Forms.Cursors.Arrow;
this.Aktar.Image = global::Surface.Properties.Resources.kaydet;
this.Aktar.Location = new System.Drawing.Point(57, 8);
this.Aktar.Name = "Aktar";
this.Aktar.Padding = new System.Windows.Forms.Padding(5);
this.Aktar.Size = new System.Drawing.Size(30, 30);
this.Aktar.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.Aktar.TabIndex = 12;
this.Aktar.TabStop = false;
this.Aktar.Click += new System.EventHandler(this.Onayla_Click);
//
// ResimHareket
//
this.ResimHareket.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
this.ResimHareket.Cursor = System.Windows.Forms.Cursors.Arrow;
this.ResimHareket.Image = global::Surface.Properties.Resources.hareket;
this.ResimHareket.Location = new System.Drawing.Point(26, 38);
this.ResimHareket.Name = "ResimHareket";
this.ResimHareket.Padding = new System.Windows.Forms.Padding(5);
this.ResimHareket.Size = new System.Drawing.Size(30, 30);
this.ResimHareket.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.ResimHareket.TabIndex = 11;
this.ResimHareket.TabStop = false;
this.ResimHareket.Click += new System.EventHandler(this.ResimHareket_Click);
this.ResimHareket.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ResmiHareket_MouseDown);
this.ResimHareket.MouseMove += new System.Windows.Forms.MouseEventHandler(this.ResmiHareket_MouseMove);
this.ResimHareket.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ResimHareket_MouseUp);
//
// GelenResimcik
//
this.GelenResimcik.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.GelenResimcik.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.GelenResimcik.Location = new System.Drawing.Point(57, 39);
this.GelenResimcik.Name = "GelenResimcik";
this.GelenResimcik.Size = new System.Drawing.Size(438, 452);
this.GelenResimcik.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.GelenResimcik.TabIndex = 2;
this.GelenResimcik.TabStop = false;
My Timer (Interval 100):
private void ResimKontrol_Tick(object sender, EventArgs e)
{
try
{
if (ontanimli.Height != 0)
{
GelenResimcik.Image = ontanimliGelen;
GelenResimcik.Parent = cizim;
entegreGoster();
}
}
catch
{
}
}
entegreGoster and entegreGizle
public void entegreGoster() { GelenResimcik.BringToFront();ResimHareket.BringToFront();Aktar.BringToFront();Vazgec.BringToFront(); Yonlendir.BringToFront(); }
public void entegreGizle() { GelenResimcik.SendToBack(); ResimHareket.SendToBack(); Aktar.SendToBack(); Vazgec.SendToBack(); Yonlendir.SendToBack(); }
My MouseDown event :
private void ResmiHareket_MouseDown(object sender, MouseEventArgs e)
{
MouseDownLocation = e.Location;
inHareket = true;
}
I start timer when Open new form
Ontanimlilar ontanimli = new Ontanimlilar();
ontanimli.Show();
ResimKontrol.Start();
ontanimliGelen (public static pictureBox)
ontanimli Class :
Ciz.ontanimliGelen = seciliRes.Image;
this.Close();
Related
I am trying to make a Windows Form that shows display data from data a table. Every thing is working fine until I start scrolling in the Form using the auto scroll property.
I don't know why that's happening. I hope someone can help me.
private void allShowsToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
// Load All the Shows from the database
DataTable dt = accesse.LoadAllShows();
// Create the allShows From
Form allShows = new Form();
// Create the Controls for the AllShows form.
PictureBox[] pic = new PictureBox[dt.Rows.Count];
Label[] showName = new Label[dt.Rows.Count], season = new Label[dt.Rows.Count], eps = new Label[dt.Rows.Count], typ = new Label[dt.Rows.Count];
TextBox[] txtShowName = new TextBox[dt.Rows.Count], txtSeason = new TextBox[dt.Rows.Count], txtEps = new TextBox[dt.Rows.Count], txtTyp = new TextBox[dt.Rows.Count];
// AllShows Form properties
allShows.BackColor = Color.White;
allShows.Font = new Font("Calibri", 14f, FontStyle.Regular);
allShows.ForeColor = Color.Black;
allShows.Size = new Size(1050, 700);
allShows.StartPosition = FormStartPosition.CenterScreen;
allShows.AutoScroll = true;
allShows.AutoScrollMargin = new Size(0, 18);
// Variables
int y = 325; // the distens bettwen the controls on the Y and X.
bool xTurn = false; // the axies turn.
int yTurnNum = 0; // the y turn number.
for (int i = 0; i < dt.Rows.Count; i++)
{
// PictureBox Poster Properties
pic[i] = new PictureBox();
pic[i].BorderStyle = BorderStyle.FixedSingle;
pic[i].Size = new Size(162, 288);
pic[i].SizeMode = PictureBoxSizeMode.StretchImage;
pic[i].Image = Image.FromFile(dt.Rows[i][4].ToString());
// Label showName Properties
showName[i] = new Label();
showName[i].Text = "Show Name: " + dt.Rows[i][0];
showName[i].AutoSize = true;
// Label Season Properties
season[i] = new Label();
season[i].Text = "Season: " + dt.Rows[i][1];
season[i].AutoSize = true;
// Label Eps Properties
eps[i] = new Label();
eps[i].Text = "Episodes: " + dt.Rows[i][2];
eps[i].AutoSize = true;
// Label Typ Properties
typ[i] = new Label();
typ[i].Text = "Typ: " + dt.Rows[i][3];
typ[i].AutoSize = true;
if (xTurn)
{
// Sitting the location of the controls on the X turn
pic[i].Location = new Point(515, pic[i - 1].Location.Y);
showName[i].Location = new Point(687, showName[i - 1].Location.Y);
season[i].Location = new Point(687, season[i - 1].Location.Y);
eps[i].Location = new Point(687, eps[i - 1].Location.Y);
typ[i].Location = new Point(687, typ[i - 1].Location.Y);
xTurn = false;
}
else
{
// Sitting the location of the controls on the Y turn
pic[i].Location = new Point(15, 15 + (yTurnNum * y));
showName[i].Location = new Point(187, 20 + (yTurnNum * y));
season[i].Location = new Point(187, 55 + (yTurnNum * y));
eps[i].Location = new Point(187, 90 + (yTurnNum * y));
typ[i].Location = new Point(187, 125 + (yTurnNum * y));
yTurnNum += 1;
xTurn = true;
}
allShows.Controls.Add(pic[i]);
allShows.Controls.Add(showName[i]);
allShows.Controls.Add(season[i]);
allShows.Controls.Add(eps[i]);
allShows.Controls.Add(typ[i]);
}
allShows.ShowDialog();
}
catch (Exception ex)
{
tools.ShowErrorMessageBox(ex.Message, "Error");
}
}
I hope this gif will help you to understand what is happening.
enter image description here
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.
The error is
Index out of range exception
and is on the first line in the for loop:
for (i = (int)start, j = 0; i <= (int)end; i++, j++)
{ // Since linear relationships...
idealWeights[0, j] = (female += 3.5); **ERROR HERE ON THIS LINE**
idealWeights[1, j] = (male += 4.0);
}
The values in txtStart and txtEnd are:
txtStart = 36
txtEnd = 96
Here is the code:
using System;
using System.Windows.Forms;
public class frmMain : Form
{
private TextBox txtStart;
private TextBox txtEnd;
private ListBox lstResults;
private Button btnClose;
private Label label1;
private Label label2;
private Button btnCalc;
private void InitializeComponent()
{
this.btnCalc = new System.Windows.Forms.Button();
this.txtStart = new System.Windows.Forms.TextBox();
this.txtEnd = new System.Windows.Forms.TextBox();
this.lstResults = new System.Windows.Forms.ListBox();
this.btnClose = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnCalc
//
this.btnCalc.Location = new System.Drawing.Point(26, 197);
this.btnCalc.Name = "btnCalc";
this.btnCalc.Size = new System.Drawing.Size(75, 23);
this.btnCalc.TabIndex = 0;
this.btnCalc.Text = "Ca&lculate";
this.btnCalc.UseVisualStyleBackColor = true;
this.btnCalc.Click += new System.EventHandler(this.btnCalc_Click);
//
// txtStart
//
this.txtStart.Location = new System.Drawing.Point(172, 12);
this.txtStart.Name = "txtStart";
this.txtStart.Size = new System.Drawing.Size(100, 20);
this.txtStart.TabIndex = 1;
//
// txtEnd
//
this.txtEnd.Location = new System.Drawing.Point(172, 38);
this.txtEnd.Name = "txtEnd";
this.txtEnd.Size = new System.Drawing.Size(100, 20);
this.txtEnd.TabIndex = 2;
//
// lstResults
//
this.lstResults.FormattingEnabled = true;
this.lstResults.Location = new System.Drawing.Point(26, 96);
this.lstResults.Name = "lstResults";
this.lstResults.Size = new System.Drawing.Size(246, 95);
this.lstResults.TabIndex = 3;
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(197, 197);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 4;
this.btnClose.Text = "&Close";
this.btnClose.UseVisualStyleBackColor = true;
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(23, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(143, 20);
this.label1.TabIndex = 5;
this.label1.Text = "Start:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label2
//
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label2.Location = new System.Drawing.Point(23, 38);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(143, 20);
this.label2.TabIndex = 6;
this.label2.Text = "End:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.lstResults);
this.Controls.Add(this.txtEnd);
this.Controls.Add(this.txtStart);
this.Controls.Add(this.btnCalc);
this.Name = "frmMain";
this.ResumeLayout(false);
this.PerformLayout();
}
public frmMain()
{
InitializeComponent();
}
public static void Main()
{
frmMain main = new frmMain();
Application.Run(main);
}
const double MININCHES = 36;
const double MAXINCHES = 96;
private void btnCalc_Click(object sender, EventArgs e)
{
bool flag;
int i;
int j;
double start;
double end;
double male;
double female;
double[,] idealWeights;
string buff;
//============================== Input ===============================================
flag = double.TryParse(txtStart.Text, out start); // Table start
if (flag == false)
{
MessageBox.Show("Numeric only.");
txtStart.Focus();
return;
}
flag = double.TryParse(txtEnd.Text, out end); // Table end
if (flag == false)
{
MessageBox.Show("Numeric only.");
txtEnd.Focus();
return;
}
//============================= Validate input ==============================================
if (start < MININCHES || start > MAXINCHES) // Check table limits
{
MessageBox.Show("Table can only span " + MININCHES.ToString() + " to " +
MAXINCHES + " inches.");
txtStart.Focus();
return;
}
if (end < MININCHES || end > MAXINCHES)
{
MessageBox.Show("Table can only span " + MININCHES.ToString() +
" to " + MAXINCHES + " inches.");
txtEnd.Focus();
return;
}
if (end <= start) // Can we display anything
{
MessageBox.Show("Starting value must be less than ending value");
txtStart.Focus();
return;
}
// Define the array for table data
idealWeights = new double[2, (int)(end - start) + 1];
//=============================== Process ====================================================
start--; // This is the new line
female = 3.5 * start - 108; // Set initial table values
male = 4.0 * start - 128;
for (i = (int)start, j = 0; i <= (int)end; i++, j++)
{ // Since linear relationships...
idealWeights[0, j] = (female += 3.5);
idealWeights[1, j] = (male += 4.0);
}
//================================ Display step ==============================================
for (i = (int)start, j = 0; i <= (int)end; i++, j++)
{
buff = String.Format("{0,5}{1,15}{2,15}", i, idealWeights[0, j], idealWeights[1, j]);
lstResults.Items.Add(buff);
}
}
}
I am very new to programming so I'm a bit lost here, this code is from a book and there haven't been any error's yet in the book. So hope it's not very complicated the error because I'm still just typing alot of code and reading code to try to understand it.
You've got no bondary checking on the j variable. It's obviously increasing past the size of the second array element. As Servy pointed out in our comments, it's a common off by one error. I have adjusted the code to remove the = from the <=.
The for line needs to look something like...
for (i = (int)start, j = 0; i < (int)end; i++, j++)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I do not understand what I am doing incorrectly in my dotButton, it shows it but does not give correct results? Any advise?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool isFirst = true;
private bool shouldClear = true;
private double num1;
private double num2;
private double result = 0;
private string symbol = string.Empty;
private bool hasDecimal = false;
private void numberButton_Click(object sender, EventArgs e)
{
Button sourceButton = (sender as Button);
double oldNumber, buttonNumber, newNumber;
if (shouldClear)
{
outputTextbox.Clear();
oldNumber = 0;
shouldClear = false;
}
else
{
oldNumber = double.Parse(outputTextbox.Text);
hasDecimal = true;
}
buttonNumber = Double.Parse(sourceButton.Text);
newNumber = (oldNumber * 10) + buttonNumber;
if (isFirst)
{
num1 = newNumber;
}
else
{
num2 = newNumber;
}
outputTextbox.Text += sourceButton.Text;
Calculate(symbol);
}
private void Calculate(string operatorSymbol)
{
if (isFirst)
{
result = num1;
return;
}
switch (operatorSymbol)
{
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
}
}
private void operatorButton_Click(object sender, EventArgs e)
{
num1 = result;
Button sourceButton = (sender as Button);
string operatorSymbol = sourceButton.Text;
if (isFirst)
isFirst = false;
hasDecimal = true;
shouldClear = true;
symbol = operatorSymbol;
outputTextbox.Text = result.ToString();
}
private void equalsButton_Click(object sender, EventArgs e)
{
outputTextbox.Text = result.ToString();
isFirst = true;
shouldClear = true;
hasDecimal = true;
}
private void clearButton_Click(object sender, EventArgs e)
{
outputTextbox.Text = "0";
result = 0;
isFirst = true;
shouldClear = true;
hasDecimal = false;
}
private void dotButton_Click(object sender, EventArgs e)
{
if (hasDecimal)
hasDecimal = true;
isFirst = false;
shouldClear = true;
outputTextbox.Text = dotButton.Text;
}
}
}
namespace WindowsFormsApplication2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
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.zeroButton = new System.Windows.Forms.Button();
this.oneButton = new System.Windows.Forms.Button();
this.twoButton = new System.Windows.Forms.Button();
this.threeButton = new System.Windows.Forms.Button();
this.fourButton = new System.Windows.Forms.Button();
this.fiveButton = new System.Windows.Forms.Button();
this.sixButton = new System.Windows.Forms.Button();
this.sevenButton = new System.Windows.Forms.Button();
this.eightButton = new System.Windows.Forms.Button();
this.nineButton = new System.Windows.Forms.Button();
this.dotButton = new System.Windows.Forms.Button();
this.addButton = new System.Windows.Forms.Button();
this.subtractButton = new System.Windows.Forms.Button();
this.multiplyButton = new System.Windows.Forms.Button();
this.divideButton = new System.Windows.Forms.Button();
this.clearButton = new System.Windows.Forms.Button();
this.equalsButton = new System.Windows.Forms.Button();
this.outputTextbox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// zeroButton
//
this.zeroButton.Location = new System.Drawing.Point(9, 230);
this.zeroButton.Margin = new System.Windows.Forms.Padding(2);
this.zeroButton.Name = "zeroButton";
this.zeroButton.Size = new System.Drawing.Size(58, 41);
this.zeroButton.TabIndex = 0;
this.zeroButton.Text = "0";
this.zeroButton.UseVisualStyleBackColor = true;
this.zeroButton.Click += new System.EventHandler(this.numberButton_Click);
//
// oneButton
//
this.oneButton.Location = new System.Drawing.Point(9, 184);
this.oneButton.Margin = new System.Windows.Forms.Padding(2);
this.oneButton.Name = "oneButton";
this.oneButton.Size = new System.Drawing.Size(38, 41);
this.oneButton.TabIndex = 1;
this.oneButton.Text = "1";
this.oneButton.UseVisualStyleBackColor = true;
this.oneButton.Click += new System.EventHandler(this.numberButton_Click);
//
// twoButton
//
this.twoButton.Location = new System.Drawing.Point(51, 184);
this.twoButton.Margin = new System.Windows.Forms.Padding(2);
this.twoButton.Name = "twoButton";
this.twoButton.Size = new System.Drawing.Size(38, 41);
this.twoButton.TabIndex = 2;
this.twoButton.Text = "2";
this.twoButton.UseVisualStyleBackColor = true;
this.twoButton.Click += new System.EventHandler(this.numberButton_Click);
//
// threeButton
//
this.threeButton.Location = new System.Drawing.Point(93, 184);
this.threeButton.Margin = new System.Windows.Forms.Padding(2);
this.threeButton.Name = "threeButton";
this.threeButton.Size = new System.Drawing.Size(38, 41);
this.threeButton.TabIndex = 3;
this.threeButton.Text = "3";
this.threeButton.UseVisualStyleBackColor = true;
this.threeButton.Click += new System.EventHandler(this.numberButton_Click);
//
// fourButton
//
this.fourButton.Location = new System.Drawing.Point(9, 139);
this.fourButton.Margin = new System.Windows.Forms.Padding(2);
this.fourButton.Name = "fourButton";
this.fourButton.Size = new System.Drawing.Size(38, 41);
this.fourButton.TabIndex = 4;
this.fourButton.Text = "4";
this.fourButton.UseVisualStyleBackColor = true;
this.fourButton.Click += new System.EventHandler(this.numberButton_Click);
//
// fiveButton
//
this.fiveButton.Location = new System.Drawing.Point(51, 139);
this.fiveButton.Margin = new System.Windows.Forms.Padding(2);
this.fiveButton.Name = "fiveButton";
this.fiveButton.Size = new System.Drawing.Size(38, 41);
this.fiveButton.TabIndex = 5;
this.fiveButton.Text = "5";
this.fiveButton.UseVisualStyleBackColor = true;
this.fiveButton.Click += new System.EventHandler(this.numberButton_Click);
//
// sixButton
//
this.sixButton.Location = new System.Drawing.Point(93, 139);
this.sixButton.Margin = new System.Windows.Forms.Padding(2);
this.sixButton.Name = "sixButton";
this.sixButton.Size = new System.Drawing.Size(38, 41);
this.sixButton.TabIndex = 6;
this.sixButton.Text = "6";
this.sixButton.UseVisualStyleBackColor = true;
this.sixButton.Click += new System.EventHandler(this.numberButton_Click);
//
// sevenButton
//
this.sevenButton.Location = new System.Drawing.Point(9, 93);
this.sevenButton.Margin = new System.Windows.Forms.Padding(2);
this.sevenButton.Name = "sevenButton";
this.sevenButton.Size = new System.Drawing.Size(38, 41);
this.sevenButton.TabIndex = 7;
this.sevenButton.Text = "7";
this.sevenButton.UseVisualStyleBackColor = true;
this.sevenButton.Click += new System.EventHandler(this.numberButton_Click);
//
// eightButton
//
this.eightButton.Location = new System.Drawing.Point(51, 93);
this.eightButton.Margin = new System.Windows.Forms.Padding(2);
this.eightButton.Name = "eightButton";
this.eightButton.Size = new System.Drawing.Size(38, 41);
this.eightButton.TabIndex = 8;
this.eightButton.Text = "8";
this.eightButton.UseVisualStyleBackColor = true;
this.eightButton.Click += new System.EventHandler(this.numberButton_Click);
//
// nineButton
//
this.nineButton.Location = new System.Drawing.Point(93, 93);
this.nineButton.Margin = new System.Windows.Forms.Padding(2);
this.nineButton.Name = "nineButton";
this.nineButton.Size = new System.Drawing.Size(38, 41);
this.nineButton.TabIndex = 9;
this.nineButton.Text = "9";
this.nineButton.UseVisualStyleBackColor = true;
this.nineButton.Click += new System.EventHandler(this.numberButton_Click);
//
// dotButton
//
this.dotButton.Location = new System.Drawing.Point(72, 230);
this.dotButton.Margin = new System.Windows.Forms.Padding(2);
this.dotButton.Name = "dotButton";
this.dotButton.Size = new System.Drawing.Size(58, 41);
this.dotButton.TabIndex = 10;
this.dotButton.Text = ".";
this.dotButton.UseVisualStyleBackColor = true;
this.dotButton.Click += new System.EventHandler(this.dotButton_Click);
//
// addButton
//
this.addButton.Location = new System.Drawing.Point(135, 230);
this.addButton.Margin = new System.Windows.Forms.Padding(2);
this.addButton.Name = "addButton";
this.addButton.Size = new System.Drawing.Size(38, 41);
this.addButton.TabIndex = 11;
this.addButton.Text = "+";
this.addButton.UseVisualStyleBackColor = true;
this.addButton.Click += new System.EventHandler(this.operatorButton_Click);
//
// subtractButton
//
this.subtractButton.Location = new System.Drawing.Point(135, 184);
this.subtractButton.Margin = new System.Windows.Forms.Padding(2);
this.subtractButton.Name = "subtractButton";
this.subtractButton.Size = new System.Drawing.Size(38, 41);
this.subtractButton.TabIndex = 12;
this.subtractButton.Text = "-";
this.subtractButton.UseVisualStyleBackColor = true;
this.subtractButton.Click += new System.EventHandler(this.operatorButton_Click);
//
// multiplyButton
//
this.multiplyButton.Location = new System.Drawing.Point(135, 139);
this.multiplyButton.Margin = new System.Windows.Forms.Padding(2);
this.multiplyButton.Name = "multiplyButton";
this.multiplyButton.Size = new System.Drawing.Size(38, 41);
this.multiplyButton.TabIndex = 13;
this.multiplyButton.Text = "*";
this.multiplyButton.UseVisualStyleBackColor = true;
this.multiplyButton.Click += new System.EventHandler(this.operatorButton_Click);
//
// divideButton
//
this.divideButton.Location = new System.Drawing.Point(135, 93);
this.divideButton.Margin = new System.Windows.Forms.Padding(2);
this.divideButton.Name = "divideButton";
this.divideButton.Size = new System.Drawing.Size(38, 41);
this.divideButton.TabIndex = 14;
this.divideButton.Text = "/";
this.divideButton.UseVisualStyleBackColor = true;
this.divideButton.Click += new System.EventHandler(this.operatorButton_Click);
//
// clearButton
//
this.clearButton.Location = new System.Drawing.Point(9, 48);
this.clearButton.Margin = new System.Windows.Forms.Padding(2);
this.clearButton.Name = "clearButton";
this.clearButton.Size = new System.Drawing.Size(80, 41);
this.clearButton.TabIndex = 15;
this.clearButton.Text = "Clear";
this.clearButton.UseVisualStyleBackColor = true;
this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
//
// equalsButton
//
this.equalsButton.Location = new System.Drawing.Point(93, 48);
this.equalsButton.Margin = new System.Windows.Forms.Padding(2);
this.equalsButton.Name = "equalsButton";
this.equalsButton.Size = new System.Drawing.Size(80, 41);
this.equalsButton.TabIndex = 16;
this.equalsButton.Text = "=";
this.equalsButton.UseVisualStyleBackColor = true;
this.equalsButton.Click += new System.EventHandler(this.equalsButton_Click);
//
// outputTextbox
//
this.outputTextbox.Location = new System.Drawing.Point(9, 3);
this.outputTextbox.Margin = new System.Windows.Forms.Padding(2);
this.outputTextbox.Multiline = true;
this.outputTextbox.Name = "outputTextbox";
this.outputTextbox.Size = new System.Drawing.Size(164, 41);
this.outputTextbox.TabIndex = 17;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(184, 280);
this.Controls.Add(this.outputTextbox);
this.Controls.Add(this.equalsButton);
this.Controls.Add(this.clearButton);
this.Controls.Add(this.divideButton);
this.Controls.Add(this.multiplyButton);
this.Controls.Add(this.subtractButton);
this.Controls.Add(this.addButton);
this.Controls.Add(this.dotButton);
this.Controls.Add(this.nineButton);
this.Controls.Add(this.eightButton);
this.Controls.Add(this.sevenButton);
this.Controls.Add(this.sixButton);
this.Controls.Add(this.fiveButton);
this.Controls.Add(this.fourButton);
this.Controls.Add(this.threeButton);
this.Controls.Add(this.twoButton);
this.Controls.Add(this.oneButton);
this.Controls.Add(this.zeroButton);
this.Margin = new System.Windows.Forms.Padding(2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button zeroButton;
private System.Windows.Forms.Button oneButton;
private System.Windows.Forms.Button twoButton;
private System.Windows.Forms.Button threeButton;
private System.Windows.Forms.Button fourButton;
private System.Windows.Forms.Button fiveButton;
private System.Windows.Forms.Button sixButton;
private System.Windows.Forms.Button sevenButton;
private System.Windows.Forms.Button eightButton;
private System.Windows.Forms.Button nineButton;
private System.Windows.Forms.Button dotButton;
private System.Windows.Forms.Button addButton;
private System.Windows.Forms.Button subtractButton;
private System.Windows.Forms.Button multiplyButton;
private System.Windows.Forms.Button divideButton;
private System.Windows.Forms.Button clearButton;
private System.Windows.Forms.Button equalsButton;
private System.Windows.Forms.TextBox outputTextbox;
}
}
I would suspect that you want
outputTextbox.Text += dotButton.Text;
(you missed the +)
In your method for dotButton_Click you have a boolean shouldClear set to true:
private void dotButton_Click(object sender, EventArgs e)
{
if (hasDecimal)
hasDecimal = true;
isFirst = false;
shouldClear = true;
outputTextbox.Text += dotButton.Text;
}
Change it to false.
And: outputTextbox.Text += dotButton.Text; with a += not just an =.
The following lines have a problem:
buttonNumber = Double.Parse(sourceButton.Text);
newNumber = (oldNumber*10) + buttonNumber;
Why are you multiplying by 10?
There seems to be quite a lot of logic a bit mixed up here, I have solved the first issue you need to debug your code and step through it. You will see the sequence of events are not in the order you want them to be. Too many things here to change, and it will take a rewrite that I am sure you would want to do yourself rather than have me do for you... Even if I had the time.
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();