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
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