0

I am developing one vb application. In that I have one list box. I want to add different types of Items. Like Different Colored and differently aligned text(Like one item item is right aligned and one more is left aligned). Can you please tell me how can i do the same.

Thanks.

3
  • to do that you will need to inherit the listbox class and overload some things.. IN essence you would have to build you own custom class based on listbox.. or go buy/find a .net dll that does this. The include listbox is very basic and only some colours for global elements can be changed. not very dynamic unpleasantly Commented Aug 18, 2011 at 9:42
  • Actually I am doing a chat application. When user types something and press enter text as to display inside list box(Or any other control) with users image and other info like time and all. Is there any way I can do this. Is there any control is there instead of using ListBox? Commented Aug 18, 2011 at 9:47
  • Gridview? you can add columns and format them the way you want. Update the application dynamically. better than listbox i suppose Commented Aug 18, 2011 at 9:52

2 Answers 2

1

This is how I did it in one of my projects (the original code is in c#):

Public Class ColoringStepCommandListBox
    Inherits CommandListBox

    Const ItemHeight As Integer = 20

    Public Sub New()
        listBox.ItemHeight = ItemHeight
        listBox.DrawMode = DrawMode.OwnerDrawFixed
    End Sub

    Protected Overrides Sub OnDrawItem(sender As Object, e As DrawItemEventArgs)
        Const  textFormatFlags__1 As TextFormatFlags = TextFormatFlags.EndEllipsis Or TextFormatFlags.PreserveGraphicsClipping Or TextFormatFlags.VerticalCenter
        Const  colorRectangleWidth As Integer = 100, textLeft As Integer = 110

        If e.Index >= 0 Then
            'Cast the listbox item to your custom type (ColoringStep in my example).
            Dim coloringStep = TryCast(listBox.Items(e.Index), ColoringStep)

            e.DrawBackground()

            'Do custom coloring and rendering, draw icons etc.
            Dim colorRect = New Rectangle(2, e.Bounds.Top + 2, colorRectangleWidth, ItemHeight - 5)
            Dim innerRect = New Rectangle(colorRect.Left + 1, colorRect.Top + 1, colorRect.Width - 1, colorRect.Height - 1)
            e.Graphics.DrawRectangle(Pens.Black, colorRect)
            DrawingHelper.DrawGradient(coloringStep, e.Graphics, innerRect, LinearGradientMode.Horizontal)

            'Draw the text (this does not happen automatically any more with owner draw modes).
            Dim textRect = New Rectangle(textLeft, e.Bounds.Top, e.Bounds.Width - textLeft, e.Bounds.Height)
            TextRenderer.DrawText(e.Graphics, coloringStep.ToString(), e.Font, textRect, e.ForeColor, textFormatFlags__1)

            e.DrawFocusRectangle()
        End If
    End Sub
End Class
Sign up to request clarification or add additional context in comments.

Comments

0

I got simple solution for this. Below is the code

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    e.DrawBackground()
    Dim textFont As New Font(e.Font.FontFamily, e.Font.Size * 4)
    e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), textFont, New SolidBrush(Color.BlueViolet), RectangleF.op_Implicit(e.Bounds))
    e.DrawFocusRectangle()
End Sub

Private Sub listBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ListBox1.MeasureItem
    e.ItemHeight = e.ItemHeight * 4
End Sub

You can add extra code inside ListBox1_DrawItem method to customize Items

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.