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.
Unfortunately, activation email could not send to your email. Please try again.

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

 

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.

C#

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.Clear();

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 );

}

VB

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)

Else

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

comboBox1.Items.Clear()

comboBox1.Items.Add("Solid")

Dim name As String

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

comboBox1.Items.Add(name)

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()

editControl1.SetSelectionBackColor(format)

End Sub

Article ID: Published Date: Last Revised Date: Platform: Control:
319 04/30/2011 07/10/2014 Windows Forms EditControl
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

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.