RichTextBox
MyRichTextBox自定义控件继承原来的控件
然后说说几个实用方法
public partial class MyRichTextBox : RichTextBox //打开链接 this.LinkClicked += MyRichTextBox_LinkClicked; private void MyRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e) { System.Diagnostics.Process.Start(e.LinkText); } // 保存 if (System.IO.Path.GetExtension(strFilename).ToLower().Equals(".rtf")) this.SaveFile(strFilename); else this.SaveFile(strFilename, RichTextBoxStreamType.PlainText); //查找 private List<int> searchResult = new List<int>(); public int searchString(string searchString, System.Drawing.Color color) { searchResult.Clear(); this.Select(0, this.Text.Length); this.SelectionColor = System.Drawing.Color.Black; int index; //第一次搜索 //index = this.Text.IndexOf(searchString); index = this.Find(searchString, RichTextBoxFinds.MatchCase); //这个标志是滚动到第一个搜索项 int flag = index; while (index != -1) { this.Select(index, searchString.Length); this.SelectionColor = color; index = this.Find(searchString, index + 1, RichTextBoxFinds.MatchCase); //index = this.Text.IndexOf(searchString, index + 1); if (index != -1) { searchResult.Add(index); } } if (flag != -1) { this.Select(flag, searchString.Length); } //移动到插入符号 this.ScrollToCaret(); if (flag == -1) { return 0; } else { return searchResult.Count + 1; } } /// <summary> /// 滚动 /// </summary> public void ScrollPosition() { if (searchResult.Count > 0) { while (this.SelectionStart > searchResult[0]) { //this.Select(searchResult[0], 1); //this.ScrollToCaret(); searchResult.RemoveAt(0); } this.Select(searchResult[0], 1); this.ScrollToCaret(); searchResult.RemoveAt(0); } else { MessageBox.Show("已经是最后一个"); } }
本来想做代码折叠 但是百度了一下没有能用的代码
代码折叠的思路都是继承control 然后自己在paint方法中画字符串,
都是调用的gdi的drawingstring,这样的话很多方法还得自己写,而且原来项目用richitextbox的地方改动很大,
希望高手指点,有没有richtextbox能实现代码折叠的。