vb.net 判断一个DataGridView的水平滚动条是否存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16673293/
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
Determine if a DataGridView's horizontal scroll bar exists?
提问by Rose
Visual Basic 2010, .NET 3.5 Is there a way to pragmatically determine if a DataGridView's horizontal scroll bar is active or visible? I need to move a few items about when the DGV's horizontal scroll bar comes on.
Visual Basic 2010, .NET 3.5 有没有办法切实地确定 DataGridView 的水平滚动条是活动的还是可见的?当 DGV 的水平滚动条出现时,我需要移动一些项目。
回答by tezzo
Try this:
尝试这个:
dgvYourDataGridView.Controls.OfType(Of HScrollBar).SingleOrDefault.Visible
dgvYourDataGridView.Controls.OfType(Of HScrollBar).SingleOrDefault.Visible
回答by Nianios
Just to give you an idea (I have no time right now , or visual studio in front of me):
只是给你一个想法(我现在没有时间,或者我面前的视觉工作室):
For Each c In DataGridView1.Controls
If c.GetType() Is GetType(VScrollBar) Then
Dim vbar as VScrollBar= DirectCast(c, VScrollBar)
If vbar.Visible = True Then
'Do whatever you like
End If
End If
Next
回答by LarsTech
Here is the VB.Net version of raising an event when the scrollbar visibility changes, from How to detect the vertical scrollbar in a DataGridView control
这是在滚动条可见性发生变化时引发事件的 VB.Net 版本,来自How to detection the vertical scrollbar in a DataGridView control
Public Class MyGrid
Inherits DataGridView
Public Event ScrollbarVisibleChanged As EventHandler
Public Sub New()
AddHandler Me.HorizontalScrollBar.VisibleChanged, _
AddressOf HorizontalScrollBar_VisibleChanged
End Sub
Public ReadOnly Property HorizontalScrollbarVisible() As Boolean
Get
Return HorizontalScrollBar.Visible
End Get
End Property
Private Sub HorizontalScrollBar_VisibleChanged(sender As Object, e As EventArgs)
RaiseEvent ScrollbarVisibleChanged(Me, EventArgs.Empty)
End Sub
End Class
回答by Rose
I took Nianios example, made a few adjustments and arrived at way to determine if the scroll bar is visible. Thanks!
我以 Nianios 为例,进行了一些调整,并找到了确定滚动条是否可见的方法。谢谢!
Private Function HScrollBarVisible() As Boolean
Dim ctrl As New Control
For Each ctrl In DataGridView1.Controls
If ctrl.GetType() Is GetType(HScrollBar) Then
If ctrl.Visible = True Then
Return True
Else
Return False
End If
End If
Next
Return Nothing
End Function

