Winforms control that displays text and supports individual coloring of lines

For a Winforms application, I am looking for a control that can display text and supports separate line colors (foreground and background colors for each line).

For example, I would like to have line 1 with a green background color and line 4 with a red color.

+4
source share
4 answers

A ListView control is probably the easiest thing for what you need (for now, it's just for display). Set the View property to List . Each item is a ListViewItem on which you can set foreground and background .

+7
source

In the past, I used a custom combo box for this type of control. I set DrawMode to OwnerDrawVariable in the constructor and override the OnDrawItem method. Thus, you can draw a background for each item in the drop-down list with any color or style that you want.

The code below draws any other element with a gray background, for example, the images shown in this message , but you can configure it as you want in the OnDrawItem method. this example also supports multiple columns and text packaging in combobox, but it just complicates things unnecessarily.

  Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs) MyBase.OnDrawItem(e) //Don't draw the custom area if in DesignMode. If Me.DesignMode Then Return //Don't draw the custom area if the index is -1. If e.Index = -1 Then Return e.DrawBackground() Dim boundsRect As Rectangle = e.Bounds //Shift everything just a bit to the right. Dim lastRight As Integer = 2 Dim brushForeColor As Color Dim bckColor As Color Dim lineColor As Color If Not (e.State And DrawItemState.Selected) = DrawItemState.Selected Then //Item is not selected, use _BackColorOdd and _BackColorEven. If e.Index Mod 2 = 0 Then bckColor = _BackColorEven Else bckColor = _BackColorOdd End If //Use black text color. brushForeColor = Color.Black //Use dark line color. lineColor = _BackColorSelected Else //Item is selected, use the 'Selected' background color. bckColor = _BackColorSelected //Item is selected, use white font color. brushForeColor = Color.White //Use white line color. lineColor = Color.White End If //Draw the background rectangle with the appropriate color. Using brushBackColor As New SolidBrush(bckColor) e.Graphics.FillRectangle(brushBackColor, e.Bounds) End Using Using linePen As New Pen(lineColor) Using brsh As New SolidBrush(brushForeColor) //This is the multi-column stuff. For colIndex As Integer = 0 To _ColumnNames.Count - 1 //Variant(Object) type used to support different data types in each column. Dim itm As Object itm = FilterItemOnProperty(Items(e.Index), _ColumnNames(colIndex)) boundsRect.X = lastRight boundsRect.Width = _ColumnWidths(colIndex) lastRight = boundsRect.Right e.Graphics.DrawString(itm, Me.Font, brsh, boundsRect) //Draw a divider line. If colIndex < _ColumnNames.Count - 1 Then e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, _ boundsRect.Right, boundsRect.Bottom) End If Next End Using End Using End Sub 

It may be a little redundant for what you want, but it will be a trick.

+2
source

If you do not need editing tools, you can use the ListView control (in details mode) or TreeControl to do this. Editing using these controls will be limited to one line at a time (for example, the rename tool in Windows Explorer).

If you need editing tools, you'll need the RichTextBox control that ChrisF offers.

+1
source

It looks like you might need the Rich Text Editor.

There are many third-party controls, including one from CodeProject

+1
source

All Articles