在禁用文本框在VB.NET的WinForms的启用滚动条 [英] Enabled Scrollbar in a Disabled Textbox in VB.NET for WinForms

查看:273
本文介绍了在禁用文本框在VB.NET的WinForms的启用滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让滚动即使文本框设置为多行,文本框

  textbox.Enabled =假
 

这是不可能的,因为滚动条被禁用的的启用的-Command了。

这里默认的解决方案是设置

  textbox.ReadOnly = TRUE
textbox.Enabled = TRUE
 

但这并没有真正做的伎俩我。通过的只读的我还可以选择一个文本框的文本,也可以代替它里面的光标。但因为我有正常(非的的-TextBoxes)和相同的形式,我不希望这样的事情发生其他控件。我想有完全一样的行为,因为所有其他残疾人文本框。

对于一切,就像一个残疾文本框的模仿的颜色等,则说明的只读的-Property合法的解决办法,但我coudn't找到任何选择文本和放置光标。

更新:

与<一个一些提示href="http://stackoverflow.com/questions/3524754/scrollbar-flicker-when-calling-enablescrollbar">here我试图用WIN32 API,但预期它没有工作:

 进口System.Windows.Forms的
进口了System.Runtime.InteropServices

公共类TestTextBox
    继承文本框

    私有类原生
        &LT;的DllImport(user32.dll中)&GT; _
        朋友共享功能EnableScrollBar(BYVAL的HWND作为IntPtr的,BYVAL wSBflags作为UInteger,BYVAL wArrows作为UInteger)作为布尔
        端功能
        &LT;的DllImport(的User32.dll)&GT; _
        朋友共享SendMessage函数(BYVAL的HWND作为IntPtr的,BYVAL味精作为UInteger,BYVAL WPARAM作为整数,BYVAL LPARAM为整数)作为整数
        端功能

        公共常量WM_SETREDRAW只要=安培; HB
        公共常量ESB_ENABLE_BOTH作为UInteger = 0
        公共常量SB_VERT作为UInteger = 1
    末级

    公用Sub变化()被调用在我的例子一个按钮。
        'Native.SendMessage(Me.Handle,Native.WM_SETREDRAW,新的IntPtr(0),IntPtr.Zero)
        Native.EnableScrollBar(Me.Handle,Native.SB_VERT,Native.ESB_ENABLE_BOTH)
        'Native.SendMessage(Me.Handle,Native.WM_SETREDRAW,新的IntPtr(1),IntPtr.Zero)
        Me.PerformLayout()
    结束小组

末级
 

解决方案

我觉得这应该工作。 HideCaret可确保插入符号,那么,隐藏在该文本框是只读的情况。其他WM截获prevent从选择用鼠标或键盘的任何用户。

 导入了System.Runtime.InteropServices

公共类CustomTextbox
    继承System.Windows.Forms.TextBox

    私人常量WM_KEYDOWN =安培; H100
    私人常量WM_SYSKEYDOWN =安培; H104
    私人常量WM_MOUSEMOVE =安培; H200

    &LT;的DllImport(user32.dll中)&GT; _
    私人共享功能HideCaret(BYVAL HWND作为IntPtr的)作为整数
    端功能

    受保护的覆盖子OnGotFocus(BYVALË作为System.EventArgs)
        如果Me.ReadOnly然后HideCaret(Me.Handle)
        MyBase.OnGotFocus(五)
    结束小组

    受保护的覆盖子的WndProc(为ByRef米作为System.Windows.Forms.Message)
        如果Me.ReadOnly及(m.Msg = WM_MOUSEMOVE)或_
                           (m.Msg = WM_KEYDOWN)或_
                           (m.Msg = WM_SYSKEYDOWN)然后退出小组
        MyBase.WndProc(米)
    结束小组
末级
 

I'm trying to allow scrolling in a Multiline-TextBox even if the TextBox is set to

textbox.Enabled = False

This is not possible, as the Scrollbar is disabled with the Enabled-Command, too.

The default solution here is to set

textbox.ReadOnly = True
textbox.Enabled = True

but this doesn't really do the trick for me. With ReadOnly I can still select the text of a TextBox as well as place the cursor inside of it. But as I have normal (non Multiline-TextBoxes) and other controls on the same form I don't want that to happen. I want to have exactly the same behavior as all the other disabled TextBoxes.

For everything else, like mimic the color of a disabled textbox and so on, there is a legitimate workaround with the ReadOnly-Property, but I coudn't find any for selecting text and placing the cursor.

UPDATE:

With some hints from here I tried to use WIN32 API but it didn't work as expected:

Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class TestTextBox
    Inherits TextBox

    Private Class Native
        <DllImport("user32.dll")> _
        Friend Shared Function EnableScrollBar(ByVal hWnd As IntPtr, ByVal wSBflags As UInteger, ByVal wArrows As UInteger) As Boolean
        End Function
        <DllImport("User32.dll")> _
        Friend Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As UInteger, ByVal wparam As Integer, ByVal lparam As Integer) As Integer
        End Function

        Public Const WM_SETREDRAW As Long = &HB
        Public Const ESB_ENABLE_BOTH As UInteger = 0
        Public Const SB_VERT As UInteger = 1
    End Class

    Public Sub Change() 'Gets called by a Button in my example.
        'Native.SendMessage(Me.Handle, Native.WM_SETREDRAW, New IntPtr(0), IntPtr.Zero)
        Native.EnableScrollBar(Me.Handle, Native.SB_VERT, Native.ESB_ENABLE_BOTH)
        'Native.SendMessage(Me.Handle, Native.WM_SETREDRAW, New IntPtr(1), IntPtr.Zero)
        Me.PerformLayout()
    End Sub

End Class

解决方案

I think this should work. HideCaret makes sure that the caret is, well, hidden in the case that the textbox is ReadOnly. The other WM intercepts prevent the user from selecting anything with mouse or keyboard.

Imports System.Runtime.InteropServices

Public Class CustomTextbox
    Inherits System.Windows.Forms.TextBox

    Private Const WM_KEYDOWN = &H100
    Private Const WM_SYSKEYDOWN = &H104
    Private Const WM_MOUSEMOVE = &H200

    <DllImport("user32.dll")> _
    Private Shared Function HideCaret(ByVal hwnd As IntPtr) As Integer
    End Function

    Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
        If Me.ReadOnly Then HideCaret(Me.Handle)
        MyBase.OnGotFocus(e)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If Me.ReadOnly And (m.Msg = WM_MOUSEMOVE) Or _
                           (m.Msg = WM_KEYDOWN) Or _
                           (m.Msg = WM_SYSKEYDOWN) Then Exit Sub
        MyBase.WndProc(m)
    End Sub
End Class

这篇关于在禁用文本框在VB.NET的WinForms的启用滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆