关于richtextBox的text值的问题(winform)
时间:2011-12-08
来源:互联网
但是在richtextBox中输入的时候如果按回车进行换行,则text的值就有/r/n。
我的需求是:希望用户自动写满一行后,界面上看到自动换行的同时,text属性的值也有/r/n标记。
怎么做这个功能?
作者: ximomomoxinei6 发布时间: 2011-12-08
首先 根据/r/n进行分组。
然后每个组的成员如果满10个字符,加入/r/n标记
最后统一整合为一个string输出。
代码还没写完 不知道怎么写了 代码如下 大侠给补充下哈 谢谢了
C# code
string ss = txtRemark.Text.Replace("\r\n", " "); string[] sss = ss.Split(' '); foreach (string aa in sss) { //满10个就给加个\r\n } //最后 整合成一个string 输出....大侠们帮忙写下啊
或者有更简便的方法实现也可以
作者: ximomomoxinei6 发布时间: 2011-12-08
string ss = txtRemark.Text.Replace("\r\n", " "); string[] sss = ss.Split(' '); string str=""; for (int i=0;i<sss.length;i++) { if(满10个) { str+=sss[i]+"\r\n"; } //满10个就给加个\r\n }
作者: zyloveyrf 发布时间: 2011-12-08
我暂时有个思路 方法有点笨 假设10个字符的宽度是一行
首先 根据/r/n进行分组。
然后每个组的成员如果满10个字符,加入/r/n标记
最后统一整合为一个string输出。
代码还没写完 不知道怎么写了 代码如下 大侠给补充下哈 谢谢了
C# code
string ss = txtRemark.Text.Replace("\r\n", " ");
……
楼主的是richtextBox,richtextBox每个字符都可能是不同的字体和大小,即使是同样的字体和大小,不同的字符占用的宽度也可能不同。
我也没想到什么好办法,在1楼的基础上,用MeasureString来测量每个字的大小,然后当下一个字会超出范围后就插入\r\n
作者: bunliney 发布时间: 2011-12-08
引用 1 楼 ximomomoxinei6 的回复:
我暂时有个思路 方法有点笨 假设10个字符的宽度是一行
首先 根据/r/n进行分组。
然后每个组的成员如果满10个字符,加入/r/n标记
最后统一整合为一个string输出。
代码还没写完 不知道怎么写了 代码如下 大侠给补充下哈 谢谢了
C# code
string ss = txtRemark.Text.Rep……
先不考虑大小 暂时就认为都那么大
一楼的代码木写全啊 我着急用 谁能补充一下呀
作者: ximomomoxinei6 发布时间: 2011-12-08
C# code
foreach (string aa in sss) { int jj = 0; foreach (char a in aa) { jj++; if (jj == 10) { jj = 0; //下面脑袋又短路了 } } //满10个就给加个\r\n }
作者: ximomomoxinei6 发布时间: 2011-12-08
C# code
string ss = txtRemark.Text.Replace("\r\n", " "); string[] sss = ss.Split(' '); string wwww = ""; string all = ""; foreach (string aa in sss) { int jj = 0; foreach (char a in aa) { jj++; wwww += a; if (jj == 10) { jj = 0; wwww += "\r\n"; //下面脑袋又短路了 } } all += wwww + "\r\n"; wwww = ""; //满10个就给加个\r\n }
作者: ximomomoxinei6 发布时间: 2011-12-08
拼音和数字的可以 汉字的话这么写就不成了 谁能帮下啊 汉字和拼音都存在的情况下能准确测量出长度 怎么改呢???
作者: ximomomoxinei5 发布时间: 2011-12-08
汉字是2 字母是1的那个属性是啥来着
作者: ximomomoxinei5 发布时间: 2011-12-08
作者: ximomomoxinei5 发布时间: 2011-12-08
C# code
private void richTextBox1_TextChanged(object sender, EventArgs e) { int index = richTextBox1.SelectionStart; string[] lines = Regex.Split(richTextBox1.Text, @"\n"); int width = richTextBox1.Size.Width - 15; Graphics gr = Graphics.FromImage(new Bitmap(1, 1)); StringBuilder builder = new StringBuilder(); foreach (string el in lines) { if (el == string.Empty) continue; int linewidth=(int)gr.MeasureString(el, richTextBox1.Font).Width; if (linewidth > width) { char[] item = el.ToCharArray(); StringBuilder tempstr = new StringBuilder(); for (int i = 0; i < item.Length; i++) { string tc2 = tempstr.ToString() + item[i]; int charwidth = (int)gr.MeasureString(tc2, richTextBox1.Font).Width; if (charwidth > width) { builder.Append(tempstr + "\r\n"); tempstr = new StringBuilder(); i--; } else tempstr.Append(item[i]); } } else builder.Append(el+"\r\n"); } richTextBox1.Text = builder.ToString(); richTextBox1.SelectionStart = index; gr.Dispose(); }
作者: bunliney 发布时间: 2011-12-08
C# code
private void richTextBox1_TextChanged(object sender, EventArgs e) { int index = richTextBox1.SelectionStart; string[] lines = Regex.Split(richTextBox1.Text, @"\n"); int width = richTextBox1.Size.Width - 15; Graphics gr = Graphics.FromImage(new Bitmap(1, 1)); StringBuilder builder = new StringBuilder(); foreach (string el in lines) { if (el == string.Empty) continue; int linewidth=(int)gr.MeasureString(el, richTextBox1.Font).Width; if (linewidth > width) { char[] item = el.ToCharArray(); StringBuilder tempstr = new StringBuilder(); for (int i = 0; i < item.Length; i++) { string tc2 = tempstr.ToString() + item[i]; int charwidth = (int)gr.MeasureString(tc2, richTextBox1.Font).Width; if (charwidth > width) { builder.Append(tempstr + "\r\n"); tempstr = new StringBuilder(); tempstr.Append(item[i]); index++; } else tempstr.Append(item[i]); } builder.Append(tempstr.ToString() + "\r\n"); } else builder.Append(el+"\r\n"); } richTextBox1.Text = builder.ToString(); richTextBox1.SelectionStart = index; gr.Dispose(); }
作者: bunliney 发布时间: 2011-12-08
修改了下,忘了把不满一行的字符加进去了
C# code
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
int index = richTextBox1.SelectionStart;
string[] lin……
厉害呀 回家我研究研究你这算法 我的那个就少了标点符号什么的 你的这个好全啊 而且比起我的那个更准确的多 多谢了 回家了先
作者: ximomomoxinei5 发布时间: 2011-12-08
C# code
private int GetStrLen(String ss) { Char[] cc = ss.ToCharArray(); int intLen = ss.Length; int i; for (i = 0; i < cc.Length; i++) { if (Convert.ToInt32(cc[i]) > 255) { intLen++; } } return intLen; } private string StringConform(string oldStr) { string ss = oldStr.Replace("\r\n", " "); string[] sss = ss.Split(' '); string wwww = string.Empty; string all = string.Empty; foreach (string aa in sss) { foreach (char a in aa) { //jj++; wwww += a; if (GetStrLen(wwww) == 35 || GetStrLen(wwww) == 36) { //jj = 0; wwww += "\r\n"; } } all += wwww + "\r\n"; wwww = ""; } return all; }
作者: ximomomoxinei5 发布时间: 2011-12-08
比如我的是39字符宽度的richtextBox 输入“aaaaaaaaa啊啊啊啊啊啊啊aaaaaaaaaaaaaaaa”的时候一切正常 但是一旦多输入个a 就会变成下面这样
HTML code
aaaaaaaaa啊啊啊啊啊啊啊
aaaaaaaaaaaaaaaaa
richtextBox的右面空出来一大块,而实际输出的结果还是对的 只是richtextBox显示的诡异些!!
作者: ximomomoxinei5 发布时间: 2011-12-08
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28