Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Graphics on a Separate Form

 

New Items

Example Code

Initial Form1

Initial Form2

Form1 at Run Time

Form2 at Run Time

 

 

 

New Items

 

Ø Project, Add Windows Form

Ø FormName.Show at start of graphics on the new form

Ø Prefix properties with name of the form; otherwise will defalt to Form1

Ø Prefix main form properties with Me

Ø Place items that you want to load before running program in the Form Load procedure

 

Example Code

 

Public Class Form1

    'Instance Variables

    Private XStart As Integer

    Private YStart As Integer

    Private XEnd As Integer

    Private YEnd As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Make Form2 Visible

        Form2.Show()       

        'Get line parameters supplied by user             

        XStart = StartX.Text

        YStart = StartY.Text

        XEnd = EndX.Text

        YEnd = EndY.Text

        'Get the graphics object

        Dim G As Graphics

        G = Form2.CreateGraphics

        'Set dimensions of Form2 - could also set the dimensions using user input as above

        Form2.Height = 400

        Form2.Width = 400

        Form2.Text = "Lines"                                                'Caption at top of the second form

        Dim pen As New Pen(Color.Black)                             'Setting properties of the pen used for the graphics

        G.DrawLine(pen, XStart, YStart, XEnd, YEnd)             'Draw line - note  that G above was declared for Form2

    End Sub

    'Items that you want to occur when program starts should be placed in the Form Load procedure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Text = "Start Form"

        Me.BackColor = Color.AliceBlue

        Me.ForeColor = Color.Blue

    End Sub

End Class

Initial Form1

 

 

Initial Form2

 

 

Form1 at Run Time (After data input and before clicking Start)

 

 

Form2 at Run Time (After clicking start on Form1)