c# - 如何使用 (anyword +'་') 创建一个后续字符,然后使用 c# 在 WinForm 中创建下一个新行?

标签 c# winforms richtextbox

如何使用自定义字符创建自定义自动换行。 IE。我想使用 .(dot) 字符而不是 来包装文本.

我正在使用 c# WinForm 在 richTextBox 中做一个关于自动换行的项目,因此我需要一起使用 (anyword+'་') 进行自动换行,然后创建下一个新行,因为我的问题是在每个自动换行 '་' 之后如下例所示,出现在新行的开头/开头,'་' 应该出现在每一行的结尾

假设我给出了原始字符串,例如: རྒྱ་གར་ཚོང་པའི་ལྷན་ཚོགས་དངོས་སུ 一旦我使用了 wordwrap 概念,它就会像这样 རྒྱ་གར་ཚོང་པའི//我们假设这是第一行

་ལྷན་ཚོགས་དངོས་སུ//this is second line and second line start with '་' so ending line should stay (anyword+'་')  before break the line and it should happen for linewise.

所以为了避免这个问题我想到了做“following character”的概念。经过如此多的谷歌搜索和浏览后,我发现了我正在寻找的以下线程,但遗憾的是他们使用的是不同的工具和平台。我尝试了很多使用这些概念并使用 C#(WinForm) 实现我的项目但无法工作。因此,请与我分享您的线程并帮助我使用 c# (WinForm) 来完成我的项目。您的帮助对我意义重大。

https://www.youtube.com/watch?v=uTajI2lWwgE http://www.c-sharpcorner.com/UploadFile/72d20e/canvas-text-wrapping-using-html-5/
谢谢!

最佳答案

如果您希望在每个 . 之后添加新行,那么您需要替换文本,即 anyword。 anywordanyword.\r\nanyword 其中 \r\n 是新行的序列。但是,它可能会产生一些问题,比如你写 M.B.B.S.那么它可能像 M.\r\nB.\r\nB.\r\nS.\r\n

string str = "This is the simple text. Hello world".Replace(". ", ".\r\n");

输出:

This is the simple text.
Hello world.

已编辑:

int tmpIndex = 0;
int startIndex = 0;
int lastIndex = 0;
string sChar = ".";
string strText = richTextBox1.Text;

Graphics g = this.CreateGraphics();
StringBuilder sb = new StringBuilder();

while (tmpIndex > -1)
{
    lastIndex = tmpIndex;
    tmpIndex = strText.IndexOf(sChar, tmpIndex + 1);
    if (tmpIndex < 0)
        tmpIndex = strText.Length - 1;

    if (g.MeasureString(strText.Substring(startIndex, tmpIndex - startIndex), richTextBox1.Font).Width > richTextBox1.Width || tmpIndex == (strText.Length-1))
    {
        sb.AppendLine(strText.Substring(startIndex, lastIndex - startIndex));
        startIndex = lastIndex;                    
        if (tmpIndex == (strText.Length - 1))
            break;
        tmpIndex = lastIndex;
    }
}
richTextBox1.Text = sb.ToString();

关于c# - 如何使用 (anyword +'་') 创建一个后续字符,然后使用 c# 在 WinForm 中创建下一个新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25946064/

相关文章:

c# - 为什么使用 64 位哈希而不是字符串时字典的内存消耗会增加?

c# - 运行空间 PowerShell 输出

属性网格下拉列表中的 C# SSIS 自定义控制流任务列表变量

c# - 保持演示者引用处于事件状态,而不提供对其控制的 View 的引用

vb.net - 适用于 vb.net 的高效、无闪烁语法荧光笔,其工作效果与 IDE 中的语法荧光笔一样好

c# - Entity Frameworks 插入已插入的相关项

c# - 用 RAR 压缩文件

c# - 验证 DataGridView Windows 窗体

c# - 富文本框 CtrlI

angularJS textAngular输入框输入文本后失去焦点