求RichTextBox 详用。

Oo_o_oO 2012-03-31 02:56:03
请问,
RichTextBox 如何指定文本 改其大小,改其颜色,改其字体?
还有,
要取得 RichTextBox 光标的位置。

...全文
124 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Oo_o_oO 2012-03-31
  • 打赏
  • 举报
回复
不要让它的光标动来动去,

当然,
主要是因为编辑文本是不只是在后面 追加text。
有可能是中间插入text的
Oo_o_oO 2012-03-31
  • 打赏
  • 举报
回复
就是说,
我要改变他的颜色与字体,
不用 select (index,length) ; selectColor 这种方式。

而直接改richTextBox 指定部分的color,backColor,


有办法吗?
Oo_o_oO 2012-03-31
  • 打赏
  • 举报
回复

遇到,一个问题了。
比如:
"当我用 select 的方法可以做到改部分 color 与font"

但是:
"我不想把当标的位置变了。"
因为:
"我是在线输入,要样它在线变色."
大家都用才vs 编辑器吧,我想让 richTextBox 同它一样。
实现在线变色。//我的是用正则来比较它。


"求救"啊。


startstartsvip 2012-03-31
  • 打赏
  • 举报
回复
richTextBox1.Select(smc.Index, smc.Value.Length);
EnForGrass 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

引用 10 楼 的回复:
。。。 就只是行中没有东西挡着,才行.

你的只有第一个开始才行的,
[/Quote]
你自己改吧,思路给你了
Oo_o_oO 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
。。。 就只是行中没有东西挡着,才行.
[/Quote]
你的只有第一个开始才行的,
EnForGrass 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

[/Quote]
我试的只有电话号码的,如果一行包含其他字符,你要调整i的值,i的值就是这样的字符串长度
Oo_o_oO 2012-03-31
  • 打赏
  • 举报
回复
Oo_o_oO 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chinajiyong 的回复:]
你的不行啊,如下测式:
[/Quote]
string pattern = @"[\+\-\*\/={}]+";
bool ismatch = Regex.IsMatch(rtbSource.Text.Trim(), pattern);
if (ismatch)
{
int i = 0;
MatchCollection mc = Regex.Matches(rtbSource.Text.Trim(), pattern);
foreach (Match smc in mc)
{
rtbSource.Select(i, smc.Value.Length);
rtbSource.SelectionBackColor = Color.Blue;
rtbSource.SelectionFont = new Font(@"宋体", 12);
i = i + smc.Value.Length + 1;
}
}

结果:


可见,这样是不行的,没有选中 所有的符号啊。
EnForGrass 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

C# code

string pattern = @"[\d]{3}[-\/][\d]{7}|[\d]{4}[-\/][\d]{6}|[\d]{11}|[(][\d]{3}[)][\d]{7}";
bool ismatch = Regex.IsMatch(richTextBox1.Text.Trim(), pattern);
if (isma……
[/Quote]
改一下

string pattern = @"[\d]{3}[-\/][\d]{7}|[\d]{4}[-\/][\d]{6}|[\d]{11}|[(][\d]{3}[)][\d]{7}";
bool ismatch = Regex.IsMatch(richTextBox1.Text.Trim(), pattern);
if (ismatch)
{
int i = 0;
MatchCollection mc = Regex.Matches(richTextBox1.Text.Trim(), pattern);
foreach (Match smc in mc)
{
richTextBox1.Select(i, smc.Value.Length);
richTextBox1.SelectionBackColor = Color.Blue;
richTextBox1.SelectionFont = new Font(@"宋体", 20);
i = i + smc.Value.Length + 1;
}
}
EnForGrass 2012-03-31
  • 打赏
  • 举报
回复

string pattern = @"[\d]{3}[-\/][\d]{7}|[\d]{4}[-\/][\d]{6}|[\d]{11}|[(][\d]{3}[)][\d]{7}";
bool ismatch = Regex.IsMatch(richTextBox1.Text.Trim(), pattern);
if (ismatch)
{
int i = 0;
MatchCollection mc = Regex.Matches(richTextBox1.Text.Trim(), pattern);
foreach (Match smc in mc)
{


richTextBox1.Select(i, smc.Value.Length);
richTextBox1.SelectionBackColor = Color.Blue;
richTextBox1.SelectionFont = new Font(@"宋体", 10);
i = i + smc.Value.Length + 1;
// richTextBox1.SelectionStart = 0;
}

}
Oo_o_oO 2012-03-31
  • 打赏
  • 举报
回复
就是说 用正则表达式找到了richtextbox 中的 匹配项。
如何返回 这样的类型
//定义richTextBox 选中的字符串
public class selectText
{
public int index;
public int length;
public selectText();
public selectText(int index,int length) {
this.index = index;
this.length = length;
}
}

我有没有办法用正则返回下面这种类型的?
 List<selectText> 


然后这才 遍历 List<selectText> 类型的返回值,在richTextBox中一一添加 颜色。
???求助。
JICHAOWEI 2012-03-31
  • 打赏
  • 举报
回复
顺便问不一个 richtextbox里的字体怎样才能和原来文件的字体保持一致啊 加急令
Oo_o_oO 2012-03-31
  • 打赏
  • 举报
回复
Oo_o_oO 2012-03-31
  • 打赏
  • 举报
回复
如有一个正则表达式,
如下: (是电话号正则式)
string pattern = @"[\d]{3}[-\/][\d]{7}|[\d]{4}[-\/][\d]{6}|[\d]{11}|[(][\d]{3}[)][\d]{7}";


我要把 找到的电话号变成 蓝色,20号字,并把全部符合条件的 选中呢?
注: 文本中有很多 电话号码。
bdmh 2012-03-31
  • 打赏
  • 举报
回复


richTextBox1.SelectionBackColor
richTextBox1.SelectionFont
richTextBox1.SelectionStart

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧