vb.net

How to draw a right triangle in vb.net WinForm?


I have x ImageButtons in my WinForm each ImageButton has the same height and width and works like a radio-button group, the user can press only one ImageButtton at the time.

I would add a triangle at the center of each ImageButton when it's clicked like this:

enter image description here

I was trying to draw a Triangle by using Graphics but i can't get how to make thar triangle pointing at the Right and anchored to the center of the image button...

Here is the code i've tried to draw the triangle

 With e.Graphics
        Using gp As New GraphicsPath
            gp.AddLine(150, 20, 180, 240)
            gp.AddLine(180, 240, 20, 240)
            gp.CloseFigure()
            .FillPath(Brushes.Red, gp)
            .DrawPath(Pens.Red, gp)
        End Using
 End With

Solution

  • In your shoes I’ll pay attention about comments above as if you do that everything will be easier in a long term prospective. Saying that in a short term this code gives an idea to start with:

    Private Sub Button2_Paint(sender As Object, e As PaintEventArgs) Handles Button2.Paint
    
        Dim w As Single = 30
        Dim h As Single = 50
        Dim centerY As Single = CSng(Button2.Height / 2)
    
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
        e.Graphics.CompositingQuality = CompositingQuality.HighQuality
    
        Using gp As New GraphicsPath()
            gp.AddLine(0, centerY - h / 2, w, centerY)
            gp.AddLine(w, centerY, 0, centerY + h / 2)
            e.Graphics.DrawPath(Pens.Red, gp)
            e.Graphics.FillPath(Brushes.Red, gp)
        End Using
    
    End Sub
    

    And let me add something else which is: your triangle (as your image shows) is a Left triangle not a Right triangle.

    If you need to do it on click and reset all the other parts to their initial state, start from here:

    Private Event UpdateBtns(caller As Button)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RaiseEvent UpdateBtns(Button1)
    End Sub
    
    Private Sub Button2_Paint(sender As Object, e As PaintEventArgs) Handles Button2.Paint
    
        AddHandler UpdateBtns, Sub(caller As Button)
                                   If (caller.Name <> CType(sender, Button).Name) Then CType(sender, Button).Refresh()
                               End Sub
    
        If ActiveControl Is CType(sender, Button) Then
            Dim w As Single = 30
            Dim h As Single = 50
            Dim centerY As Single = CSng(Button2.Height / 2)
    
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality
    
            Using gp As New GraphicsPath()
                gp.AddLine(0, centerY - h / 2, w, centerY)
                gp.AddLine(w, centerY, 0, centerY + h / 2)
                e.Graphics.DrawPath(Pens.Red, gp)
                e.Graphics.FillPath(Brushes.Red, gp)
            End Using
        End If
    
    End Sub