We read your photo as:

Design a Visual Basic application with five buttons to draw different shapes (circle, line, rectangle, triangle) and a button to close the application. Include code for the form's Paint event.

Computer Science

ScanSolve AI Answer

Step-by-step solution

3 steps

Okay, let's complete the Visual Basic application.

  1. Design the Form (continued)

    • Drag and drop five Button controls onto Form1.
    • Set the Name and Text properties for each button as follows:
      • Button 1: Name = btnBlueCircle, Text = BlueCircle
      • Button 2: Name = btnRedLine, Text = RedLine
      • Button 3: Name = btnGreenRectangle, Text = GreenRectangle
      • Button 4: Name = btnMyTriangle, Text = MyTriangle
      • Button 5: Name = btnCloseApp, Text = CloseApp
  2. Add Code to the Form

    Double-click on the form itself (not a button) to create the Form1_Load event, or select the form and go to its events. We will also need a Paint event for the form to handle drawing.

    First, declare a variable at the class level to keep track of which shape to draw.

    Public Class Form1
        Private shapeToDraw As String = "" ' Stores the name of the shape to draw
    

    Next, add the Form1_Paint event handler. This event is responsible for drawing on the form.

        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            ' Create a Graphics object from the PaintEventArgs
            Dim g As Graphics = e.Graphics
    
            ' Define pens for drawing
            Dim bluePen As New Pen(Color.Blue, 3)
            Dim redPen As New Pen(Color.Red, 3)
            Dim greenPen As New Pen(Color.Green, 3)
            Dim blackPen As New Pen(Color.Black, 3)
    
            ' Define coordinates and sizes for shapes
            Dim circleRect As New Rectangle(150, 50, 100, 100) ' x, y, width, height
            Dim lineStart As New Point(150, 200)
            Dim lineEnd As New Point(300, 200)
            Dim rectCoords As New Rectangle(150, 250, 120, 80)
    
            ' Define points for the triangle
            Dim trianglePoints As Point() = { _
                New Point(200, 350), _
                New Point(250, 400), _
                New Point(200, 400) _
            }
    
            ' Draw the selected shape based on the shapeToDraw variable
            Select Case shapeToDraw
                Case "BlueCircle"
                    g.DrawEllipse(bluePen, circleRect)
                Case "RedLine"
                    g.DrawLine(redPen, lineStart, lineEnd)
                Case "GreenRectangle"
                    g.DrawRectangle(greenPen, rectCoords)
                Case "MyTriangle"
                    g.DrawPolygon(blackPen, trianglePoints)
            End Select
    
            ' Dispose of the pens to release resources
            bluePen.Dispose()
            redPen.Dispose()
            greenPen.Dispose()
            blackPen.Dispose()
        End Sub
    
  3. Add Code for Button Click Events

    Double-click each button in the designer to create its Click event handler.

    • BlueCircle Button:

      Private Sub btnBlueCircle_Click(sender As Object, e As EventArgs) Handles btnBlueCircle.Click
          shapeToDraw = "BlueCircle"
          Me.Invalidate() ' Redraw the form, triggering the Paint event
      End Sub
      
    • RedLine Button:

      Private Sub btnRedLine_Click(sender As Object, e As EventArgs) Handles btnRedLine.Click
          shapeToDraw = "RedLine"
          Me.Invalidate() ' Redraw the form, triggering the Paint event
      End Sub
      
    • GreenRectangle Button:

      Private Sub btnGreenRectangle_Click(sender As Object, e As EventArgs) Handles btnGreenRectangle.Click
          shapeToDraw = "GreenRectangle"
          Me.Invalidate() ' Redraw the form, triggering the Paint event
      End Sub
      
    • MyTriangle Button:

      Private Sub btnMyTriangle_Click(sender As Object, e As EventArgs) Handles btnMyTriangle.Click
          shapeToDraw = "MyTriangle"
          Me.Invalidate() ' Redraw the form, triggering the Paint event
      End Sub
      
    • CloseApp Button:

      Private Sub btnCloseApp_Click(sender As Object, e As EventArgs) Handles btnCloseApp.Click
          Me.Close() ' Closes the current form and exits the application
      End Sub
      

    End Class ```

    When you run the application and click each button, the corresponding shape will be drawn on the form.

Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question