简体   繁体   English

如何通过鼠标单击启用禁用的文本框vb.net

[英]How to enable a disabled textbox from mouse click vb.net

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位置。单击不会触发任何事件?

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

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 我的意图绝不是禁用它,而是让用户认为它已被禁用,直到他单击它。

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

当文本框处于enabled = false状态时,无法用鼠标单击它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM