无法计算 RichTextBox 中的行号
这是我的代码,
private void Allocation_Matrix_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
int Total_Row_Check;
Total_Row_Check = getRow();
Textbox1.Text = Convert.ToString(Total_Row_Check);
if (Total_Row_Check >= Convert.ToInt32(Total_Processes.Text))
{
MessageBox.Show("rows cannot exceed from total number of processess");
}
}
}
public int getRow()
{
int Row = 0;
string[] arLines;
int i;
arLines = Allocation_Matrix.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
for (i = 0; i < arLines.Length; i++)
{
Row++;
}
return Row;
}
当我从键盘在 RichTextBox 中按 Enter 时,我想更新 TextBox1...但 Textbox 只显示 FirstRow 并显示 One(1)
here is my code
private void Allocation_Matrix_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
int Total_Row_Check;
Total_Row_Check = getRow();
Textbox1.Text = Convert.ToString(Total_Row_Check);
if (Total_Row_Check >= Convert.ToInt32(Total_Processes.Text))
{
MessageBox.Show("rows cannot exceed from total number of processess");
}
}
}
public int getRow()
{
int Row = 0;
string[] arLines;
int i;
arLines = Allocation_Matrix.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
for (i = 0; i < arLines.Length; i++)
{
Row++;
}
return Row;
}
i want to update TextBox1 as i hit ENTER in richtextbox from keyboard...but Textbox keeps show only FirstRow and shows One(1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如何使用
RichTextBox.GetLineFromCharIndex
方法?它返回字符索引所在的从零开始的行号。
How about using
RichTextBox.GetLineFromCharIndex
Method? It returns, the zero-based line number in which the character index is located.Allocation_Matrix 是您的 RichTextBox 吗?
请注意,RichTextBox.Text 返回带有换行符 ("\n") 的文本作为换行符,而大多数其他 Windows 控件都使用回车符、换行符 ("\r\n")。
因此,如果您对 Text 属性返回的字符串调用 Split,它将不包含任何 Evironment.NewLine。
Is Allocation_Matrix your RichTextBox?
Note that RichTextBox.Text returns the text with line feed ("\n") for the line breaks, where most all other Windows controls use a carriage return, line feed ("\r\n").
So if you are calling Split on the string returned by the Text property, it won't contain any Evironment.NewLine.
尝试Allocation_Matrix.Text.Lines.Count()方法
try Allocation_Matrix.Text.Lines.Count() method
试试这个
try this