如何格式化RichTextBox中的字符串 [英] How to format strings in a RichTextBox

查看:85
本文介绍了如何格式化RichTextBox中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Hi

How do I select all the items in a ListBox with a CheckBox and then display the Strings associate with each item in the ListBox on separate lines in bold in a RichTextBox?

推荐答案

使用休闲代码



Use The Fallowing Coding



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '  String 1
        Dim Position As Int16 = 0
        Dim string1 As String = "This is string1. Font=Arial: FontSize=14: Color=Blue" & vbNewLine
        Dim myFont As New Font("Arial", 14, FontStyle.Regular, GraphicsUnit.Point)
        Dim myColor As Color = Color.Blue
        rtb.Select(Position, 0)
        rtb.SelectionFont = myFont
        rtb.SelectionColor = myColor
        rtb.SelectedText = string1
        Position += string1.Length

        '  String 2
        string1 = "This is string2. Font=microsoft sans serif: FontSize=10: Color=Red" & vbNewLine
        myFont = New Font("microsoft sans serif", 10, FontStyle.Regular, GraphicsUnit.Point)
        myColor = Color.Red
        rtb.Select(Position, 0)
        rtb.SelectionFont = myFont
        rtb.SelectionColor = myColor
        rtb.SelectedText = string1
        Position += string1.Length

        '  String 3
        string1 = "This is string3. Font=courier new: FontSize=30: Color=Green: BOLD" & vbNewLine
        myFont = New Font("courier new", 30, FontStyle.Bold, GraphicsUnit.Point)
        myColor = Color.Green
        rtb.Select(Position, 0)
        rtb.SelectionFont = myFont
        rtb.SelectionColor = myColor
        rtb.SelectedText = string1
        Position += string1.Length
    End Sub
End Class


请参阅我敲过的这个演示上.
将CheckedListBox和RichTextBox添加到窗体中

load事件将向列表框中添加20个项目,还将向RTB添加相同的20个项目.
当您在列表框中选择一个项目时,它将在RTB中突出显示,您可以通过更改选中状态来切换该项目的BOLD状态.

See this demo i knocked up.
Add a CheckedListBox and RichTextBox to a form

The load event will add 20 items to a list box, it will also add the same 20 items to the RTB.
When you select an item in the listbox, it will highlight in the RTB, you can toggle the BOLD state of the item by changing the checked status.

Public class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'Add 20 items to list box and Add the 20 items to the Rich Text Box
    For x as Integer = 1 To 20
      CheckedListBox1.Items.Add("Item" + x.ToString)
      RichTextBox1.Text = RichTextBox1.Text + "Item" + x.ToString
    Next
End Sub

Private Sub CheckedListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
    'Get the Current Selected Item
    Dim item as String = CheckedListBox1.SelectedItem

    If item.Length > 0 Then
      'Find the item selected in the RTB
      Dim index As Integer = RichTextBox1.Text.IndexOf(item)
      If index >=0 Then
        'item found
        RichTextBox1.SelectionStart = index
        RichTextBox1.SelectionLength = item.Length

        'Check the checked box state for the item selected and adjust font style
        If CheckedListBox1.checkedIndices.Contains(CheckedListBox1.SelectedIndices(0)) Then
          'take current font and make it bold
          RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily,  RichTextBox1.SelectionFont.FontFamily,  FontStyle.Bold)
        Else
          'take the current font and make it regular
          RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily,  RichTextBox1.SelectionFont.FontFamily,  FontStyle.Regular)
        End If
      End If
    End If
End Sub
End Class


我不想将CheckBox放在ListBox内.ListBox在窗体上.当我检查CheckBox时,必须选择ListBox的所有项目,并且与ListBox关联的字符串必须在RichTextBox中以粗体显示在单独的行上."

啊!
选择很容易:处理CheckBox.Changed事件:
"I do not want to put a CheckBox inside a ListBox. The ListBox is on Form. When I check the CheckBox all the items of the ListBox must be selected and the strings associated with the ListBox must be displayed in the RichTextBox on seperate lines in bold font."

Ah!
Selection is easy: Handle the CheckBox.Changed event:
If myCheckBox.Checked Then
    For i As Integer = 0 To myListBox.Items.Count - 1
        myListBox.SetSelected(i, True)
    Next
End If


RTF显示也很简单:


The RTF display is also simple:

Dim sb As New StringBuilder()
sb.Append("{\rtf1\ansi ")
For Each i As Integer In myListBox.SelectedIndices
	sb.Append("\b ")
	sb.Append(myListBox.Items(i).ToString())
	sb.Append("\b0 ")
	sb.Append("\par")
Next
sb.Append("}")
myRichTextBox.Rtf = sb.ToString()


这篇关于如何格式化RichTextBox中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆