无法计算 RichTextBox 中的行号

发布于 2024-10-05 14:15:04 字数 927 浏览 1 评论 0原文

这是我的代码,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒儿 2024-10-12 14:15:04

如何使用 RichTextBox.GetLineFromCharIndex方法?它返回字符索引所在的从零开始的行号。

How about using RichTextBox.GetLineFromCharIndex Method? It returns, the zero-based line number in which the character index is located.

逐鹿 2024-10-12 14:15:04

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.

静水深流 2024-10-12 14:15:04

尝试Allocation_Matrix.Text.Lines.Count()方法

try Allocation_Matrix.Text.Lines.Count() method

濫情▎り 2024-10-12 14:15:04

试试这个

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
var lineCount = richTextBox.Lines.Count();
numberLabel.Text = lineCount.ToString();
}

try this

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
var lineCount = richTextBox.Lines.Count();
numberLabel.Text = lineCount.ToString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文