Why when i click the button to add a new point to the pictureBox sometimes the point is added...

  • Thread starter Thread starter Chocolade1972
  • Start date Start date
C

Chocolade1972

Guest
I have in Form1 two textBoxes :


The first one i called it Object name: For example i write inside: Bird

Then another textBox and i called it Animation name: For example i write inside: Fly


Then i have a button click event i called it Confirm Object:


private void button7_Click(object sender, EventArgs e)
{
wireObject1 = new WireObject(textBox3.Text);
saveBaseFileToolStripMenuItem.Enabled = true;
button1.Enabled = true;
button8.Enabled = false;
button7.Enabled = false;
}


The clss wireObject1 in the top is getting the Object Name i typed inside the first textBox:


public WireObject( string name )
{
wo_name = name;
woc = new WireObjectCoordinates();
fnExt = false;
}


Then i have another button click event i called it : Add Point:


private void button1_Click(object sender, EventArgs e)
{
addFrame = true;
halfX = pictureBox1.ClientRectangle.Width / 2;
halfY = pictureBox1.ClientRectangle.Height / 2;
Random rnd = new Random();
offsetX = rnd.Next(-10, 10);
offsetY = rnd.Next(-10, 10);
if (wireObject1 != null)
{
wireObject1.addPoint(halfX + offsetX, halfY + offsetY);
}
pictureBox1.Refresh();
numberOfPoints++;
label5.Text = "{X = " + (halfX + offsetX) + "}" + " " + "{Y = " + (halfY + offsetY) + "}";
label5.Visible = true;
label7.Visible = true;
label16.Text = numberOfPoints.ToString();
label16.Visible = true;
label15.Visible = true;
button10.Enabled = true;
button11.Enabled = true;

}



This is the addPoint function in the class wireObject:


public void addPoint(float x, float y)
{
woc.Point_X.Add(x);
woc.Point_Y.Add(y);
}


woc is another class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AnimationEditor
{
class WireObjectCoordinates
{
public List<float> Point_X = new List<float>();
public List<float> Point_Y = new List<float>();

public WireObjectCoordinates()
{
}

public WireObjectCoordinates(WireObjectCoordinates w)
{
Point_X.AddRange(w.Point_X);
Point_Y.AddRange(w.Point_Y);
}

public void Set(WireObjectCoordinates w)
{
if (w != null)
{
if (Point_X == null)
Point_X = new List<float>();
if (Point_Y == null)
Point_Y = new List<float>();
for (int i = 0; i < w.Point_X.Count; i++)
{
if (i < Point_X.Count && i < w.Point_X.Count)
Point_X = w.Point_X;
else if (i >= Point_X.Count && i < w.Point_X.Count)
Point_X.Add(w.Point_X);
if (i < Point_Y.Count && i < w.Point_Y.Count)
Point_Y = w.Point_Y;
else if (i >= Point_Y.Count && i < w.Point_Y.Count)
Point_Y.Add(w.Point_Y);
}
}
}
}
}


Then im doing pictureBox1.Refresh(); and right after this line its jumping to the pictureBox1 paint event:


private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{


Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

moveCounter++;
label6.Text = moveCounter.ToString();



if (addFrame == true)
{
addFrame = false;
WireObjectGraphics.Draw(wireObject1, g);
}

}


I used a breakpoint and i see its going to the WireObjectGraphics class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace AnimationEditor
{
class WireObjectGraphics
{
static Point connectionPointStart;
static Point connectionPointEnd;
static SolidBrush brush;
static Pen p = null;

public WireObjectGraphics()
{

}

public static void Draw(WireObject wo, Graphics graphics)
{
brush = new SolidBrush(Color.Red);
p = new Pen(brush);
Graphics g = graphics;
WireObject wireObject1 = wo;

if (wireObject1 != null)
{
for (int idx = 0; idx < wireObject1.woc.Point_X.Count; ++idx)
{
Point dPoint = new Point((int)wireObject1.woc.Point_X[idx], (int)wireObject1.woc.Point_Y[idx]);
dPoint.X = dPoint.X - 5; // was - 2
dPoint.Y = dPoint.Y - 5; // was - 2
Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
g.FillEllipse(brush, rect);
}

for (int i = 0; i < wireObject1._connectionstart.Count; i++)
{

int startIndex = wireObject1._connectionstart;
int endIndex = wireObject1._connectionend;



connectionPointStart = new Point((int)wireObject1.woc.Point_X[startIndex], (int)wireObject1.woc.Point_Y[startIndex]);
connectionPointEnd = new Point((int)wireObject1.woc.Point_X[endIndex], (int)wireObject1.woc.Point_Y[endIndex]);
p.Width = 2;
g.DrawLine(p, connectionPointStart, connectionPointEnd);
}
}
}

}


}



And i see its doing the Draw function but then after it nothing i dont see any point have been added to the pictureBox.

Sometimes there is a point sometimes in most of the cases i click the Add Point button and see a point for millisecond and its deleted. Then if i click again and again the add point button i see the points even the first one which was deleted .


But for some reason i cant figure out why sometimes the first point is added and sometimes its deleted .


What could be the reason that the first point is added and then automatic deleted after millisecond ?

Sometimes its drawing the point the first one and its staying on the pictureBox and sometimes the first point is showing for millisecond and then deleted.

Continue reading...
 
Back
Top