2012-10-03 61 views
1

我需要大胆一些文字,我将它们添加到RichTextBox控件,目前这里是我的代码选择和格式化文本

编辑

With txtDetails 
     If Not IsNullOrEmpty(title) Then 
     Dim intStart As Integer 
     intStart = Len(.Text) 
      .Text = .Text & title '& vbCrLf 
      .SelStart = intStart 
      .SelLength = Len(title) 
      .SelBold = True 
      .SelLength = 0 
      .SelBold = False 
      .Text = .Text & vbNewLine 
     End If 
     If Not IsNullOrEmpty(value) Then 
      .Text = .Text & value & vbNewLine 
     End If 
     .Text = .Text & vbNewLine 
    End With 

谁能帮我我已经修改了代码,但仍是修复

得到所有的后续测试我添加要大胆,insted的兴趣

回答

2

看起来要附加一个加粗title如果没有“失踪”和非粗体value如果没有“失踪”。

Option Explicit 

Private Function IsNullOrEmpty(ByVal Item As Variant) As Boolean 
    If IsNull(Item) Then 
     IsNullOrEmpty = True 
    ElseIf IsEmpty(Item) Then 
     IsNullOrEmpty = True 
    ElseIf VarType(Item) = vbString Then 
     If Len(Item) = 0 Then 
      IsNullOrEmpty = True 
     End If 
    End If 
End Function 

Private Sub cmdAppend_Click() 
    With rtbDisplay 
     .SelStart = &H7FFFFFFF 
     If Not IsNullOrEmpty(txtTitle.Text) Then 
      .SelBold = True 
      .SelText = txtTitle.Text 
      txtTitle.Text = "" 
      .SelBold = False 
      .SelText = vbNewLine 
     End If 
     If Not IsNullOrEmpty(txtValue.Text) Then 
      .SelText = txtValue.Text 
      txtValue.Text = "" 
      .SelText = vbNewLine 
     End If 
    End With 
    txtTitle.SetFocus 
End Sub 

这里我使用TextBox控件作为数据源,但它应该给你一个总体思路。使用两个操作比使用字符串连接添加换行符要便宜得多。

撷取电流。文本,并用LEN()测定它也是昂贵的,如果内容是大的,所以只需设置.SelStart为最大值移动到端。

+0

文本追加到RTB控制使文本失去其格式 – Smith

+0

我想我不知道什么是“格式”你指的是。上面的代码就像你所问的一样,尽我所能。我没有看到任何“格式丢失”。 – Bob77

2
一上午

设置.Text有一定的副作用,能防止你的代码做你想要什么:

  1. 它重置.SelStart所以你需要保存的.Text第一长度。
  2. 它重置所有的格式,所以胆量就会丢失。
+0

请我编辑的代码 – Smith

-1

我没有进入vb,但我创建了一个项目并进行了测试。
试试这个

With txtDetails 
     .SelectionStart = 0 
     .SelectionLength = txtDetails.TextLength 
     .SelectionFont = New Font(txtDetails.Font, FontStyle.Bold) 
    End With 
+0

我问了一个VB6的问题,而不是VB.NET – Smith