Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual basic: picuter border on a button. How to?

Tags:

vb.net

Heey, I've been searching till my brain hurts for an answer to "how do i add a border to a button" The general idea is that i have a button with a portrait of a person, this button needs a border (green or red) to indicate whether the person is online or offline. My buttons is generated in runtime so any of this have to happen through code. Also to note that these buttons is generated using count of rows in a database (dtActive) This is how my buttons is generated:

Public Sub GenerateUsers()
    Dim ContactID As Integer = Nothing
    Dim FirstName As String = ""
    Dim LastName As String = ""
    Dim FullName As String = ""
    Dim INTwidth As Integer = Nothing
    Dim TextRendering As Size
    dim CordX As Integer = 20
    Dim CordY As Integer = 30
    Dim RunThrough As Integer= 0
    Dim tileLine As integer = 1
    For Each Row As DataRow In dtActive.Rows
        ContactID = Row(0)
        FirstName = Row(1)
        LastName = Row(3)
        FullName = FirstName & " " & LastName
        TextRendering = TextRenderer.MeasureText(FullName, Font)
        INTwidth = TextRendering.Width
        Dim NewLabel As New Label()
        With NewLabel
            .Parent = Me
            .Text = FullName.ToString
            .Location = New Point(CordX + 5, CordY + 5)
            .Name = "Label" & ContactID
            .AutoSize = False
            .Size = New System.Drawing.Size(INTwidth, 20)
            Me.GroupBox1.Controls.Add(NewLabel)
        End With
        Dim LogOnBtn As New Button
        With LogOnBtn
            .Parent = Me
            .Location = New Point(CordX, CordY)
            .Name = "Tile" & ContactID
            .Size = New System.Drawing.Size(140, 140)
            .Text = Nothing
            Me.GroupBox1.Controls.Add(LogOnBtn)
            AddHandler LogOnBtn.Click, AddressOf LogInOut
            Try
                .BackgroundImage = System.Drawing.Bitmap.FromFile(Row(17).ToString) 'System.Drawing.Bitmap.FromFile(Row(17).ToString)
                .BackColor = Color.Red
                .FlatStyle = FlatStyle.Flat
                .BackgroundImageLayout = ImageLayout.Zoom
            Catch ex As Exception
            End Try
        End With
        CordX += 145

        RunThrough += 1 'this ensures that once 3 buttons(tiles) have been made, next 3 will be on a new line
        If RunThrough = 3 Then
            CordX = 20
            CordY += 145
            RunThrough = 0
        End If
    Next
End Sub

This is my windows form: http://imageshack.us/photo/my-images/29/2bjm.jpg/

My idea is that its going to look like this: http://imageshack.us/photo/my-images/547/otuk.jpg/

like image 875
Simon Jensen Avatar asked Feb 26 '26 02:02

Simon Jensen


1 Answers

WinForms does not have a Border control like WPF does, so you are going to have to draw the border yourself using the GraphicsPath Class.

Here is a function that will draw a rounded rectangle, you would need to supply the button's x,y coordinates, height/width and a radius value for the rounded corners:

Public Sub DrawRoundRect(g As Graphics, p As Pen, x As Single, y As Single, width As Single, height As Single, radius As Single)
    Dim gp As New GraphicsPath()

    gp.AddLine(x + radius, y, x + width - (radius * 2), y) ' Line
    gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90) ' Corner
    gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2)) ' Line
    gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90) ' Corner
    gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height) ' Line
    gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90) ' Corner
    gp.AddLine(x, y + height - (radius * 2), x, y + radius) ' Line
    gp.AddArc(x, y, radius * 2, radius * 2, 180, 90) ' Corner

    gp.CloseFigure()

    g.DrawPath(p, gp)

    gp.Dispose()
End Sub
like image 134
Karl Anderson Avatar answered Feb 27 '26 20:02

Karl Anderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!