VB.NET禁用ListView而不将Enable属性设置为false

时间:2015-09-12 18:46:45

标签: vb.net listview

有没有办法在不将enable属性设置为False的情况下禁用ListView。基本上,捕获和忽略任何鼠标点击或键盘输入。 VB2013。

1 个答案:

答案 0 :(得分:1)

这是另一种方法......

让ListView将自身绘制到位图并在PictureBox中显示它取代ListView。根据需要交换两个控件:

Public Class Form1

    Private pbListView As PictureBox
    Private ListViewDisabled As Boolean = False

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not ListViewDisabled Then
            pbListView = New PictureBox
            Dim bmp As New Bitmap(ListView1.Size.Width, ListView1.Size.Height)
            ListView1.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
            pbListView.Image = bmp
            pbListView.SizeMode = PictureBoxSizeMode.AutoSize
            pbListView.Location = ListView1.Location
            pbListView.Dock = ListView1.Dock
            ListView1.Parent.Controls.Add(pbListView)
            pbListView.BringToFront()
            ListView1.Enabled = False
            ListViewDisabled = True
        Else
            ListView1.Enabled = True
            pbListView.Dispose()
            ListViewDisabled = False
        End If
    End Sub

End Class

这将允许您禁用ListView,使其无法获得焦点(处理键盘输入),并让PictureBox处理鼠标输入抑制。

如果您需要使用当前关注的项目捕获ListView,则必须采用实际的屏幕截图。但是,这可能会有问题,因为其他窗口可能会遮挡窗口并在图像中捕获。