C# Windows Form snake - c#

I have this simple snake game, my problem is that the tails wont add when it reaches three tails.
namespace Snake
{
public partial class Form1 : Form
{
bool left = false, right = false;
bool top = false, down = false;
PictureBox pic = new PictureBox();
List<PictureBox> tails = new List<PictureBox>();
int score = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (((e.KeyChar.ToString() == "a") || (e.KeyChar.ToString() == "A"))&&(right == false))
{
right = false;
top = false;
down = false;
left = true;
}
else if (((e.KeyChar.ToString() == "d") || (e.KeyChar.ToString() == "D"))&& (left == false))
{
top = false;
down = false;
left = false;
right = true;
}
else if (((e.KeyChar.ToString() == "w") || (e.KeyChar.ToString() == "W"))&& (down == false))
{
down = false;
left = false;
right = false;
top = true;
}
else if (((e.KeyChar.ToString() == "s") || (e.KeyChar.ToString() == "S"))&& (top == false))
{
top = false;
left = false;
right = false;
down = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
//ticks every 1 sec
if (pic.Location == head.Location)
{
score++;
spawnFood();
tails.Add(addTails());
}
sortLocation();
if (right == true)
{
int r = head.Location.X + head.Height;
head.Location = new Point(r, head.Location.Y);
}
else if(left == true)
{
int l = head.Location.X - head.Height;
head.Location = new Point(l, head.Location.Y);
}
else if (top == true)
{
int t = head.Location.Y - head.Height;
head.Location = new Point(head.Location.X, t);
}
else if (down == true)
{
int d = head.Location.Y + head.Height;
head.Location = new Point(head.Location.X,d);
}
txtScore.Text = score.ToString();
}
private void sortLocation()
{
if (tails.Count == 0)
{
}
else
{
for (int i = 1; i < tails.Count; i++)
{
tails[i].Location = tails[i-1].Location;
}
tails[0].Location = head.Location;
}
}
private PictureBox addTails()
{
PictureBox tail = new PictureBox();
tail.Name = "tail" + score.ToString();
tail.BackColor = Color.Black;
tail.Width = 10;
tail.Height = 10;
this.Controls.Add(tail);
return tail;
}
private void spawnFood()
{
Random rnd = new Random();
int rndLocationX = rnd.Next(10, 50);
int rndLocationY = rnd.Next(10, 50);
pic.BackColor = Color.Red;
pic.Height = 10;
pic.Width = 10;
this.Controls.Add(pic);
if (rndLocationX >= 500)
{
rndLocationX -= 10;
}
if (rndLocationY >= 500)
{
rndLocationY -= 10;
}
pic.Location = new Point(rndLocationX*10,rndLocationY*10);
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
spawnFood();
}
}
}

for (int i = 1; i < tails.Count; i++)
{
tails[i].Location = tails[i+1].Location;
}
tails[0].Location = head.Location;
Are your tails there just stacked so to speak? That's another thought I might be thinking. I changed the tails[i-1] to tails [i+1]. Check if that does the trick.

Related

Moving Pictureboxes from left to right

I have 4 pictureBoxes. I need they move until hit right side and then move to left side and again. But after 1st picturebox hit left side other move closer to him. How fix it ??
link on video with problem
int changePositionX;
bool change = true;
private void timer1_Tick(object sender, EventArgs e)
{
foreach (Control x in this.Controls)
{
if (x is PictureBox && x.Tag != null && x.Tag.ToString() == "enemy")
{
if (x.Location.X < 750 && change == true)
{
changePositionX = x.Location.X + 50;
x.Location = new Point(changePositionX, x.Location.Y);
}
else
{
change = false;
}
if(x.Location.X >= 100 && change == false)
{
changePositionX = x.Location.X - 50;
x.Location = new Point(changePositionX, x.Location.Y);
}
else
{
change = true;
}
}
}
}
Try it like this instead:
private int jumpDistance = 50;
private bool goingRight = true;
private List<PictureBox> PBs = new List<PictureBox>();
private void Form1_Load(object sender, EventArgs e)
{
foreach (PictureBox pb in this.Controls.OfType<PictureBox>())
{
if (pb.Tag != null && pb.Tag.ToString() == "enemy")
{
PBs.Add(pb);
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (goingRight)
{
if (PBs.Any(pb => pb.Location.X >= 750))
{
goingRight = !goingRight;
}
}
else
{
if (PBs.Any(pb => pb.Location.X < 100))
{
goingRight = !goingRight; ;
}
}
foreach (PictureBox pb in PBs)
{
if (goingRight)
{
pb.Location = new Point(pb.Location.X + jumpDistance, pb.Location.Y);
}
else // going left
{
pb.Location = new Point(pb.Location.X - jumpDistance, pb.Location.Y);
}
}
}
Here's my code doing its thing with four PictureBoxes:

clearing pictureboxes from a list array

im trying to clear the picturebox that are created from a list in my game once it has ended, i have cleared the list but the picturebox still display i have tried using the below code in a timer that is enable upon the game ending
foreach (Invader ship in invaders)
{
ship.isDisposed = true;
ship.ship.Dispose();
}
this doesnt do anyhting tho so does anyone have any ideas how i could do this?
public partial class Form1 : Form
{
private List<Invader> invaders = new List<Invader>();
private List<Laser> lasers = new List<Laser>();
int invaderNumber = 0;
int score = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// moves the spacefighter up when clicked
if (e.KeyCode.Equals(Keys.W))
{
if (SpaceFighter.Top > 0)
{
SpaceFighter.Top = SpaceFighter.Top - 30;
}
}
// moves the spacefighter left when clicked
if (e.KeyCode.Equals(Keys.A))
{
if (SpaceFighter.Left > 0)
{
SpaceFighter.Left = SpaceFighter.Left - 10;
}
}
// moves the spacefighter right when clicked
if (e.KeyCode.Equals(Keys.D))
{
if (SpaceFighter.Right < this.Width)
{
SpaceFighter.Left = SpaceFighter.Left + 10;
}
}
// moves the spacefighter down when clicked
if (e.KeyCode.Equals(Keys.S))
{
if (SpaceFighter.Bottom < this.Height - 10)
{
SpaceFighter.Top = SpaceFighter.Top + 10;
}
}
// fires lasers when clicked
if (e.KeyCode.Equals(Keys.Space))
{
System.Media.SoundPlayer LaserSound = new System.Media.SoundPlayer(Properties.Resources.LaserSound);
LaserSound.Play();
this.lasers.Add(new Laser(this, SpaceFighter));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
// introduces 10 enemies once the game starts
if (invaderNumber > 9 )
{
timer1.Enabled = false;
timer2.Enabled = true;
}
else
{
invaders.Add(new Invader(this));
invaderNumber++;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
// detects if the enemy ship interacts with the spacefighter and ends the game if this happens
invaders.RemoveAll(ship => ship.isDisposed);
foreach(Invader ship in invaders)
{
ship.MoveInvader(this);
if (SpaceFighter.Bounds.IntersectsWith(ship.ship.Bounds))
{
// plays sound for exploding ship
System.Media.SoundPlayer SpaceshipSound = new System.Media.SoundPlayer(Properties.Resources.SpaceshipSound);
SpaceshipSound.Play();
timer2.Enabled = false;
timer3.Enabled = false;
timer4.Enabled = true;
invaders.Clear();
listBox1.Items.Add(lblScore.Text); // adds score to listbox
MessageBox.Show("You Lose!");
return;
}
}
// detects if an enemy ship his hit by a laser
lasers.RemoveAll(laser => laser.isDisposed);
foreach (Laser laser in lasers)
{
laser.MoveLaser(this);
foreach (Invader ship in invaders)
{
if (laser.laser.Bounds.IntersectsWith(ship.ship.Bounds))
{
laser.isDisposed = true;
laser.laser.Dispose();
ship.isDisposed = true;
ship.ship.Dispose(); // makes the ship dissappear once its shot
System.Media.SoundPlayer ShipSound = new System.Media.SoundPlayer(Properties.Resources.EnemySound); // sound for the enemy ship being destroyed
ShipSound.Play();
score = score + 2; //adds 2 points to players score if enemy is hit
lblScore.Text = score.ToString(); //updates the score label
invaderNumber = invaderNumber - 1;
}
}
}
foreach (Invader ship in invaders)
{
if (ship.ship.Top > 485)
{
ship.isDisposed = true;
ship.ship.Dispose();
invaderNumber = invaderNumber - 1;
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
timer1.Enabled = true; // activates timer 1
timer3.Enabled = true; // activates timer 3
btnStart.Visible = false; // hidesthe start button
lblScore.Text = "0"; // updates score label to 0 for start of game
lblName.Text = txtName.Text; // updates the name label to user nput
txtName.Visible = false; // hides the textbox
lblEnterName.Visible = false; // hides the enter name label
SpaceFighter.Visible = true; // makes the spacefighter visible
}
// code for the countdown clock
int m = 2;
int s = 60;
private void timer3_Tick(object sender, EventArgs e)
{
if(s > 0)
{
s = s - 1;
lblTimer.Text = "0" + m.ToString() + ":" + s.ToString();
}
if(s == 0)
{
s = 59;
m = m - 1;
lblTimer.Text = "0" + m.ToString() + ":" + s.ToString();
}
if(s < 10)
{
s = s - 1;
lblTimer.Text = "0" + m.ToString() + ":" + "0" + s.ToString();
}
if (m < 0)
{
listBox1.Items.Add(lblScore.Text + " " + lblName.Text); // adds score to list box
timer4.Enabled = true;
invaders.Clear();
}
if (m >= 0)
{
timer1.Enabled = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
SpaceFighter.Visible = false; // hides the space fighter until the player starts the game
listBox1.Visible = false; // keepsscore table hidden
lblScoreTable.Visible = false; // score table lables kept hidden
lblNameTable.Visible = false;
btnMenu.Visible = false;
}
private void Timer4_Tick(object sender, EventArgs e)
{
lblTimer.Text = "00:00"; // sets game timer to 00:00
timer3.Enabled = false; // disbales timer 3
listBox1.Visible = true; // makes score card visible
listBox1.Sorted = true;
lblNameTable.Visible = true; // displays score table labels
lblScoreTable.Visible = true;
btnMenu.Visible = true;
foreach (Invader ship in invaders)
{
ship.isDisposed = true;
ship.ship.Dispose();
}
}
private void BtnMenu_Click(object sender, EventArgs e)
{
// resets game to its original state in order to play another game
m = 2;
s = 60;
lblTimer.Text = "03:00";
timer1.Enabled = false;
timer2.Enabled = false;
timer3.Enabled = false;
timer4.Enabled = false;
listBox1.Visible = false;
lblNameTable.Visible = false;
lblScoreTable.Visible = false;
lblEnterName.Visible = true;
txtName.Visible = true;
SpaceFighter.Visible = false;
btnMenu.Visible = false;
btnStart.Visible = true;
score = 0;
lblScore.Text = "Score";
lblName.Text = "Name";
txtName.Clear();
invaderNumber = 0;
SpaceFighter.Top = 380;
SpaceFighter.Left = 400;
}
}
this would do:
foreach (var control in this.Controls)
{
var pb = control as PictureBox;
if (pb != null)
{
if (pb.Name != "SpaceFighter")
{
pb.Dispose();
this.Controls.Remove(pb);
}
}
}
for the listbox sorting:
using System.Collections;
ArrayList Sorting = new ArrayList();
foreach (var o in listBox1.Items)
{
Sorting.Add(o);
}
Sorting.Sort();
Sorting.Reverse();
listBox1.Items.Clear();
foreach (var o in Sorting)
{
listBox1.Items.Add(o);
}

How would i create an array of images and then load each image into successive indexes

As you can see I've created an array of images, but I'm not sure how to load each image into successive indexes.
Someone told me to do this for my game but I'm not sure how or if it will fix my problem
I've made a game where the character walks left then up then down then right and a few timers that load the animation and handle the movement but when I draw using this code
*e.Graphics.DrawImage(Properties.Resources.Corn_Cobs, 70, 70, 40, 40);*
My character gets really laggy/slow but when I don't draw the image it works smoothly and the character speeds up like normal.
Here is the code:
namespace Rice_Boy_Tester_2
{
public partial class Form1 : Form
{
bool iggy = false;
bool left2 = false;
bool right2 = false;
bool Up2 = false;
bool Down2 = false;
bool Check2 = false;
bool left = false;
bool right = false;
bool Up = false;
bool Down = false;
bool Check = false;
Image[] Animations;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Animations = new Image[6];
Animations[0] = Image.FromFile("Rice-Boy-Walking-Left-Bouncing.gif");
Animations[1] = Image.FromFile("Rice-Boy-Walking-Up-Bouncing.gif");
Animations[2] = Image.FromFile("Rice-Boy-Walking-Right-Bouncing.gif");
Animations[3] = Image.FromFile("Rice-Boy-Walking-Down.gif");
Animations[4] = Properties.Resources.Rice_Boy_Standing_Left;
Animations[5] = Properties.Resources.Corn_Cobs;
}
private void Refresh_Tick(object sender, EventArgs e)
{
this.Refresh();
}
private void PriceBoyWalk_Tick(object sender, EventArgs e)
{
if (left)//Goes Left
{
Player.Left -= 1;
}
if (Player.Left < 170 & Check == false)//checks how far away player is from form
{
left = false;
Up = true;
}
if (Up & Player.Left < 170) //Goes Up
{
Player.Top -= 1;
Check = true;
}
if (Player.Top < 100 & Check)
{
Up = false;
Down = true;
}
if (right)//Goes Right
{
Player.Left += 1;
}
if (Down)//Goes Down
{
Player.Top += 1;
}
if (Player.Top + 150 > this.ClientSize.Height)// When RiceBoy goes down and hits bottom right = true
{
Check = false;
Down = false;
right = true;
}
if (Player.Left + 150 > this.ClientSize.Width)//Stops At Starting Point
{
right = false;
}
}
private void B1_Click(object sender, EventArgs e)
{
this.Paint += new PaintEventHandler(form1_Pad1_Corn);
RiceBoyWalkGif.Enabled = true;
left = true;
left2 = true;
RiceBoyWalk.Enabled = true;}
}
private void form1_Pad1_Corn(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawImage(Properties.Resources.Corn_Cobs, 70, 70, 400, 400);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (left2)
{
Player.Image = Animations[0];
left2 = false;
}
if (Player.Left < 170 & Check2 == false)//checks how far away player is from form
{
left2 = false;
Up2 = true;
}
if (Up2 & Player.Left < 170) //Goes Up
{
this.Player.Size = new System.Drawing.Size(36, 76); // Changes size of the picture box to maintain quaility
Player.Image = Animations[1];//Animates RiceBoyWalkingUp
Check2 = true;
Up2 = false;
}
if (Player.Top < 105 & Check2)//Player.Top < 101 must be +1 greater than the RiceBoyWalkTimer
{
Up2 = false;
Down2 = true;
}
if (right2)
{
this.Player.Size = new System.Drawing.Size(53, 77); // Changes size of the picture box to maintain quaility
Player.Image = Animations[2];//Animates RiceBoyWalkingRight
right2 = false;
}
if (Down2)//Goes Down
{
Player.Image = Animations[3];//Animates RiceBoyWalkingDown
Down2 = false;
}
if (Player.Top + 150 > this.ClientSize.Height)// When RiceBoy goes down and hits bottom riceboy walks right
{
iggy = true;// shows that riceboy is approching the starting point
Check2 = false;
Down2 = false;
right2 = true;
}
if (Player.Left + 150 > this.ClientSize.Width & iggy)//Stops At Starting Point
{
right2 = false;
Player.Image = Animations[4];// Rice boy standing left
}

CountDown check Time

I've made an countdown and i want to add an time check for it now. If the minutes are < 01 and the seconds are != 60 so 00:59 the Time should be orange and if the seconds are then smaller then 10 the time should be red.
But it does not work.
They're always just getting orange if the time is 00:00:58, but why?
private int hours, minutes, seconds;
private bool paused;
private void button_Start_Click(object sender, EventArgs e)
{
button_Pause.Enabled = true;
button_Stop.Enabled = true;
if(paused != true)
{
hours = int.Parse(textBox_Hours.Text);
minutes = int.Parse(textBox_Minutes.Text);
seconds = int.Parse(textBox_Seconds.Text) + 1;
textBox_Hours.Enabled = false;
textBox_Minutes.Enabled = false;
textBox_Seconds.Enabled = false;
button_Start.Enabled = false;
timer_CountDown.Start();
}
}
private void timer_CountDown_Tick(object sender, EventArgs e)
{
if(hours == 0 && minutes < 1)
{
label_Hours.ForeColor = Color.Red;
label_Minutes.ForeColor = Color.Red;
label_Seconds.ForeColor = Color.Red;
label8.ForeColor = Color.Red;
label10.ForeColor = Color.Red;
}
if(hours == 0 && minutes == 0 && seconds == 0)
{
timer_CountDown.Stop();
textBox_Seconds.Enabled = true;
textBox_Minutes.Enabled = true;
textBox_Hours.Enabled = true;
button_Start.Enabled = true;
}
else
{
if (seconds < 1)
{
seconds = 59;
if (minutes < 1)
{
minutes = 59;
if (hours != 0)
{
hours -= 1;
}
}
else
{
minutes -= 1;
}
}
else
{
seconds -= 1;
}
if(hours > 9)
{
label_Hours.Text = hours.ToString();
}
else { label_Hours.Text = "0" + hours.ToString(); }
if(minutes > 9)
{
label_Minutes.Text = minutes.ToString();
}
else { label_Minutes.Text = "0" + minutes.ToString(); }
if(seconds > 9)
{
label_Seconds.Text = seconds.ToString();
}
else { label_Seconds.Text = "0" + seconds.ToString(); }
}
}
The Timer Intervall is 1000.
You're over complicating things. Why not just use the TimeSpan type and get rid of those hours, minutes, seconds?
private TimeSpan countDownTime = TimeSpan.Zero;
private void timer_CountDown_Tick(object sender, EventArgs e)
{
if(countDownTime == TimeSpan.Zero)
{
timer_CountDown.Stop();
textBox_Seconds.Enabled = true;
textBox_Minutes.Enabled = true;
textBox_Hours.Enabled = true;
button_Start.Enabled = true;
return;
}
countDownTime = countDownTime.Add(TimeSpan.FromSeconds(1).Negate());
label_Hours.Text = countDownTime.ToString("hh");
label_Minutes.Text = countDownTime.ToString("mm");
label_Seconds.Text = countDownTime.ToString("ss");
if(countDownTime.TotalSeconds < 10)
{
label_Hours.ForeColor = Color.Red;
label_Minutes.ForeColor = Color.Red;
label_Seconds.ForeColor = Color.Red;
label8.ForeColor = Color.Red;
label10.ForeColor = Color.Red;
}
else if (countDownTime.TotalMinutes < 1)
{
label_Hours.ForeColor = Color.Orange;
label_Minutes.ForeColor = Color.Orange;
label_Seconds.ForeColor = Color.Orange;
label8.ForeColor = Color.Orange;
label10.ForeColor = Color.Orange;
}
}
private void button_Start_Click(object sender, EventArgs e)
{
button_Pause.Enabled = true;
button_Stop.Enabled = true;
if(paused != true)
{
int hours = int.Parse(textBox_Hours.Text);
int minutes = int.Parse(textBox_Minutes.Text);
int seconds = int.Parse(textBox_Seconds.Text) + 1;
this.countDownTime = new TimeSpan(hours,minutes,seconds);
textBox_Hours.Enabled = false;
textBox_Minutes.Enabled = false;
textBox_Seconds.Enabled = false;
button_Start.Enabled = false;
timer_CountDown.Start();
}
}

How can i prevent form the user to move the point beyond the pictureBox1 borders?

This is all the events and functions in my Form1 what should i do ?
I need to detect the borders/bounds of the pictureBox1 control and stop the mouse move when its touching the borders.
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label1.Text = e.X.ToString();
label2.Text = e.Y.ToString();
label1.Visible = true;
label2.Visible = true;
label3.Visible = true;
label4.Visible = true;
// find the index that is closest to the current mouse location
MinDist = float.MaxValue;
for (idx = 0; idx < Point_X.Count; ++idx)
{
float dx = Point_X[idx] - e.X;
float dy = Point_Y[idx] - e.Y;
float dist = (float)Math.Sqrt(dx * dx + dy * dy);
if (dist < MinDist)
{
MinDist = dist;
selectedIndex = idx;
}
}
if (MinDist < 5)
{
mouseMove = true;
OriginalX = Point_X[(int)selectedIndex];
OriginalY = Point_Y[(int)selectedIndex];
if (cyclicSelectedIndex.Count() == 2)
{
cyclicSelectedIndex[currentCyclicIndex] = (int)selectedIndex;
currentCyclicIndex++;
if (currentCyclicIndex > 1)
{
currentCyclicIndex = 0;
}
if ((cyclicSelectedIndex[0] == cyclicSelectedIndex[1]) || (cyclicSelectedIndex[0] == -1) || (cyclicSelectedIndex[1] == -1))
{
button2.Enabled = false;
}
else
{
button2.Enabled = true;
}
label13.Text = selectedIndex.ToString();
label13.Visible = true;
label14.Visible = true;
listView1.Items.Add(selectedIndex.ToString()).EnsureVisible();
}
}
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseMove == true)
{
Point NewPoint = e.Location;
Point_X[(int)selectedIndex] = NewPoint.X;
Point_Y[(int)selectedIndex] = NewPoint.Y;
label1.Text = NewPoint.X.ToString();
label2.Text = NewPoint.Y.ToString();
pictureBox1.Refresh();
Point_X[(int)selectedIndex] = Math.Max(pictureBox1.Left, Math.Min(pictureBox1.Right, NewPoint.X));
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (mouseMove == true)
{
Point NewPoint = e.Location;
Point_X[(int)selectedIndex] = NewPoint.X;
Point_Y[(int)selectedIndex] = NewPoint.Y;
mouseMove = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
halfX = pictureBox1.ClientRectangle.Width / 2;
halfY = pictureBox1.ClientRectangle.Height / 2;
Random rnd = new Random();
offsetX = rnd.Next(-10, 10);
offsetY = rnd.Next(-10, 10);
addPoint(halfX + offsetX, halfY + offsetY);
pictureBox1.Refresh();
numberOfPoints++;
label16.Text = numberOfPoints.ToString();
label16.Visible = true;
label15.Visible = true;
}
private void addPoint(float x , float y)
{
Point_X.Add(x);
Point_Y.Add(y);
label5.Text = x.ToString();
label6.Text = y.ToString();
label5.Visible = true;
label6.Visible = true;
label7.Visible = true;
label8.Visible = true;
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Point connectionPointStart;
Point connectionPointEnd;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
SolidBrush brush = new SolidBrush(Color.Red);
Pen p = new Pen(brush);
for (int idx = 0; idx < Point_X.Count; ++idx)
{
Point dPoint = new Point((int)Point_X[idx], (int)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 < connectionStart.Count; i++)
{
int startIndex = connectionStart[i];
int endIndex = connectionEnd[i];
connectionPointStart = new Point((int)Point_X[startIndex], (int)Point_Y[startIndex]);
connectionPointEnd = new Point((int)Point_X[endIndex], (int)Point_Y[endIndex]);
p.Width = 4;
g.DrawLine(p, connectionPointStart, connectionPointEnd);
}
}
private void button2_Click(object sender, EventArgs e)
{
addConnection(cyclicSelectedIndex[0], cyclicSelectedIndex[1]);
pictureBox1.Invalidate();
}
private void addConnection(int i, int j)
{
if (cyclicSelectedIndex[0] == -1)
{
return;
}
if (cyclicSelectedIndex[0] == cyclicSelectedIndex[1])
{
return;
}
if (connectionStart.Count() == 0)
{
connectionStart.Add(i);
connectionEnd.Add(j);
//label12.Text = i.ToString();
//label11.Text = j.ToString();
label12.Text = connectionStart[0].ToString();
label11.Text = connectionEnd[0].ToString();
label9.Visible = true;
label10.Visible = true;
label11.Visible = true;
label12.Visible = true;
return;
}
for (int f = 0; f < connectionStart.Count(); f++)
{
if ((connectionStart[f] == i && connectionEnd[f] == j) || (connectionStart[f] == j && connectionEnd[f] == i)) // this checking dosent work good !
{
button2.Enabled = false;
return;
}
else
{
label12.Text = connectionStart[f].ToString();
label11.Text = connectionEnd[f].ToString();
label9.Visible = true;
label10.Visible = true;
label11.Visible = true;
label12.Visible = true;
}
}
connectionStart.Add(i);
connectionEnd.Add(j);
}
Im not sure how to explain it. But how can i do it ?
Thanks.
When calculating the X value use Math.Max with that and the left boundary of the image, and Min on the right boundary of the image. Do the same with the Y value on the Top and Bottom.
Point_X[(int)selectedIndex] = Math.Max(imageLeft, Math.Min(imageRight, NewPoint.X));
I assume you can determine the left/right/top/bottom of the image?

Categories