Category / Section
How to set different background colors for the lines in the WinForms SyntaxEditor (EditControl)?
2 mins read
Background color for the lines
Please refer to the code snippets below for 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, 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), CType(System.Enum.Parse(GetType(HatchStyle), comboBox1.SelectedItem.ToString()), HatchStyle), HatchStyle.Min) Dim format As IBackgroundFormat = editControl1.RegisterBackColorFormat(background, foreground, style, bUseHatchFille) Return format End Function 'code for setting line backcolor for the entire line Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim temp() As IDynamicFormat = editControl1.GetLineBackColorFormats(editControl1.CurrentLine) Dim format As IBackgroundFormat = RegisterFormat() editControl1.SetLineBackColor(editControl1.CurrentLine, True, format) End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) comboBox1.Items.Clear() comboBox1.Items.Add("Solid") For Each name As String In System.Enum.GetNames(GetType(HatchStyle)) comboBox1.Items.Add(name) comboBox1.SelectedText = "Percent05" comboBox1.SelectedIndex = 0 editControl1.Text += Constants.vbLf editControl1.CurrentLine = 1 Next name End Sub 'code to set the backcolor for a selected text Private Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim format As IBackgroundFormat = RegisterFormat() editControl1.SetSelectionBackColor(format) End Sub
Reference link: https://help.syncfusion.com/windowsforms/syntaxeditor/text-visualization#customize-line-number-appearance