Create rectangle object and move it

AthlonRob
10-05-2006, 01:36 PM
I want to create a rectangle as an object, and move it around the screen using object.left, object.top, object.location, etc.

Here is my object:


Dim box As System.Drawing.Graphics
cbox = Me.CreateGraphics
box.FillRectangle(Brushes.White, 10, 245, 10, 10)


but I guess this thing I made is not a true object, so I can't use the left, top, and location properties. How do you do this?

Thanks

wayneph
10-05-2006, 01:45 PM
I'm a WebForms programmer more than a WinForms programmer, so please take this with a grain of salt...

You could draw on a Picture Box or Panel, and move that around instead of just the Rectangle.

AthlonRob
10-05-2006, 01:52 PM
I actually created a 10 x 10 jpg in mspaint, and used that as by picture box. Can I create a picturebox and just fill it in (with white or whatever) without referencing an external image file?

wayneph
10-05-2006, 01:54 PM
i believe so. You should be able to either just change the back color, or use FillRectangle like you were doing.

AthlonRob
10-05-2006, 03:09 PM
I am using a picturebox as we decided above, but it's not being created, or at least it's not visible. Odd.


Dim box As New PictureBox
box.BackColor = Color.Pink
box.Height = 10
box.Width = 10
box.Location = New Point(50, 50)
box.Show()
box.Visible = True


I get the feeling this should be something simple :)

AthlonRob
10-06-2006, 07:11 AM
I just had a though this morning driving to work. I am creating my form and creating this object by putting that code in the form's PAINT event. I was reading in my book that it is a good idea to do that since whatever you create will disappear if you minimize or anything. Is it possible that by doing that, I am overwriting my rectangle?

wayneph
10-06-2006, 08:26 AM
I probably wouldn't put it in the Paint event. Since you're just using properties of the PictureBox now, it should take care of all the painting issues. You should be able to set all the properties in the forms Init or Load events. Then when you are moving it, repainting should happen automatically.

Just for testing, I created a Form with a PictureBox and a couple buttons to move it around. Everything seems to be working fine.

AthlonRob
10-06-2006, 08:55 AM
I can do the same with a picturebox on the form, but I don't want that. I want to create the picturebox within the code. That way I can dynamically create pictureboxes in differnet places with different properties. Shouldn't I be able to do this?


dim box1 as new picturebox


I should then have a picturebox object named box1, right?

wayneph
10-06-2006, 10:22 AM
box1 needs to be declared at the form level. Not in a method.
You also need to add it to the form's control collection.

Me.Controls.Add(box1)

Then you should be able to control it.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum