/********************************************************************************************** * 创建时间: 2005-1-19 16:32:06 * 作者: Jack * * 介绍: 一个利用ICSharpCode.TextEditor.TextEditorControl控件 * 实现代码语法高亮显示的示例程序,需要引用ICSharpCode.TextEditor.dll文件 * 该文件可以在#develop的库中找到 **********************************************************************************************/
#region UsingBlock
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using ICSharpCode.TextEditor; using ICSharpCode.TextEditor.Gui.CompletionWindow; using ICSharpCode.TextEditor.Document; using ICSharpCode.TextEditor.Actions;
#endregion
namespace HighLightingCodeFile { /// <summary> /// ICSharpCode.TextEditor. /// TextEditorControl的示例程序 /// </summary> public class TestCSharpTextBox : System.Windows.Forms.Form { #region 私有属性
private ICSharpCode.TextEditor.TextEditorControl textEditorControl; private System.Windows.Forms.MainMenu mainMenu; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem OpenMenu; private System.Windows.Forms.MenuItem Save; private System.Windows.Forms.SaveFileDialog saveFileDialog; private System.Windows.Forms.OpenFileDialog openFileDialog; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem menuItem4; private System.Windows.Forms.MenuItem menuItem5; private System.Windows.Forms.MenuItem menuItem6; private System.Windows.Forms.MenuItem menuItem7; private System.Windows.Forms.MenuItem menuItem8; #endregion #region 属性 #endregion #region 构造函数 public TestCSharpTextBox() { // // Windows 窗体设计器支持所必需的 // InitializeComponent();
textEditorControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C#"); textEditorControl.Encoding = System.Text.Encoding.Default; } #endregion #region 私有方法 #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TestCSharpTextBox)); this.textEditorControl = new ICSharpCode.TextEditor.TextEditorControl(); this.mainMenu = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.OpenMenu = new System.Windows.Forms.MenuItem(); this.Save = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.menuItem4 = new System.Windows.Forms.MenuItem(); this.menuItem5 = new System.Windows.Forms.MenuItem(); this.menuItem6 = new System.Windows.Forms.MenuItem(); this.menuItem7 = new System.Windows.Forms.MenuItem(); this.menuItem8 = new System.Windows.Forms.MenuItem(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.SuspendLayout(); // // textEditorControl // this.textEditorControl.AutoScroll = true; this.textEditorControl.Dock = System.Windows.Forms.DockStyle.Fill; this.textEditorControl.Encoding = ((System.Text.Encoding)(resources.GetObject("textEditorControl.Encoding"))); this.textEditorControl.Location = new System.Drawing.Point(0, 0); this.textEditorControl.Name = "textEditorControl"; this.textEditorControl.ShowEOLMarkers = true; this.textEditorControl.ShowInvalidLines = false; this.textEditorControl.ShowSpaces = true; this.textEditorControl.ShowTabs = true; this.textEditorControl.ShowVRuler = true; this.textEditorControl.Size = new System.Drawing.Size(472, 454); this.textEditorControl.TabIndex = 0; // // mainMenu // this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1, this.menuItem2}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.OpenMenu, this.Save}); this.menuItem1.Text = "File(&F)"; // // OpenMenu // this.OpenMenu.Index = 0; this.OpenMenu.Text = "&Open"; this.OpenMenu.Click += new System.EventHandler(this.OpenMenu_Click); // // Save // this.Save.Index = 1; this.Save.Text = "&Save"; this.Save.Click += new System.EventHandler(this.Save_Click); // // menuItem2 // this.menuItem2.Index = 1; this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem3, this.menuItem4, this.menuItem5, this.menuItem6, this.menuItem7, this.menuItem8}); this.menuItem2.Text = "CodeType(&T)"; // // menuItem3 // this.menuItem3.Index = 0; this.menuItem3.Text = "&C#"; this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click); // // menuItem4 // this.menuItem4.Index = 1; this.menuItem4.Text = "C++.&NET"; this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click); // // menuItem5 // this.menuItem5.Index = 2; this.menuItem5.Text = "&Xml"; this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click); // // menuItem6 // this.menuItem6.Index = 3; this.menuItem6.Text = "&Html"; this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click); // // menuItem7 // this.menuItem7.Index = 4; this.menuItem7.Text = "&Java"; this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click); // // menuItem8 // this.menuItem8.Index = 5; this.menuItem8.Text = "&VB.NET"; this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click); // // TestCSharpTextBox // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(472, 454); this.Controls.Add(this.textEditorControl); this.Menu = this.mainMenu; this.Name = "TestCSharpTextBox"; this.Text = "TestCSharpTextBox"; this.ResumeLayout(false);
} #endregion
#region 事件处理
private void OpenMenu_Click(object sender, System.EventArgs e) { OpenFileDialog oDia = new OpenFileDialog(); DialogResult result = oDia.ShowDialog(); if (result == DialogResult.OK) { textEditorControl.LoadFile(oDia.FileName); } }
private void menuItem3_Click(object sender, System.EventArgs e) { textEditorControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C#"); }
private void menuItem4_Click(object sender, System.EventArgs e) { textEditorControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C++.NET"); }
private void menuItem5_Click(object sender, System.EventArgs e) { textEditorControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("XML"); }
private void menuItem6_Click(object sender, System.EventArgs e) { textEditorControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("HTML"); }
private void menuItem7_Click(object sender, System.EventArgs e) { textEditorControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("Java"); }
private void menuItem8_Click(object sender, System.EventArgs e) { textEditorControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("VBNET"); }
private void Save_Click(object sender, System.EventArgs e) { SaveFileDialog sDia = new SaveFileDialog(); DialogResult result = sDia.ShowDialog(); if (result == DialogResult.OK) { textEditorControl.SaveFile(sDia.FileName); } } #endregion
#endregion
#region 入口点
[STAThread] static void Main() { Application.Run(new TestCSharpTextBox()); }
#endregion #region 公有方法 #endregion } } 
|