Всем привет. Я создал пользовательский элемент управления NumberedTextBoxUC.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace NumberedTextBox
{
public partial class NumberedTextBoxUC : UserControl
{
public int SelectionLength;
public NumberedTextBoxUC()
{
InitializeComponent();
numberLabel.Font = new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size + 1.019f);
}
private void updateNumberLabel()
{
//we get index of first visible char and number of first visible line
Point pos = new Point(0, 0);
int firstIndex = richTextBox1.GetCharIndexFromPosition(pos);
int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex);
//now we get index of last visible char and number of last visible line
pos.X = ClientRectangle.Width;
pos.Y = ClientRectangle.Height;
int lastIndex = richTextBox1.GetCharIndexFromPosition(pos);
int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex);
//this is point position of last visible char, we'll use its Y value for calculating numberLabel size
pos = richTextBox1.GetPositionFromCharIndex(lastIndex);
//finally, renumber label
numberLabel.Text = "";
for (int i = firstLine; i <= lastLine + 1; i++)
{
numberLabel.Text += i + 1 + "\n";
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
updateNumberLabel();
}
private void richTextBox1_VScroll(object sender, EventArgs e)
{
//move location of numberLabel for amount of pixels caused by scrollbar
int d = richTextBox1.GetPositionFromCharIndex(0).Y % (richTextBox1.Font.Height + 1);
numberLabel.Location = new Point(0, d);
updateNumberLabel();
}
private void richTextBox1_Resize(object sender, EventArgs e)
{
richTextBox1_VScroll(null, null);
}
private void richTextBox1_FontChanged(object sender, EventArgs e)
{
updateNumberLabel();
richTextBox1_VScroll(null, null);
}
public int SelectionStart { get; set; }
public Color SelectionColor { get; set; }
}
}
И NumberedTextBoxUC.Designer.cs
namespace NumberedTextBox
{
partial class NumberedTextBoxUC
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.numberLabel = new System.Windows.Forms.Label();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.SuspendLayout();
//
// numberLabel
//
this.numberLabel.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
this.numberLabel.Location = new System.Drawing.Point(3, 0);
this.numberLabel.Name = "numberLabel";
this.numberLabel.Size = new System.Drawing.Size(37, 267);
this.numberLabel.TabIndex = 1;
this.numberLabel.Text = "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16";
this.numberLabel.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
this.splitContainer1.IsSplitterFixed = true;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.numberLabel);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.richTextBox1);
this.splitContainer1.Size = new System.Drawing.Size(403, 267);
this.splitContainer1.SplitterDistance = 41;
this.splitContainer1.SplitterWidth = 1;
this.splitContainer1.TabIndex = 2;
this.splitContainer1.Text = "splitContainer1";
//
// richTextBox1
//
this.richTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(361, 267);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
this.richTextBox1.WordWrap = false;
this.richTextBox1.VScroll += new System.EventHandler(this.richTextBox1_VScroll);
this.richTextBox1.FontChanged += new System.EventHandler(this.richTextBox1_FontChanged);
this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged);
this.richTextBox1.Resize += new System.EventHandler(this.richTextBox1_Resize);
//
// NumberedTextBoxUC
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.splitContainer1);
this.Name = "NumberedTextBoxUC";
this.Size = new System.Drawing.Size(403, 267);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Label numberLabel;
private System.Windows.Forms.SplitContainer splitContainer1;
}
}
Это типа textbox, только с нумерацией строк. Я хочу сделать так, что если я напишу в него слово end, оно было красным, а все другие слова станд.