English 中文(简体)
如何显示RichTextBox C#的一条线。
原标题:How to show number of a line in a RichTextBox C#

我正在做一个简单的文本和文字编辑,其中着重阐述法典。 为此,我使用了RichTextBox。 但我不知道如何显示左边的界线,如VS或NOpad++。 是否有解决办法?

最佳回答

我试图从其他地方提到的代码项目条款中重新使用该守则,但我所研究的每一种选择似乎过于费力。

因此,我建造了另一个显示线数的RichTextBoxEx。

线路编号可以翻开。 它迅速。 预示着。 你们可以选择数字的颜色、梯度的背景颜色、边界厚度、斜体,决定是否使用铅零。 你们可以选择“显示”的线号,或者根据RTB的硬新路线。

实例:

“entergraph

“enterography

“entergraph

它有局限性:只出现在左边。 如果没有太多的努力,你就可以改变这种状况。

该法典是作为C#项目设计的。 虽然它属于较大的“溶解”(XPath视觉化工具)的一部分,但习俗RichTextBox被包装成一种可分割的组件,并准备在你的新项目中使用。 在视觉演播室,我只字句DL,你可以把它拖到你的设计表面。 你们只能从较大的解决办法中抛弃其他法典。

见代码

问题回答

我将把每一行都储存在一个有办法向丰富文本箱出版的类别中。 在这种方法中,你可以根据其在班级中的地位预先排出行号。

例如(大致):

class myText
{
    public List<string> Lines;

    public string GetList()
    {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        foreach (string s in Lines)
        {
            sb.AppendFormat("{0}: {1}", i, s).AppendLine();
            i++;
        }
        return sb.ToString();
    }
}

Scintilla.Net http://scintillanet.codeplex.com/ could be the most feasible solution for your needs. But for my project I used solution suggested by Cheeso (RichTextBoxEx from XPath visualizer). It s simple and fast enough for not very big documents. All other .net components from the internet were incredibly slow.

简单方式:

Dim myArray = RichTextBox1.Text.Split()

Dim cnt As Integer = 0
RichTextBox1.Clear()

Do While cnt < myArray.Count
  RichTextBox1.AppendText(cnt & ":" & myArray(cnt) & vbNewLine)
  cnt = cnt + 1
Loop
    public int getWidth()
    {
        int w = 25;
        // get total lines of richTextBox1
        int line = richTextBox1.Lines.Length;

        if (line <= 99)
        {
            w = 20 + (int)richTextBox1.Font.Size;
        }
        else if (line <= 999)
        {
            w = 30 + (int)richTextBox1.Font.Size;
        }
        else
        {
            w = 50 + (int)richTextBox1.Font.Size;
        }

        return w;
    }

    public void AddLineNumbers()
    {
        // create & set Point pt to (0,0)
        Point pt = new Point(0, 0);
        // get First Index & First Line from richTextBox1
        int First_Index = richTextBox1.GetCharIndexFromPosition(pt);
        int First_Line = richTextBox1.GetLineFromCharIndex(First_Index);
        // set X & Y coordinates of Point pt to ClientRectangle Width & Height respectively
        pt.X = ClientRectangle.Width;
        pt.Y = ClientRectangle.Height;
        // get Last Index & Last Line from richTextBox1
        int Last_Index = richTextBox1.GetCharIndexFromPosition(pt);
        int Last_Line = richTextBox1.GetLineFromCharIndex(Last_Index);
        // set Center alignment to LineNumberTextBox
        LineNumberTextBox.SelectionAlignment = HorizontalAlignment.Center;
        // set LineNumberTextBox text to null & width to getWidth() function value
        LineNumberTextBox.Text = "";
        LineNumberTextBox.Width = getWidth();
        // now add each line number to LineNumberTextBox upto last line
        for (int i = First_Line; i <= Last_Line + 2; i++)
        {
            LineNumberTextBox.Text += i + 1 + "
";
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        LineNumberTextBox.Font = richTextBox1.Font;
        richTextBox1.Select();
        AddLineNumbers();
    }

    private void richTextBox1_SelectionChanged(object sender, EventArgs e)
    {
        Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
        if (pt.X == 1)
        {
            AddLineNumbers();
        }
    }

    private void richTextBox1_VScroll(object sender, EventArgs e)
    {
        LineNumberTextBox.Text = "";
        AddLineNumbers();
        LineNumberTextBox.Invalidate();
    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        if (richTextBox1.Text == "")
        {
            AddLineNumbers();
        }
    }

    private void richTextBox1_FontChanged(object sender, EventArgs e)
    {
        LineNumberTextBox.Font = richTextBox1.Font;
        richTextBox1.Select();
        AddLineNumbers();
    }

    private void LineNumberTextBox_MouseDown(object sender, MouseEventArgs e)
    {
        richTextBox1.Select();
        LineNumberTextBox.DeselectAll();
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        AddLineNumbers();
    }

You can achieve that by drawing your own control. Here s an example how to draw yourself link





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签