We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How can I set different background colors for the lines in the Edit Control?

Platform: WinForms |
Control: EditControl |
Published Date: April 30, 2011 |
Last Revised Date: July 10, 2014


This sample illustrates the EditControl's support for custom coloring the line background.

Please refer to the code snippets below and the sample attached to know how to set the back color for the entire line and the selected text.


private IBackgroundFormat RegisterFormat()


Color background = Color.Empty, foreground = Color.Empty;

if( radioButton1.Checked )

background = radioButton1.BackColor;

else if( radioButton2.Checked )

background = radioButton2.BackColor;

else if( radioButton3.Checked )

background = radioButton3.BackColor;

if( radioButton6.Checked )

foreground = radioButton6.BackColor;

else if( radioButton5.Checked )

foreground = radioButton5.BackColor;

else if( radioButton4.Checked )

foreground = radioButton4.BackColor;

bool bUseHatchFille = comboBox1.SelectedIndex > 0;

HatchStyle style = ( bUseHatchFille )? ( HatchStyle )Enum.Parse( typeof( HatchStyle ),

comboBox1.SelectedItem.ToString() ): HatchStyle.Min;

IBackgroundFormat format = editControl1.RegisterBackColorFormat( background, foreground, style, bUseHatchFille );

return format;


//code for setting line backcolor for the entire line

private void button1_Click(object sender, System.EventArgs e)


IDynamicFormat[] temp = editControl1.GetLineBackColorFormats( editControl1.CurrentLine );

IBackgroundFormat format = RegisterFormat();

editControl1.SetLineBackColor( editControl1.CurrentLine, true, format );


private void Form1_Load(object sender, System.EventArgs e)



comboBox1.Items.Add( "Solid" );

foreach( string name in Enum.GetNames( typeof( HatchStyle) ) )

comboBox1.Items.Add( name );

comboBox1.SelectedText = "Percent05";

comboBox1.SelectedIndex = 0;

editControl1.Text += "\n";

editControl1.CurrentLine = 1;


//code to set the backcolor for a selected text

private void button2_Click(object sender, System.EventArgs e)


IBackgroundFormat format = RegisterFormat();

editControl1.SetSelectionBackColor( format );



Private Function RegisterFormat() As IBackgroundFormat

Dim background As Color = Color.Empty

Dim foreground As Color = Color.Empty

If radioButton1.Checked Then

background = radioButton1.BackColor

ElseIf radioButton2.Checked Then

background = radioButton2.BackColor

ElseIf radioButton3.Checked Then

background = radioButton3.BackColor

End If

If radioButton6.Checked Then

foreground = radioButton6.BackColor

ElseIf radioButton5.Checked Then

foreground = radioButton5.BackColor

ElseIf radioButton4.Checked Then

foreground = radioButton4.BackColor

End If

Dim bUseHatchFille As Boolean = comboBox1.SelectedIndex > 0

Dim style As HatchStyle

If bUseHatchFille = True Then

style = CType([Enum].Parse(GetType(HatchStyle), comboBox1.SelectedItem.ToString()), HatchStyle)


style = HatchStyle.Min

End If

Dim format As IBackgroundFormat = editControl1.RegisterBackColorFormat(background, foreground, style, bUseHatchFille)

Return format

End Function 'RegisterFormat

' code to set backcolor for the entire line

Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click

Dim temp As IDynamicFormat() = editControl1.GetLineBackColorFormats(editControl1.CurrentLine)

Dim format As IBackgroundFormat = RegisterFormat()

editControl1.SetLineBackColor(editControl1.CurrentLine, True, format)

End Sub 'button1_Click

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load



Dim name As String

For Each name In [Enum].GetNames(GetType(HatchStyle))


Next name

comboBox1.SelectedText = "Percent05"

comboBox1.SelectedIndex = 0

editControl1.Text += vbLf

editControl1.CurrentLine = 1

End Sub 'Form1_Load

' Code to set backcolor for the selected text

Private Sub button2_Click(sender As Object, e As System.EventArgs) Handles button2.Click

Dim format As IBackgroundFormat = RegisterFormat()


End Sub

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.