' RandomLines Example ' Use the Graphics context drawing methods to draw ' a picture with random lines in the Paint event handler. Public Class Form1 Inherits System.Windows.Forms.Form #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 picDisplay As System.Windows.Forms.PictureBox Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents mnuBrushes As System.Windows.Forms.MenuItem Friend WithEvents mnu1 As System.Windows.Forms.MenuItem Friend WithEvents mnu2 As System.Windows.Forms.MenuItem Friend WithEvents mnu5 As System.Windows.Forms.MenuItem Private Sub InitializeComponent() Me.picDisplay = New System.Windows.Forms.PictureBox Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.mnuBrushes = New System.Windows.Forms.MenuItem Me.mnu1 = New System.Windows.Forms.MenuItem Me.mnu2 = New System.Windows.Forms.MenuItem Me.mnu5 = New System.Windows.Forms.MenuItem Me.SuspendLayout() ' 'picDisplay ' Me.picDisplay.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.picDisplay.Location = New System.Drawing.Point(192, 88) Me.picDisplay.Name = "picDisplay" Me.picDisplay.Size = New System.Drawing.Size(80, 64) Me.picDisplay.TabIndex = 0 Me.picDisplay.TabStop = False ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuBrushes}) ' 'mnuBrushes ' Me.mnuBrushes.Index = 0 Me.mnuBrushes.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnu1, Me.mnu2, Me.mnu5}) Me.mnuBrushes.Text = "Number of Colors" ' 'mnu1 ' Me.mnu1.Index = 0 Me.mnu1.Text = "1" ' 'mnu2 ' Me.mnu2.Index = 1 Me.mnu2.Text = "2" ' 'mnu5 ' Me.mnu5.Index = 2 Me.mnu5.Text = "5" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(328, 266) Me.Controls.Add(Me.picDisplay) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "RandomPoints Example" Me.ResumeLayout(False) End Sub #End Region Dim p() As Pen = New Pen() _ {New Pen(Color.Black, 2), _ New Pen(Color.White, 2), _ New Pen(Color.Red, 2), _ New Pen(Color.Lime, 2), _ New Pen(Color.Blue, 2)} Dim nColors As Integer = 1 Dim nLines As Integer = 200 Private Sub ResizePictureBox() picDisplay.Location = New Point(0, 0) picDisplay.Size = Me.ClientSize End Sub Private Sub ResizePictureBox(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load, MyBase.Resize ResizePictureBox() End Sub Private Sub Form1_Resize(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Resize ResizePictureBox() End Sub Private Sub picDisplay_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) Handles picDisplay.Paint Dim g As Graphics = e.Graphics Dim r As New Random Dim c, i, x1, y1, x2, y2 As Integer Dim w As Integer = picDisplay.Width Dim h As Integer = picDisplay.Height For i = 1 To nLines x1 = r.Next(0, w) y1 = r.Next(0, h) x2 = r.Next(0, w) y2 = r.Next(0, h) c = r.Next(0, nColors) g.DrawLine(p(c), x1, y1, x2, y2) Next End Sub Private Sub mnu1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnu1.Click nColors = 1 picDisplay.Invalidate() End Sub Private Sub mnu2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnu2.Click nColors = 2 picDisplay.Invalidate() End Sub Private Sub mnu5_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnu5.Click nColors = 5 picDisplay.Invalidate() End Sub End Class