如何在 RichTextBox 中选择性地为字符串添加下划线?

发布于 2024-07-19 02:10:39 字数 1260 浏览 4 评论 0原文

在我的程序中,单击按钮后 - 选定的 ListView 条目应复制到 RichTextBox。 ListView包含联系人信息,我想要实现的效果类似于Oultook中的效果(当从通讯录中选择联系人时)。 用于此目的的部分代码如下所示:

    private void toButton_Click(object sender, EventArgs e)
    {
        int start = 0;
        for (int i = 0; i < contactsListView.SelectedItems.Count; i++)
        {
            if (contactsTextBox.TextLength != 0) contactsTextBox.Text += "; ";
            start = contactsTextBox.TextLength;
            contactsTextBox.Text += contactsListView.SelectedItems[i].Text + " " + contactsListView.SelectedItems[i].SubItems[1].Text + " [" + contactsListView.SelectedItems[i].SubItems[2].Text + "]";
            contactsTextBox.Select(start, contactsTextBox.TextLength);       
            contactsTextBox.SelectionFont = new Font(contactsTextBox.SelectionFont, FontStyle.Underline);
            contactsTextBox.DeselectAll();
            contactsTextBox.SelectionFont = new Font(contactsTextBox.SelectionFont, FontStyle.Regular);
        }
    }

不幸的是,整个文本继承了 FontStyle,并且我从 ListView 输入后输入的所有内容也都带有下划线。

所以我的问题是 - 如何仅在某些文本下划线(我犯了错误)?

stackoverflow上有类似的主题这里不幸的是在我的案例解决方案中指出了会造成资源浪费。

In my program after clicking on the button - selected ListView entries should be copied to RichTextBox. ListView contains contact information, and the effect I want to accomplish is similar to the one in Oultook (when choosing contacts from contact book). Part of my code that serves this purpose looks like that:

    private void toButton_Click(object sender, EventArgs e)
    {
        int start = 0;
        for (int i = 0; i < contactsListView.SelectedItems.Count; i++)
        {
            if (contactsTextBox.TextLength != 0) contactsTextBox.Text += "; ";
            start = contactsTextBox.TextLength;
            contactsTextBox.Text += contactsListView.SelectedItems[i].Text + " " + contactsListView.SelectedItems[i].SubItems[1].Text + " [" + contactsListView.SelectedItems[i].SubItems[2].Text + "]";
            contactsTextBox.Select(start, contactsTextBox.TextLength);       
            contactsTextBox.SelectionFont = new Font(contactsTextBox.SelectionFont, FontStyle.Underline);
            contactsTextBox.DeselectAll();
            contactsTextBox.SelectionFont = new Font(contactsTextBox.SelectionFont, FontStyle.Regular);
        }
    }

Unfortunately somehow FontStyle is inherited by entire text and everything I enter after entry from ListView is also underlined.

So my question is - how to underline only certain text (where I have made a mistake)?

There is similar topic on stackoverflow here unfortunately in my case solution stated there will be waste of resources.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

魂ガ小子 2024-07-26 02:10:39

试试这个:

        int start = 0;
        for (int i = 0; i < contactsListView.SelectedItems.Count; i++)
        {
            if (contactsTextBox.TextLength != 0) contactsTextBox.Text += "; ";
            start = contactsTextBox.TextLength;
            contactsTextBox.Text += contactsListView.SelectedItems[i].Text +" " + contactsListView.SelectedItems[i].SubItems[1].Text + " [" + contactsListView.SelectedItems[i].SubItems[2].Text + "]";
        }


        this.contactsTextBox.Text += " ";
        this.contactsTextBox.SelectionStart = 0;
        this.contactsTextBox.SelectionLength = this.contactsTextBox.Text.Length-1;
        contactsTextBox.SelectionFont = new Font(contactsTextBox.SelectionFont, FontStyle.Underline);
        this.contactsTextBox.SelectionLength = 0;

Total hack,但基本上,如果您在文本全部存在后选择文本,但不要选择全部文本(这就是我添加额外的“”的原因),然后设置选择文本,它就会具有所需的文本影响。

Try this instead:

        int start = 0;
        for (int i = 0; i < contactsListView.SelectedItems.Count; i++)
        {
            if (contactsTextBox.TextLength != 0) contactsTextBox.Text += "; ";
            start = contactsTextBox.TextLength;
            contactsTextBox.Text += contactsListView.SelectedItems[i].Text +" " + contactsListView.SelectedItems[i].SubItems[1].Text + " [" + contactsListView.SelectedItems[i].SubItems[2].Text + "]";
        }


        this.contactsTextBox.Text += " ";
        this.contactsTextBox.SelectionStart = 0;
        this.contactsTextBox.SelectionLength = this.contactsTextBox.Text.Length-1;
        contactsTextBox.SelectionFont = new Font(contactsTextBox.SelectionFont, FontStyle.Underline);
        this.contactsTextBox.SelectionLength = 0;

Total hack, but basically, if you select the text after it's all there, but don't select ALL of it (which is why I add the extra " ") and then set the selection text, it has the desired effect.

柠檬心 2024-07-26 02:10:39

您的代码的问题在于 SelectionFont 就是选择的字体。 如果没有选择,则字体更改不会执行任何操作。 BFree 提供的解决方案似乎可行。 这与我在 WORD 中输入文档时所做的事情是一样的——我会在加下划线之前在带下划线的部分后面添加一些字符,这样额外的字符将“保存”原始格式,以便我继续输入文档时使用。

BFree +1,但我还没有声誉:( ...

The problem with your code is that the SelectionFont is just that -- the font of the selection. If there is no selection, the font change isn't going to do anything. The solution that BFree offered seems like it would work. That's the same thing that I would do if I was typing the document in WORD -- I'd add some characters after the underlined section before I underlined it so the extra characters would "save" the original formatting for when I continued the document.

+1 for BFree, but I don't have a reputation yet :( ...

知你几分 2024-07-26 02:10:39

在向文本框末尾添加更多文本之前,请将光标置于末尾,并将字体设置为所需的样式。 然后调用 rtb.AppendLine() 应该会产生所需的结果。

请务必记住,RTB 控件的执行方式与任何其他文字处理器相同。 您设置样式并开始打字。 然后,您在设置该样式后键入的任何内容都将采用 corsor 下的那些属性。

更新:
这看起来效果很好。

Dim tTexts() As String = {"Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me"}
    Dim tUnderline As Boolean = False
    Dim tIndex As Integer = 0

    With oRTB
        For tIndex = tTexts.GetLowerBound(0) To tTexts.GetUpperBound(0)
            If tUnderline Then
                .SelectionStart = .Text.Length
                .SelectionFont = New Font("Arial", 12, FontStyle.Underline)
            Else
                .SelectionStart = .Text.Length
                .SelectionFont = New Font("Arial", 12, FontStyle.Regular)
            End If
            .AppendText(tTexts(tIndex))
            tUnderline = Not tUnderline
        Next
    End With

Before adding more text to the end of the text box, position the cursor at the end, and set the font to the desired style. Then a call to rtb.AppendLine() should produce the desired results.

It's key to remember that the RTB control performs in the same manner as any other word processor. You set a style and start typing. Then anything you type after setting that style will take on those attributes that are under the corsor.

Update:
This appears to work perfectly.

Dim tTexts() As String = {"Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me"}
    Dim tUnderline As Boolean = False
    Dim tIndex As Integer = 0

    With oRTB
        For tIndex = tTexts.GetLowerBound(0) To tTexts.GetUpperBound(0)
            If tUnderline Then
                .SelectionStart = .Text.Length
                .SelectionFont = New Font("Arial", 12, FontStyle.Underline)
            Else
                .SelectionStart = .Text.Length
                .SelectionFont = New Font("Arial", 12, FontStyle.Regular)
            End If
            .AppendText(tTexts(tIndex))
            tUnderline = Not tUnderline
        Next
    End With
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文