c# identifying scrollbar on control
How can I check if a scrollbar is present on a datagridview in Winforms.
Something like (pseudocode)
开发者_运维技巧If(datagridview.verticalscrollbar = true)
do something......
Thanks.
There's no property or method specifically for this purpose, but you can tell if the vertical scrollbar is showing by performing the following check:
bool scrolling = (dataGridView.DisplayedRowCount(false) != dataGridView.Rows.Count);
(It compares the total number of rows with the number currently being displayed; if they don't match, a scrollbar will be visible.)
In a Datagridview there are special controls for the scrollbars - not like in an usual panel.
You can do sth. like
private VScrollBar _verticalScrollBar;
private HScrollBar _horizontalScrollBar;
foreach (Control c in _dataGridView.Controls)
{
if (c is VScrollBar)
{
_verticalScrollBar = c as VScrollBar;
if (_horizontalScrollBar!=null)
{
break;
}
}
if (c is HScrollBar)
{
_horizontalScrollBar = c as HScrollBar;
if (_verticalScrollBar != null)
{
break;
}
}
}
and test if the scrollbar is visible with
if(_verticalScrollBar.Visible==true){
//do something you want when scrollbar is visible
}
else
{
//do something when scrollbar is invisible
}
精彩评论