-
Sep 24th, 2012, 11:23 AM
#1
Thread Starter
Member
[RESOLVED] "Name" A Shape
Hello
I have this code:
Code:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.FillEllipse(Brushes.Blue, 100, 100, 100, 100)
End Sub
How do I use this shape in the code? For example I want to be able to move it around using keydown events.
Thanks
-
Sep 24th, 2012, 11:33 AM
#2
Re: "Name" A Shape
Rather than painting it onto the form, paint it onto a panel and move the panel rather than the shape.
-
Sep 24th, 2012, 11:37 AM
#3
Thread Starter
Member
Re: "Name" A Shape
Originally Posted by dday9
Rather than painting it onto the form, paint it onto a panel and move the panel rather than the shape.
Thanks, but how can i create a panel in the code and not in the designer?
Edit: Would i use the CreateObject thing?
-
Sep 24th, 2012, 11:41 AM
#4
Re: "Name" A Shape
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Declare a new instance of a panel
Dim pnl As New Panel
'Set the panel's properties
With pnl
.Size = New Size(100, 100)
.Location = New Point(5, 5)
End With
'Add the panel's paint event
AddHandler pnl.Paint, AddressOf pnl_Paint
'Add the control to the form
Me.Controls.Add(pnl)
End Sub
Private Sub pnl_Paint(sender As Object, e As PaintEventArgs)
e.Graphics.FillEllipse(Brushes.Blue, 100, 100, 100, 100)
End Sub
End Class
-
Sep 24th, 2012, 11:45 AM
#5
Re: "Name" A Shape
I've tried editing the post, but I'm having problems doing so... here is the corrected way, I messed up on the new paint_event
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Declare a new instance of a panel
Dim pnl As New Panel
'Set the panel's properties
With pnl
.Size = New Size(100, 100)
.Location = New Point(100, 100)
End With
'Add the panel's paint event
AddHandler pnl.Paint, AddressOf pnl_Paint
'Add the control to the form
Me.Controls.Add(pnl)
End Sub
Private Sub pnl_Paint(sender As Object, e As PaintEventArgs)
e.Graphics.FillEllipse(Brushes.Blue, 0, 0, 100, 100)
End Sub
End Class
-
Sep 24th, 2012, 12:31 PM
#6
Re: "Name" A Shape
The only other alternative to this is to simply paint the shape at a different place in response to the appropriate key presses. The panel idea would probably work better, and Pictureboxes rather than panels are another option. However, if there are enough shapes at any one time, it may prove to be better to do all the drawing in the Paint event rather than control a whole bunch of different controls.
My usual boring signature: Nothing
-
Sep 24th, 2012, 01:13 PM
#7
Thread Starter
Member
Re: "Name" A Shape
Originally Posted by dday9
I've tried editing the post, but I'm having problems doing so... here is the corrected way, I messed up on the new paint_event
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Declare a new instance of a panel
Dim pnl As New Panel
'Set the panel's properties
With pnl
.Size = New Size(100, 100)
.Location = New Point(100, 100)
End With
'Add the panel's paint event
AddHandler pnl.Paint, AddressOf pnl_Paint
'Add the control to the form
Me.Controls.Add(pnl)
End Sub
Private Sub pnl_Paint(sender As Object, e As PaintEventArgs)
e.Graphics.FillEllipse(Brushes.Blue, 0, 0, 100, 100)
End Sub
End Class
The code works great, but how can I use the shape in another Sub, for example form1_keydown? I tried to put the code into a public sub and Call it in form1_load, then use it in form1_keydown, but it cant be found. Any help on making it global? The thing I want do do is to move the shape around. Again, thanks.
-
Sep 24th, 2012, 01:21 PM
#8
Re: "Name" A Shape
I think you may want to try shaggy's suggestion then. Take a look at this:
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private pt As New Point(100, 100)
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Left Then
pt.X -= 5
ElseIf e.KeyCode = Keys.Right Then
pt.X += 5
ElseIf e.KeyCode = Keys.Up Then
pt.Y -= 5
ElseIf e.KeyCode = Keys.Down Then
pt.Y += 5
End If
Me.Refresh()
End Sub
Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawEllipse(Pens.Blue, New Rectangle(pt, New Size(100, 100)))
End Sub
End Class
-
Sep 24th, 2012, 01:31 PM
#9
Thread Starter
Member
Re: "Name" A Shape
Works perfectly, thank you both dday9 and Shaggy Hiker!
Looks like i cant add this to your rep dday9, you've helped me too much already
-
Sep 24th, 2012, 02:49 PM
#10
Re: [RESOLVED] "Name" A Shape
The way you would make the initial code that dday posted global would be to move the declaration of pnl out of the sub and make it a form level variable. Then pnl would be available to any sub on the form.
I've worked with this kind of thing both ways. I was working on a program that had lots of drag and drop, but the number and location of the items was quite variable. I used Pictureboxes for those items, as dragging and dropping works best with controls. However, inside many of the pictureboxes could be up to eight other items that could be clicked, along with a few other items that could be dragged around. Initially, I went with Pictureboxes inside Pictureboxes, for the purpose of the drag and drop. I then realized that the eight items couldn't be dragged, but only clicked. What worked best for them was to draw them directly into the Picturebox that contained them, and not use a control at all. I also started out with the items that could be drawn as controls in the Picturebox, but that meant creating an arbitrarily large number of controls, which became a nightmare to manage, and all of which cost time and memory. At that point, I just started drawing things into the Pictureboxes.
Based on this experience, I would say that using controls, and moving them, works well for a small number of items. Once you get into the dozens, though, the cost of creating, maintaining, and manipulating, the controls begins to outweigh the benefits.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|