Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Draw graphic object and place button on same form? 1

Status
Not open for further replies.

mainit123

Technical User
Jan 24, 2005
25
I am using Visual Basic .NET, Version 7.0.9955 that comes with the Michael
Halvorsen Book, "Microsoft Visual Basic .NET - Step by Step." I assume what I am trying to do is doable in Visual Basic unless someone can tell me there just isn't a way to accomplish this task.

I am trying to dynamically draw graphic objects (circle, arc, lines, etc.) on the same form where I have dynamically placed a button, checkbox, or other control
object. The goal is to have this graphic object drawn and the control object placed on a form created dynamically at runtime, not ahead of time.

It is pretty straightforward to successfully draw any graphic object on my Form1 that is provided by default anytime a project is started. However, I am not finding a way to create a Form2 "on the fly" on which I can
both draw a graphic object AND add a control.

My attached code simply draws an ellipse on the standard Form1 that comes with every Visual Basic .Net project then the new button control gets placed on a new form (also called Form1) that overlays the original Form1 on which
the ellipse was drawn. What I need is to draw the ellipse and place the button on the same form at run time.

I would appreciate knowing if it is even possible to draw a graphic object on the same form where a control such as a button is also placed. The goal is to do this dynamically, at runtime, as opposed to setting up the Form1 ahead of time with these objects. Preferably, I want the graphic object and control to be on a NEW form (formnew) that is created when a Menu Item is clicked or
some other standard event is done on Form1.

Thanks for any responses.

Here is my code:


Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class Form1
Inherits System.Windows.Forms.Form

Dim imgType As Integer = 0
#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu()
Me.MenuItem1 = New System.Windows.Forms.MenuItem()
Me.MenuItem2 = New System.Windows.Forms.MenuItem()
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2})
Me.MenuItem1.Text = "Main"
'
'MenuItem2
'
Me.MenuItem2.Index = 0
Me.MenuItem2.Text = "Ellipse"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
If imgType = 1 Then
Dim pn As Pen = New Pen(Color.Blue, 100)
Dim rect As Rectangle = New Rectangle(50, 50, 200, 100)
g.DrawEllipse(pn, rect)
End If
End Sub

'Try to draw ellipse and add the button on same form
Private Sub MenuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
MsgBox("We are at menu item 1")
Dim Form1 As New Form() 'This has to be declared
'or a error is displayed
Dim lblnew As New Label()
Dim btnew As New Button()
lblnew.Text = "Hi there"
lblnew.Size = New Size(150, 50)
lblnew.Location = New Point(80, 50)
btnew.Text = "New button here"
btnew.Location = New Point(110, 100)
Form1.Controls.Add(lblnew)
Form1.Controls.Add(btnew)
Form1.ShowDialog() 'This shows only the button
'and label when the code is run
imgType = 1 'This ellipse gets drawn on the
'original Form1, not on the new Form1
'Invalidate()
End Sub

End Class
 
Yes, you can do this.

First, change the declaration of the imgType variable in Form1 to Public:

Public imgType As Integer = 0

This is so it will be visible outside of the Form1 class, so it can be set when a new instance of this class is instantiated.

Next, change the definition of the new Form1 object in the MenuItem2_Click sub:

Dim Form1 As New Form1()

Note that the definition is changed from New Form() to New Form1(). Before it just gave you a generic form object, without any of the code you added to your Form1 class. Now, the object created has all of the code you added.

Finally, move the line where you set the value of imgType to before where you call the new Form1:

Old: Form1.ShowDialog()
imgType = 1

New: Form1.imgType = 1
Form1.ShowDialog()

You need to set the value of imgType before you show the new Form1 or else it will have the default value of 0 when it shows, and the graphics will not be drawn.

Note that the new code sets the value of Form1.imgType, not just imgType. This is so that the value of imgType in the newly created Form1 is set, and not the value of imgType in the current Form1.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
To jebenson-
I can't thank you enough for this quick and soooo helpful response. You can't believe what a roadblock you have pushed aside for me and I truly appreciate it. In the spirit of this forum, I will do all I can do help fellow users, even though my expertise is somewhat limited.
Thanks again and know that you have made a BIG difference in my development career with just one post.

Joel Stevens
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top