如何启用通过单击禁用的文本框?这是怎么做到的?
我的代码不起作用
Private Sub Textbox1_MouseClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textbox1.MouseClick
Textbox1.Enabled = True
End Sub
任何人都可以帮助我。
我是否必须使用定时器跟踪鼠标点击和文本框的X,Y位置等等。点击它不会触发任何事件?
答案 0 :(得分:5)
您可以使用IMessageFilter捕获WM_LBUTTONDOWN消息,然后检查光标是否在TextBox中......如下所示:
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
答案 1 :(得分:1)
对我有用的似乎是最好的方法就是做这样的事情。
Private Sub TextBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.MouseClick
TextBox1.BackColor = Color.Empty
End Sub
并禁用它运行此类代码
'To lose focus from textbox otherwise it will have a blinker
Label1.Focus()
TextBox1.BackColor = TextBox.DefaultBackColor
但是首先将颜色设置为禁用我发现使用ButtonFace颜色可能效果最好,它看起来确实真实。
TextBox1.BackColor = SystemColors.ButtonFace
我的意图是永远不要禁用它,但要让用户认为它被禁用直到他点击它...当他点击其他地方时它会被禁用
答案 2 :(得分:1)
作为替代方案,您可以将ReadOnly控件属性设置为True,将Text属性设置为""当MouseClick事件到达另一个控件(例如另一个TextBox)时。
这对我来说很好。我的代码是:
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
答案 3 :(得分:0)
当您的文本框处于enabled = false
状态时,您无法用鼠标点击它。