问题
Simple how to enable a Textbox which is disabled by clicking on it? how is this done?
my code doesn't work
Private Sub Textbox1_MouseClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textbox1.MouseClick
Textbox1.Enabled = True
End Sub
Can anyone help me out.
Do I have to resort to tracking the mouse clicks and X,Y positions of textbox with timers etc.. no events are fired from clicking it?
回答1:
You can use IMessageFilter to trap WM_LBUTTONDOWN messages and then check to see if the cursor is within the TextBox...something like:
Public Class Form1
Private WithEvents filter As New MyFilter
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TextBox1.Enabled = False
Application.AddMessageFilter(filter)
End Sub
Private Sub filter_LeftClick() Handles filter.LeftClick
Dim rc As Rectangle = TextBox1.RectangleToScreen(TextBox1.ClientRectangle)
If rc.Contains(Cursor.Position) AndAlso Not TextBox1.Enabled Then
TextBox1.Enabled = True
TextBox1.Focus()
End If
End Sub
Private Class MyFilter
Implements IMessageFilter
Public Event LeftClick()
Private Const WM_LBUTTONDOWN As Integer = &H201
Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
Select Case m.Msg
Case WM_LBUTTONDOWN
RaiseEvent LeftClick()
End Select
Return False
End Function
End Class
End Class
回答2:
What worked for me seems like the best way to go is to do something like this.
Private Sub TextBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.MouseClick
TextBox1.BackColor = Color.Empty
End Sub
and to disable it run this kind of code
'To lose focus from textbox otherwise it will have a blinker
Label1.Focus()
TextBox1.BackColor = TextBox.DefaultBackColor
But first set the color to disabled I found that using ButtonFace color probably works best, it sure looks real.
TextBox1.BackColor = SystemColors.ButtonFace
my intention was never to disable it, but to make the user think it's disabled until he clicks it.. when he clicks somewhere else it turns disabled
回答3:
As an alternative, you can set the ReadOnly control property to True and the Text property to "" when a MouseClick event reach another control (another TextBox for example).
That work fine for me. My code is:
Private Sub TxtNameIn_Click(sender As Object, e As EventArgs) Handles TxtNameIn.MouseClick
Me.TxtNameIn.ReadOnly = False
Me.TxtPatternIn.ReadOnly = True
Me.TxtPatternIn.Text = ""
End Sub
Private Sub TxtPatternIn_Click(sender As Object, e As EventArgs) Handles TxtPatternIn.MouseClick
Me.TxtPatternIn.ReadOnly = False
Me.TxtNameIn.ReadOnly = True
Me.TxtNameIn.Text = ""
End Sub
回答4:
When your text box is in the enabled = false
state you cant click on it with a mouse.
来源:https://stackoverflow.com/questions/17497943/how-to-enable-a-disabled-textbox-from-mouse-click-vb-net