for loop throwing out of range exception - c#

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++)

Related

Cursor release event is not triggering in teechart

I am using teechart cursor tool and using its "change" event, which gets call each and every time when we move cursor of chart, but I want a event/function which gets call when we release cursor after moving.
this.cursorTool1.Change += new Steema.TeeChart.Tools.CursorChangeEventHandler(this.cursorTool1_Change);
Sample code :-
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Steema.TeeChart.Samples
{
public class Tool_Cursor : Samples.BaseForm
{
private Steema.TeeChart.Styles.Line lineSeries1;
private Steema.TeeChart.Styles.Line lineSeries2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.Button button1;
private Steema.TeeChart.Axis axis1;
private Steema.TeeChart.Tools.CursorTool cursorTool1;
private Steema.TeeChart.Tools.CursorTool cursorTool2;
private System.ComponentModel.IContainer components = null;
private bool flag;
public Tool_Cursor()
{
// This call is required by the Windows Form Designer.
InitializeComponent();
this.lineSeries1.FillSampleValues(100);
this.lineSeries2.FillSampleValues(100);
this.tChart1.Axes.Left.AutomaticMinimum = false;
this.tChart1.Axes.Left.Minimum = 0.0;
this.lineSeries2.CustomVertAxis = this.axis1;
this.tChart1.BeforeDrawSeries += new PaintChartEventHandler(tChart1_BeforeDrawSeries);
flag = true;
this.cursorTool1.Pen.Color = Color.Navy;
this.cursorTool1.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dash;
this.cursorTool1.Pen.Width = 2;
this.cursorTool2.Pen.Color = Color.Plum;
this.cursorTool2.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dot;
this.cursorTool2.Pen.Width = 2;
}
void tChart1_BeforeDrawSeries(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
if (flag)
{
// place upper series cursor in the middle
this.cursorTool1.XValue = 0.5 * (this.lineSeries1.XValues.Maximum + this.lineSeries1.XValues.Minimum);
flag = false;
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 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.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Tool_Cursor));
this.lineSeries1 = new Steema.TeeChart.Styles.Line();
this.lineSeries2 = new Steema.TeeChart.Styles.Line();
this.axis1 = new Steema.TeeChart.Axis(this.components);
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.button1 = new System.Windows.Forms.Button();
this.cursorTool1 = new Steema.TeeChart.Tools.CursorTool();
this.cursorTool2 = new Steema.TeeChart.Tools.CursorTool();
this.panel1.SuspendLayout();
this.chartContainer.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Size = new System.Drawing.Size(406, 56);
this.textBox1.Text = "Cursor Tool is used to display vertical and / or horizontal lines on top of chart" +
"s. Cursors can be dragged by mouse or by code at runtime. They notify position c" +
"hanges with the OnChange event.";
//
// panel1
//
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.checkBox2);
this.panel1.Controls.Add(this.checkBox1);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(0, 56);
this.panel1.Size = new System.Drawing.Size(406, 52);
//
// tChart1
//
//
//
//
this.tChart1.Aspect.View3D = false;
this.tChart1.Aspect.ZOffset = 0;
//
//
//
this.tChart1.Axes.Custom.Add(this.axis1);
//
//
//
this.tChart1.Axes.Left.EndPosition = 50;
//
//
//
//
//
//
//
//
//
this.tChart1.Axes.Left.Labels.Font.Brush.Color = System.Drawing.Color.Red;
this.tChart1.Axes.Left.Labels.Font.Size = 7;
this.tChart1.Axes.Left.Labels.Font.SizeFloat = 7F;
this.tChart1.Axes.Left.LogarithmicBase = 2;
//
//
//
this.tChart1.Header.Lines = new string[] {
"Cursor tool example"};
this.tChart1.Header.Visible = false;
//
//
//
//
//
//
this.tChart1.Panel.Brush.Color = System.Drawing.Color.FromArgb(((int)(((byte)(254)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
this.tChart1.Series.Add(this.lineSeries1);
this.tChart1.Series.Add(this.lineSeries2);
this.tChart1.Size = new System.Drawing.Size(406, 155);
this.tChart1.Tools.Add(this.cursorTool1);
this.tChart1.Tools.Add(this.cursorTool2);
this.tChart1.AfterDraw += new Steema.TeeChart.PaintChartEventHandler(this.tChart1_AfterDraw);
//
// chartContainer
//
this.chartContainer.Location = new System.Drawing.Point(0, 108);
this.chartContainer.Size = new System.Drawing.Size(406, 155);
//
// lineSeries1
//
//
//
//
this.lineSeries1.Brush.Color = System.Drawing.Color.FromArgb(((int)(((byte)(68)))), ((int)(((byte)(102)))), ((int)(((byte)(163)))));
this.lineSeries1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(68)))), ((int)(((byte)(102)))), ((int)(((byte)(163)))));
this.lineSeries1.ColorEach = false;
//
//
//
this.lineSeries1.LinePen.Color = System.Drawing.Color.FromArgb(((int)(((byte)(41)))), ((int)(((byte)(61)))), ((int)(((byte)(98)))));
//
//
//
//
//
//
this.lineSeries1.Marks.Callout.ArrowHead = Steema.TeeChart.Styles.ArrowHeadStyles.None;
this.lineSeries1.Marks.Callout.ArrowHeadSize = 8;
//
//
//
this.lineSeries1.Marks.Callout.Brush.Color = System.Drawing.Color.Black;
this.lineSeries1.Marks.Callout.Distance = 0;
this.lineSeries1.Marks.Callout.Draw3D = false;
this.lineSeries1.Marks.Callout.Length = 10;
this.lineSeries1.Marks.Callout.Style = Steema.TeeChart.Styles.PointerStyles.Rectangle;
this.lineSeries1.Marks.Callout.Visible = false;
//
//
//
this.lineSeries1.Pointer.Style = Steema.TeeChart.Styles.PointerStyles.Rectangle;
this.lineSeries1.Title = "line1";
//
//
//
this.lineSeries1.XValues.DataMember = "X";
this.lineSeries1.XValues.Order = Steema.TeeChart.Styles.ValueListOrder.Ascending;
//
//
//
this.lineSeries1.YValues.DataMember = "Y";
//
// lineSeries2
//
//
//
//
this.lineSeries2.Brush.Color = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(156)))), ((int)(((byte)(53)))));
this.lineSeries2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(156)))), ((int)(((byte)(53)))));
this.lineSeries2.ColorEach = false;
this.lineSeries2.CustomVertAxis = this.axis1;
//
//
//
this.lineSeries2.LinePen.Color = System.Drawing.Color.FromArgb(((int)(((byte)(146)))), ((int)(((byte)(94)))), ((int)(((byte)(32)))));
//
//
//
//
//
//
this.lineSeries2.Marks.Callout.ArrowHead = Steema.TeeChart.Styles.ArrowHeadStyles.None;
this.lineSeries2.Marks.Callout.ArrowHeadSize = 8;
//
//
//
this.lineSeries2.Marks.Callout.Brush.Color = System.Drawing.Color.Black;
this.lineSeries2.Marks.Callout.Distance = 0;
this.lineSeries2.Marks.Callout.Draw3D = false;
this.lineSeries2.Marks.Callout.Length = 10;
this.lineSeries2.Marks.Callout.Style = Steema.TeeChart.Styles.PointerStyles.Rectangle;
this.lineSeries2.Marks.Callout.Visible = false;
//
//
//
this.lineSeries2.Pointer.Style = Steema.TeeChart.Styles.PointerStyles.Rectangle;
this.lineSeries2.Title = "line2";
this.lineSeries2.VertAxis = Steema.TeeChart.Styles.VerticalAxis.Custom;
//
//
//
this.lineSeries2.XValues.DataMember = "X";
this.lineSeries2.XValues.Order = Steema.TeeChart.Styles.ValueListOrder.Ascending;
//
//
//
this.lineSeries2.YValues.DataMember = "Y";
//
// axis1
//
this.axis1.Horizontal = false;
//
//
//
//
//
//
//
//
//
this.axis1.Labels.Font.Brush.Color = System.Drawing.Color.Green;
this.axis1.Labels.Font.Size = 7;
this.axis1.Labels.Font.SizeFloat = 7F;
this.axis1.OtherSide = false;
this.axis1.StartPosition = 50;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(5, 7);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 13);
this.label1.TabIndex = 0;
this.label1.UseMnemonic = false;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(5, 26);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(0, 13);
this.label2.TabIndex = 1;
this.label2.UseMnemonic = false;
//
// checkBox1
//
this.checkBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.checkBox1.Location = new System.Drawing.Point(107, 12);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(53, 23);
this.checkBox1.TabIndex = 2;
this.checkBox1.Text = "&Snap";
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// checkBox2
//
this.checkBox2.Checked = true;
this.checkBox2.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.checkBox2.Location = new System.Drawing.Point(167, 12);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(72, 23);
this.checkBox2.TabIndex = 3;
this.checkBox2.Text = "&Active";
this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged);
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(247, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "&Edit...";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// cursorTool1
//
this.cursorTool1.Series = this.lineSeries1;
this.cursorTool1.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical;
this.cursorTool1.Change += new Steema.TeeChart.Tools.CursorChangeEventHandler(this.cursorTool1_Change);
//
// cursorTool2
//
this.cursorTool2.FollowMouse = true;
this.cursorTool2.Series = this.lineSeries2;
this.cursorTool2.Change += new Steema.TeeChart.Tools.CursorChangeEventHandler(this.cursorTool2_Change);
//
// Tool_Cursor
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(406, 263);
this.Name = "Tool_Cursor";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.chartContainer.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{
this.cursorTool1.Snap = this.checkBox1.Checked;
this.cursorTool1.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical;
}
private void checkBox2_CheckedChanged(object sender, System.EventArgs e)
{
this.cursorTool1.Active = this.checkBox2.Checked;
}
private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
Steema.TeeChart.Drawing.Graphics3D gr = this.tChart1.Graphics3D;
// TODO : add custom horizontal line where the axes meet
}
private void cursorTool1_Change(object sender, Steema.TeeChart.Tools.CursorChangeEventArgs e)
{
// show the cursor values...
this.label1.Text = "X="+e.XValue.ToString("#.00");
}
private void cursorTool2_Change(object sender, Steema.TeeChart.Tools.CursorChangeEventArgs e)
{
// show the cursor values...
this.label2.Text = "X="+e.XValue.ToString("#.00")+"; Y="+e.YValue.ToString("#.00");
}
private void button1_Click(object sender, System.EventArgs e)
{
TeeChart.Editors.Tools.ToolsEditor.ShowEditor(this.cursorTool1);
}
}
}
You could try adding in a MouseUp event and checked if the CursorTool was clicked, e.g.
this.cursorTool1.Pen.Width = 2;
tChart1.MouseUp += TChart1_MouseUp;
this.cursorTool2.Pen.Color = Color.Plum;
this.cursorTool2.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dot;
this.cursorTool2.Pen.Width = 2;
}
private void TChart1_MouseUp(object sender, MouseEventArgs e)
{
var clicked = cursorTool1.Clicked(e.X, e.Y);
if(clicked == Tools.CursorClicked.Vertical)
{
MessageBox.Show("The Cursed has been released!");
}
}

C# item Drag Fix

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();

Accessing ProgressBar in ListView

I have the following code that I am trying to figure out but I am completely stumped. I am adding the progressbar into the listview, but I really don't know how to access each progressbar to update the progress values.
public ProgressBar LvAddProgB(ListView LV, int LVII, int LVColI, string lvName)
{
Rectangle SizeR = default(Rectangle);
ProgressBar ProgBar = new ProgressBar();
SizeR = LV.Items[LVII].Bounds;
SizeR.Width = LV.Columns[LVColI].Width;
if (LVColI > 0)
{
SizeR.X = SizeR.X + LV.Columns[LVColI - 1].Width;
}
ProgBar.Parent = LV;
ProgBar.Name = lvName;
ProgBar.SetBounds(SizeR.X, SizeR.Y, SizeR.Width, SizeR.Height);
ProgBar.Visible = true;
ProgBar.Maximum = 1000;
ProgBar.Step = 1;
return ProgBar;
}
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x < 3; ++x)
{
ListViewItem item = new ListViewItem();
item.Text = "d.Name";
item.SubItems.Add(" ");
listView1.Items.Add(item);
LvAddProgB(listView1, x, 1, "Lview" + x.ToString());
}
}
If you use a key of some sort, you can fish it back out of the Controls collection to update. Since each is displayed as if it was part of the ListView, it seems like there is some sort of linkage between the two. A key will also provide a way to link the item and related ProgressBar.
Assuming your ListView is Details view, just add a subitem at the end, without a related ColumnHeader. The data will not show, but will still be related to the Item. Use the same text as the ProgressBar name and it is easy to find.
My ListView has 3 columns: {Item, Name, Completion}, but the code will add a 4th subitem to store the key:
private void AddLVItem(string key, string name, int value)
{
ListViewItem lvi = new ListViewItem();
ProgressBar pb = new ProgressBar();
lvi.SubItems[0].Text = name;
lvi.SubItems.Add(value.ToString());
lvi.SubItems.Add("");
lvi.SubItems.Add(key); // LV has 3 cols; this wont show
lv.Items.Add(lvi);
Rectangle r = lvi.SubItems[2].Bounds;
pb.SetBounds(r.X, r.Y, r.Width, r.Height);
pb.Minimum = 1;
pb.Maximum = 10;
pb.Value = value;
pb.Name = key; // use the key as the name
lv.Controls.Add(pb);
}
Then, a method to update the Value and Progress bar for a given key:
private void UpdateItemValue(string key, int value)
{
ListViewItem lvi;
ProgressBar pb;
// find the LVI based on the "key" in
lvi = lv.Items.Cast<ListViewItem>().FirstOrDefault(q => q.SubItems[3].Text == key);
if (lvi != null)
lvi.SubItems[1].Text = value.ToString();
pb = lv.Controls.OfType<ProgressBar>().FirstOrDefault(q => q.Name == key);
if (pb != null)
pb.Value = value;
}
usage:
// add some data
AddLVItem("A", "Ziggy", 1);
AddLVItem("B", "Zacky", 1);
AddLVItem("C", "Zoey", 1);
AddLVItem("D", "Zeke", 1);
// update the displayed value and progressbar using the key:
UpdateItemValue("A", 6);
UpdateItemValue("B", 5);
UpdateItemValue("C", 8);
UpdateItemValue("D", 2);
#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.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3});
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(12, 64);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(504, 164);
this.listView1.TabIndex = 1;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Width = 99;
//
// columnHeader2
//
this.columnHeader2.Width = 117;
//
// columnHeader3
//
this.columnHeader3.Width = 117;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(528, 261);
this.Controls.Add(this.listView1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.VScrollBar vScrollBar1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
public ProgressBar LvAddProgB(ListView LV, int X, int Y, string lvName)
{
ProgressBar ProgBar = new ProgressBar();
ProgBar.Parent = LV;
ProgBar.Name = lvName;
ProgBar.Location = new Point(X, Y);
ProgBar.Visible = true;
ProgBar.Maximum = 1000;
ProgBar.Step = 1;
return ProgBar;
}
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x < 3; ++x)
{
ListViewItem item = new ListViewItem();
item.Text = "d.Name";
item.SubItems.Add(" ");
listView1.Items.Add(item);
listView1.Controls.Add(LvAddProgB(listView1, item.Position.X + item.Bounds.Width, item.Position.Y, "Lview" + x.ToString()));
}
}

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.

What am I doing wrong on the decimal button, seems my bool is off? [closed]

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.

Categories

Resources