如何通过鼠标单击启用禁用的文本框 vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17497943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable a disabled textbox from mouse click vb.net
提问by SSpoke
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?
我是否必须使用计时器等来跟踪鼠标点击和文本框的 X、Y 位置。点击它不会触发任何事件?
回答by Idle_Mind
You can use IMessageFilter to trap WM_LBUTTONDOWN messages and then check to see if the cursor is within the TextBox...something like:
您可以使用 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
回答by Asrhael
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).
作为替代方法,当 MouseClick 事件到达另一个控件(例如另一个 TextBox)时,您可以将 ReadOnly 控件属性设置为 True,将 Text 属性设置为 ""。
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
回答by SSpoke
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.
但是首先将颜色设置为禁用我发现使用 ButtonFace 颜色可能效果最好,它看起来确实很真实。
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
我的意图从来不是禁用它,而是让用户认为它被禁用,直到他点击它......当他点击其他地方时它会被禁用
回答by logixologist
When your text box is in the enabled = falsestate you cant click on it with a mouse.
当您的文本框处于该enabled = false状态时,您无法用鼠标单击它。

