RichTextBox的文本怎样使关键字显示不同的颜色字体 应该是写javascript得到的。具体的你就只有请教高人了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 呵呵,我也正想找可是没找到,大概的意思就是在richtextbox的RTF属性里面写一些东西,但是怎么写我就不知道了,如果你找到答案记得告诉我, http://www.codeproject.com/cs/miscctrl/csexrichtextbox.asp using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace WinRichTextBox{ /// <summary> /// Summary description for Form1. /// </summary> public class frmMain : System.Windows.Forms.Form { private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Button btnUnload; private System.Windows.Forms.Button btnLoad; private System.Windows.Forms.RichTextBox rtb; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public frmMain() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form 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.panel1 = new System.Windows.Forms.Panel(); this.btnUnload = new System.Windows.Forms.Button(); this.btnLoad = new System.Windows.Forms.Button(); this.rtb = new System.Windows.Forms.RichTextBox(); this.panel1.SuspendLayout(); this.SuspendLayout(); // // panel1 // this.panel1.Controls.Add(this.btnUnload); this.panel1.Controls.Add(this.btnLoad); this.panel1.Dock = System.Windows.Forms.DockStyle.Top; this.panel1.Location = new System.Drawing.Point(0, 0); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(544, 40); this.panel1.TabIndex = 1; // // btnUnload // this.btnUnload.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnUnload.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.btnUnload.ForeColor = System.Drawing.SystemColors.ControlText; this.btnUnload.Location = new System.Drawing.Point(88, 8); this.btnUnload.Name = "btnUnload"; this.btnUnload.Size = new System.Drawing.Size(64, 23); this.btnUnload.TabIndex = 4; this.btnUnload.Text = "清 除"; this.btnUnload.Click += new System.EventHandler(this.btnUnload_Click); // // btnLoad // this.btnLoad.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnLoad.Location = new System.Drawing.Point(16, 8); this.btnLoad.Name = "btnLoad"; this.btnLoad.Size = new System.Drawing.Size(64, 23); this.btnLoad.TabIndex = 3; this.btnLoad.Text = "加 载"; this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click); // // rtb // this.rtb.BackColor = System.Drawing.SystemColors.GrayText; this.rtb.BorderStyle = System.Windows.Forms.BorderStyle.None; this.rtb.Dock = System.Windows.Forms.DockStyle.Fill; this.rtb.Location = new System.Drawing.Point(0, 40); this.rtb.Name = "rtb"; this.rtb.Size = new System.Drawing.Size(544, 389); this.rtb.TabIndex = 2; this.rtb.Text = ""; // // frmMain // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(544, 429); this.Controls.Add(this.rtb); this.Controls.Add(this.panel1); this.Name = "frmMain"; this.Text = "WinRichTextBoxFormatTest"; this.panel1.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new frmMain()); } private void btnLoad_Click(object sender, System.EventArgs e) { rtb.Clear(); //定义四个文本部分,便于分别应用不同的文本格式 string firstString="大山的骄傲\r\n"; string secondString="——eheartcool其人\r\n\r"; string thirdString="\t七十年代末的一个夜晚,没有星星,也没有月亮,中国湖北恩施的小山村正沉浸在睡梦中,几声婴儿的哭嘀和紧接的爆竹声冲破了夜的沉默,一个新的生命诞生了!\r\n"; string fourthString="未完待续……"; rtb.Text=firstString; rtb.Text+=secondString; rtb.Text+=thirdString; rtb.Text+=fourthString; //对标题应用格式 rtb.SelectionStart=0; rtb.SelectionLength=firstString.Length-1; rtb.SelectionFont=new System.Drawing.Font("黑体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); rtb.SelectionColor=System.Drawing.Color.Blue; rtb.SelectionAlignment=HorizontalAlignment.Center; //对副标题应用格式 rtb.SelectionStart=firstString.Length-1; rtb.SelectionLength=secondString.Length; rtb.SelectionFont=new System.Drawing.Font("黑体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); rtb.SelectionAlignment=HorizontalAlignment.Center; rtb.SelectionColor=System.Drawing.Color.Black; //对正文应用格式 rtb.SelectionStart=firstString.Length+secondString.Length-1; rtb.SelectionLength=thirdString.Length; rtb.SelectionFont=new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); rtb.SelectionColor=System.Drawing.Color.Black; rtb.SelectionAlignment=HorizontalAlignment.Left; //对结尾应用格式 rtb.SelectionStart=firstString.Length+secondString.Length+thirdString.Length-1; rtb.SelectionLength=fourthString.Length; rtb.SelectionFont=new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); rtb.SelectionColor=System.Drawing.Color.Black; rtb.SelectionAlignment=HorizontalAlignment.Right; } private void btnUnload_Click(object sender, System.EventArgs e) { rtb.Clear(); } }} 参照RTF格式用词法分析,然后加上RTF的TAG this.richTextBox1.SaveFile(save.FileName,RichTextBoxStreamType.RichText);可以进行保存颜色与字体。 提供一个笨办法,但是很奏效。比如说关键字有“int”“float”,在你将文件Load到RichTextBox中时,先设置选中文本的颜色,比如红色,再进行扫描字符串,每次“int”“float”出现时,将其置为选中状态,则该词就可以变成红色了。 checkbox列的全选 Outlook2003需要修改发送与接收的帐户与密码 请高手忙办把VB代码改成C# vs2003下串口通讯如何实现? StreamWriter写共享文件时Close()得非常慢,何解? 请教一个动态数据组组合算法问题 请教高手!!!! OPC编程 如何把字符串类型变成int型? C#高手请进。 字段全部是公有的不好之处在那里,能不能说得具体些? 头痛欲裂~~~~~
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WinRichTextBox
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button btnUnload;
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.RichTextBox rtb;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public frmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form 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.panel1 = new System.Windows.Forms.Panel();
this.btnUnload = new System.Windows.Forms.Button();
this.btnLoad = new System.Windows.Forms.Button();
this.rtb = new System.Windows.Forms.RichTextBox();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.btnUnload);
this.panel1.Controls.Add(this.btnLoad);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(544, 40);
this.panel1.TabIndex = 1;
//
// btnUnload
//
this.btnUnload.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnUnload.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnUnload.ForeColor = System.Drawing.SystemColors.ControlText;
this.btnUnload.Location = new System.Drawing.Point(88, 8);
this.btnUnload.Name = "btnUnload";
this.btnUnload.Size = new System.Drawing.Size(64, 23);
this.btnUnload.TabIndex = 4;
this.btnUnload.Text = "清 除";
this.btnUnload.Click += new System.EventHandler(this.btnUnload_Click);
//
// btnLoad
//
this.btnLoad.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnLoad.Location = new System.Drawing.Point(16, 8);
this.btnLoad.Name = "btnLoad";
this.btnLoad.Size = new System.Drawing.Size(64, 23);
this.btnLoad.TabIndex = 3;
this.btnLoad.Text = "加 载";
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
//
// rtb
//
this.rtb.BackColor = System.Drawing.SystemColors.GrayText;
this.rtb.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.rtb.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtb.Location = new System.Drawing.Point(0, 40);
this.rtb.Name = "rtb";
this.rtb.Size = new System.Drawing.Size(544, 389);
this.rtb.TabIndex = 2;
this.rtb.Text = "";
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(544, 429);
this.Controls.Add(this.rtb);
this.Controls.Add(this.panel1);
this.Name = "frmMain";
this.Text = "WinRichTextBoxFormatTest";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
} private void btnLoad_Click(object sender, System.EventArgs e)
{
rtb.Clear(); //定义四个文本部分,便于分别应用不同的文本格式
string firstString="大山的骄傲\r\n";
string secondString="——eheartcool其人\r\n\r";
string thirdString="\t七十年代末的一个夜晚,没有星星,也没有月亮,中国湖北恩施的小山村正沉浸在睡梦中,几声婴儿的哭嘀和紧接的爆竹声冲破了夜的沉默,一个新的生命诞生了!\r\n";
string fourthString="未完待续……"; rtb.Text=firstString;
rtb.Text+=secondString;
rtb.Text+=thirdString;
rtb.Text+=fourthString;
//对标题应用格式
rtb.SelectionStart=0;
rtb.SelectionLength=firstString.Length-1;
rtb.SelectionFont=new System.Drawing.Font("黑体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
rtb.SelectionColor=System.Drawing.Color.Blue;
rtb.SelectionAlignment=HorizontalAlignment.Center; //对副标题应用格式
rtb.SelectionStart=firstString.Length-1;
rtb.SelectionLength=secondString.Length; rtb.SelectionFont=new System.Drawing.Font("黑体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
rtb.SelectionAlignment=HorizontalAlignment.Center;
rtb.SelectionColor=System.Drawing.Color.Black;
//对正文应用格式
rtb.SelectionStart=firstString.Length+secondString.Length-1;
rtb.SelectionLength=thirdString.Length; rtb.SelectionFont=new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
rtb.SelectionColor=System.Drawing.Color.Black;
rtb.SelectionAlignment=HorizontalAlignment.Left;
//对结尾应用格式
rtb.SelectionStart=firstString.Length+secondString.Length+thirdString.Length-1;
rtb.SelectionLength=fourthString.Length; rtb.SelectionFont=new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
rtb.SelectionColor=System.Drawing.Color.Black;
rtb.SelectionAlignment=HorizontalAlignment.Right;
} private void btnUnload_Click(object sender, System.EventArgs e)
{
rtb.Clear();
}
}
}
用词法分析,然后加上RTF的TAG
可以进行保存颜色与字体。