2012-02-06 93 views
0

我想那裏會有這樣的問題,但我沒有任何運氣搜索。我看到有人問同樣的問題,但答案是使用TextChanged事件。這不是我想要的。如何在TextBox上實現TextChanging屬性?

TextBox在Text屬性更改後發生TextChanged事件。在Text屬性實際更改爲驗證數據之前,我需要我的控件來引發事件。如果它是有效的文本可以改變,如果它是無效的文字不會改變。

這裏是我的嘗試:

Public Class TextChangingEventArgs 
    Inherits System.ComponentModel.CancelEventArgs 

    Private p_sNewValue As String 

    Public Sub New() 
    p_sNewValue = String.Empty 
    End Sub 

    Public Sub New(sNewValue As String) 
    p_sNewValue = sNewValue 
    End Sub 

    Public Property NewValue As String 
    Get 
     Return p_sNewValue 
    End Get 
    Set(value As String) 
     p_sNewValue = value 
    End Set 
    End Property 
End Class 

Public Class BetterTextBox 
    Inherits TextBox 

    Public Event TextChanging(sender As Object, e As TextChangingEventArgs) 

    Public Overrides Property Text As String 
    Get 
     Return MyBase.Text 
    End Get 
    Set(value As String) 
     Dim e As New TextChangingEventArgs(value) 
     RaiseEvent TextChanging(Me, e) 
     If e.Cancel = False Then 
     MyBase.Text = value 
     End If 
    End Set 
    End Property 
End Class 

我在我的表我處理TextChanging事件:

Private Sub BetterTextBox1_TextChanging(sender As System.Object, e As TextChangingEventArgs) Handles BetterTextBox1.TextChanging 
    e.Cancel = Not Regex.IsMatch(e.NewValue, REGEX_PATTERN) 
End Sub 

這適用於編程設置BetterTextBox控制的文本價值,但它不當你在文本框中輸入時工作。

有沒有人知道我需要做什麼才能使其工作?

+0

不管這是太煩人了。我只是在TextChanged事件中處理它,並保留最後一個有效值的本地副本。如果該字符串在TextChanged中無效,我只會將它恢復到以前的有效值。 – Nick 2012-02-06 20:46:54

回答

0

嘗試使用文本框的KeyPress事件。

編輯:要禁用文本框中的右鍵單擊,可以這樣做。 將此代碼放入文本框的MouseDown事件中。

If e.Button = MouseButtons.Right Then 
    MessageBox.Show("can't paste!") 
    Exit Sub 
End If 

但是我注意到你仍然可以使用組合鍵Ctrl + V.您可以編寫代碼來停止在KeyPress事件粘貼文本。

If Keys.ControlKey And Keys.V Then 
     MessageBox.Show("can't paste!") 
     Exit Sub 
End If 
+0

那麼在文本框中右鍵單擊並選擇上下文菜單中的粘貼?我這樣做是因爲我想避免KeyPress。這只是不夠好。它也不適用於其他輸入方法,如語音到文本。 – Nick 2012-02-06 20:24:43

+0

編輯了答案,以防止使用文本框內的上下文菜單。 – Isuru 2012-02-06 20:38:42

+0

我不想刪除用戶可能需要的功能。我只是想讓控制做我想做的事情。 – Nick 2012-02-06 20:53:37

0

我建議你應該嘗試與keydown事件。它出現在textchanged之前或者甚至在按鍵之前。

+0

這不適用於將文本粘貼到控件中,或使用任何其他不涉及按鍵的輸入方法,如語音到文本。 – Nick 2012-02-06 20:30:25

2

由於您已經從TextBox繼承,您可以覆蓋WndProc並檢查粘貼消息。這應該解決右鍵單擊>粘貼問題。正如其他人所建議的那樣,您可以在KeyPressed或KeyDown中處理「常規」輸入文本。 這裏就是我談論的例子:

Private Const WM_PASTE As Integer = &H302 
Protected Overrides Sub WndProc(ByRef m As Message) 
    If m.Msg = WM_PASTE AndAlso Clipboard.ContainsText() AndAlso ShouldAllowPaste(Clipboard.GetText()) Then 
     MyBase.WndProc(m) 
    ElseIf m.Msg <> WM_PASTE Then 
     MyBase.WndProc(m) 
    End If 
End Sub 
1

試試這個:

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 
    If m.Msg = 8465 Then 
     If HIWORD(m.WParam) = 1024 Then 
      ' raise your TextChanging event 
     End If 
    End If 

    MyBase.WndProc(m) 
End Sub 

Public Function HIWORD(ByVal n As Integer) As UInteger 
    Return CUInt(n >> 16) And CUInt(&HFFFF) 
End Function 
+0

在wparam上檢查hiword的目的是什麼? – Nick 2012-02-07 15:47:31