使窗体控件无响应

问题描述 投票:0回答:2

我想以某种方式使Windows窗体控件不负责任 - 比如将Control.Enabled设置为False,但没有它的视觉效果(我将有一些自定义的“控制忙”指示器,所以最终用户不会不清楚为什么控制没有反应)。

原因是:我正在为Windows窗体编写繁忙的指示器控件,并希望它尽可能通用。

目的是能够像下面这样使用它。

Dim busy_control As Control = ...
ShowBusyIndicator(busy_control)
BeginDoWork() 'starts a worker thread

'in some OnCompleted-Event:
HideBusyIndicator(busy_control)

我目前的问题是,我想确保busy_control根本不会对任何用户输入作出反应。

在当前版本中,我确保控件失去焦点,并且永远不能通过处理GotFocus事件再次获得它。由于覆盖控件的Parent是busy_control,我还将OnMouseWheel事件设置为处理(否则busy_control可以滚动)。

我想还有更多这样的事件。这就是为什么我想“禁用”控件而不实际设置为false。

有没有办法做到这一点?

vb.net winforms
2个回答
0
投票

我也建议你不要这样做......但是,如果你必须......

只需将要抑制/禁用的控件传递给此子。显然,当你重新启用控件时,你需要删除创建的图片框......但我会让你弄清楚。

Private Sub SuppressControl(ByRef CtrlToSuppress As Control)
    ''create an image of the control
    Dim ctrlImage As New Bitmap(CtrlToSuppress.Width, CtrlToSuppress.Height)
    CtrlToSuppress.DrawToBitmap(ctrlImage, CtrlToSuppress.ClientRectangle)
    ''create a picturebox in the same location/size as the control
    Dim PictureBox1 As New PictureBox
    PictureBox1.Left = CtrlToSuppress.Left
    PictureBox1.Top = CtrlToSuppress.Top
    PictureBox1.Width = CtrlToSuppress.Width
    PictureBox1.Height = CtrlToSuppress.Height
    ''Set the anchors to maintain alignment on form resize
    PictureBox1.Anchor = CtrlToSuppress.Anchor
    ''place the image in picturebox
    PictureBox1.Image = ctrlImage
    ''add the picture box to the form
    CtrlToSuppress.Parent.Controls.Add(PictureBox1)
    ''and bring it to the front
    PictureBox1.BringToFront()
    ''Suppress the control
    CtrlToSuppress.Enabled = False
End Sub

-1
投票

你最好的选择可能是一个透明的Panel。通过将其放在您的控件上,您想要“禁用”,您可以拦截任何单击它的尝试,因为Panel将注册点击。您也可以将OnClick手柄添加到Panel

由于你已经在使用类似“旋转器”的东西,你可能想把你的加载指示器放在那个Panel上。这样,用户不会对为什么点击无效或为什么控件看起来不被禁用感到困惑。如果没有,只需使整个Panel透明。有关如何控制Panel透明度的信息,请参阅here

请注意,在链接的示例中,由于操作系统的限制,您需要在技术上解释Form级别的透明度。您将找到可以帮助您在该链接上构建所需内容的参考,具体取决于您定位的Windows版本。

我不能强调使用你的微调器来覆盖你禁用的东西会更好。它比使控件假装它没有被禁用更清晰(并且更容易编码,老实说)。

© www.soinside.com 2019 - 2024. All rights reserved.