0

I've made a picturebox.

public void create(Form1 u, int number, int x, int y)
{
    pictureBox1 = new PictureBox();
    pictureBox1.Location = new Point(x, y);
    pictureBox1.Name = "invader" + number;
    pictureBox1.Size = new Size(60, 54);
    pictureBox1.Image = Image.FromFile("../sprite.jpg");
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    u.Controls.Add(pictureBox1);
}

How do I move this by like 1 pixel every 20 milliseconds? I got a timer and I've tried a few things but I do not know how to properly select the picture box that this class had made so I can move it (it all has to be written within this class) I want the box to move down. The X coordinate should stay the same.

private void timer1_Tick(object sender, EventArgs e)
{

}

My super exciting timer code. It is located in the main form code.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Chaoss1848
  • 19
  • 7
  • Where is the timer code? – Steve May 16 '17 at 09:24
  • added the timer code – Chaoss1848 May 16 '17 at 09:27
  • just set the position in the tick ? I also recommend using `System.Timers.Timer` it offers an `Elapsed` event for the defined `Interval`. – Felix D. May 16 '17 at 09:29
  • I know how to change the position and all. but i dont know how to target the picturebox that I made specifically. I tried just doing pictureBox1.Top or .Location etc but none of that worked. – Chaoss1848 May 16 '17 at 09:30
  • @Chaoss1848 `... I tried just doing pictureBox1.Top or .Location etc ...` that's good. Can you show us your attempt with `Location`? – default locale May 16 '17 at 09:32
  • try calling `Application.DoEvents()` after each change. It will notify the UI that something has changed. It's not really efficient to be honest... – Felix D. May 16 '17 at 09:32
  • By the way, have you called `timer1.Start()`? – default locale May 16 '17 at 09:34
  • ` public void move(int x) { pictureBox1.Location = new Point(x, pictureBox1.Location.Y+1); } ` – Chaoss1848 May 16 '17 at 09:35
  • Looks OK at first glance. What's an error message/type? – default locale May 16 '17 at 09:36
  • {"Object reference not set to an instance of an object."} Nullreference exception – Chaoss1848 May 16 '17 at 09:37
  • Weird. This means that [something's null](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). Can you check which one of objects is `null`: `pictureBox1` or `pictureBox1.Location` or something else? – default locale May 16 '17 at 09:39
  • I don't know how to check it but I think it's the Location thats Null. X cant be null as I give a X value when I call the method. and the picturebox has been made above this method (the code I gave in the beginning) – Chaoss1848 May 16 '17 at 09:42
  • There're multiple ways to check it. The most simplistic is probably: `if(pictureBox1==null) MessageBox.Show("pictureBox1 is null");` – default locale May 16 '17 at 09:44
  • You are just making it unnecessarily difficult on yourself. A create() method should have a return type so it can return the object that was created. Now you have a shot at adding it to a `List invaders` to keep track of the invaders. Which you now can trivially iterate in the Tick event handler to move them. – Hans Passant May 16 '17 at 09:45
  • I think i've put them into a list correctly now. atleast not getting errors. but how do I move the list now.. – Chaoss1848 May 16 '17 at 09:58

1 Answers1

2

You have created the PictureBox dynamically and the variable used is a local variable, so after exiting the creation code you have no more a direct reference to the picturebox created.
However you have still the possibility to extract the picturebox from the controls collection of the form u

private void timer1_Tick(object sender, EventArgs e)
{ 
    PictureBox pic = u.Controls.OfType<PictureBox>().FirstOrDefault(x => x.Name = "invader" + currentPicNumber;
    if(pic != null) pic.Location = new Point(pic.Location.X, pic.Location.Y + 1);
}

Now we should resolve two problems.
What value for currentPicnumber and you should have a reference to the instance of Form1 u where you have added the PictureBox.

These problems could be solved with a class level variable with the reference to the Form1 (I suppose that you have already this reference) and another variable that keeps the number of the current picturebox that you want to move.

Instead if you need to move more than one PictureBox added dynamically you could extract all the pictureboxes that whose name start with the "invader" text and loop over them

private void timer1_Tick(object sender, EventArgs e)
{ 
    var picList = u.Controls.OfType<PictureBox>().FirstOrDefault(x => x.Name.StartsWith("invader");
    foreach(PictureBox pic in picList)
        pic.Location = new Point(pic.Location.X, pic.Location.Y + 1);
}
Steve
  • 213,761
  • 22
  • 232
  • 286