Is there an easy way to create a circle with initials in it for a custom control?

Anthony White 21 Reputation points
2022-10-26T16:10:54.08+00:00

I would like to create a custom control that looks like an item in an Outlook Contacts panel - A circle with initials in it, along with text for the name. I am using VB.NET.

Is there an easy way to do this?

Outlook | Windows | Classic Outlook for Windows | For business
Developer technologies | VB
{count} votes

Accepted answer
  1. LesHay 7,141 Reputation points
    2022-10-27T01:06:59.313+00:00

    Hi
    Here is one possibility. This is a simplistic version with only two control properties explicitly dealt with - border or no border and text (long name). Minimal testing as I was out for a few hours.

    254522-111.png

    Form1 code

    Option Strict On  
    Option Explicit On  
    Public Class Form1  
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        MyControl1.BorderStyle = BorderStyle.None  
      End Sub  
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        MyControl1.Txt = Trim(TextBox1.Text)  
      End Sub  
      Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged  
        With MyControl1  
          Dim bs As BorderStyle = .BorderStyle  
          Select Case bs  
            Case BorderStyle.FixedSingle  
              .BorderStyle = BorderStyle.None  
            Case Else  
              .BorderStyle = BorderStyle.FixedSingle  
          End Select  
        End With  
      End Sub  
    End Class  
    

    MyControl Class code

    Imports System.ComponentModel  
    Public Class MyControl  
      Inherits Panel  
      Private longnamefont As New Font(Font.FontFamily, 20)  
      Private initialfont As New Font(Font.FontFamily, 23, FontStyle.Bold)  
      
      Private longnamecolor As New SolidBrush(Color.Black)  
      Private circlefillcolor As New SolidBrush(Color.Green)  
      Private initialcolor As New SolidBrush(Color.White)  
      
      Private Shared ReadOnly penWidth As Single = 3.0F  
      Public Sub New()  
      
      End Sub  
      Protected Overrides Sub OnPaint(e As PaintEventArgs)  
        MyBase.OnPaint(e)  
        Dim sz As Integer = ClientRectangle.Height  
      
        e.Graphics.FillEllipse(circlefillcolor, 5, 5, sz - 10, sz - 10)  
      
        Dim w1 As Size = TextRenderer.MeasureText(Txt, longnamefont)  
        e.Graphics.DrawString(Txt, longnamefont, longnamecolor, New PointF(sz, sz \ 2 - w1.Height \ 2))  
      
        Dim init() As String = Split(Txt.ToUpper, " "c)  
        Dim ct As String = Nothing  
        For Each s As String In init  
          ct &= s(0)  
        Next  
        Dim w2 As Size = TextRenderer.MeasureText(ct, initialfont)  
        e.Graphics.DrawString(ct, initialfont, initialcolor, New PointF(5 + sz \ 2 - w2.Width \ 2, sz \ 2 - w2.Height \ 2))  
      End Sub  
      Private _text As String = "My Control 1"  
      <Browsable(True)>  
      Public Property Txt As String  
        Get  
          Return _text  
        End Get  
        Set(Value As String)  
          _text = Value  
          Invalidate()  
        End Set  
      End Property  
    End Class  
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.