2014-01-14 125 views
1

我在用戶窗體上有一個文本框。當用戶窗體顯示時,文本框以一些 默認值打開。在VBA的文本框中鎖定一行以進行編輯

我希望它是默認消息的Line1(或某些單詞)以灰色顯示並且必須鎖定以進行編輯。可能嗎?

+0

你是什麼意思與「鎖定編輯」?用戶不能刪除默認消息,但添加更多文本? – Manu

+0

不能刪除,不能添加。其實我想要第一行被鎖定進行編輯。我不認爲它是可能的。 –

+0

我也提出了一個替代方案。您可能需要刷新頁面。 –

回答

2

鎖定爲編輯文本框?

是的。這是可能的。

只需設置.Locked物業True

鎖定/着色只是第一行或在textBox中文字的一部分嗎?

。這在VBA中是不可能的。對於部分着色,您可能希望使用RichTextBox代替TextBox,但是您再次無法部分鎖定控件。

編輯

替代:由於文本的第一行包含不應該被修改,那麼爲什麼使用TextBox.ControlTipText財產不顯示在ToolTip該信息或者說一個Label案文當你將鼠標懸停在TextBox的上方時顯示?

例如(使用.ControlTipText屬性)

Option Explicit 

'~~> This is what goes in the tooltip. Amend as applicable. 
Const sMsg As String = "Hello World! This is an example of tooltip text" 

Private Sub UserForm_Initialize() 
    Dim sSample As String 
    Dim i As Long 

    For i = 1 To 10 
     sSample = sSample & "Blah Blah" & i & vbNewLine 
    Next i 

    TextBox1.Text = sSample 

    '~~> Set to starting point 
    TextBox1.SelStart = 0 
End Sub 

Private Sub TextBox1_MouseMove(ByVal Button As Integer, _ 
           ByVal Shift As Integer, _ 
           ByVal X As Single, _ 
           ByVal Y As Single) 
    TextBox1.ControlTipText = sMsg 
End Sub 

而現在,當您將鼠標懸停在文本框的頂部的文本,你將看到的ToolTip

enter image description here

0

這是一個比解決方案更多的解決方法,但它工作得很完美(至少在我的電腦上)。

Const defaultLine As String = "YourText" & vbcrlf 

Private Sub TextBox1_Change() 
    Static oldValue As String 

    'set oldValue if empty 
    If oldValue = "" Then 
     oldValue = defaultLine 
    End If 

    'disable the change of the first line 
    If Left(TextBox1.Value, Len(defaultLine)) <> defaultLine Then 
     TextBox1.Value = oldValue 
    Else 
     oldValue = TextBox1.Value 
    End If 
End Sub 

Private Sub UserForm_Initialize() 
    TextBox1.Value = defaultLine 
End Sub 
相關問題