How to detect the vertical scrollbar in a DataGridView control

后端 未结 7 742
半阙折子戏
半阙折子戏 2021-01-05 05:19

Using winforms in vs2008. I have a DataGridView and I would like to detect when the vertical scroll bar is visible. What event should I register for?

I am adding the

7条回答
  •  粉色の甜心
    2021-01-05 05:56

    Instead of using Linq (Adam Butler) you can just iterate through the controls and sign up an event handler that will be called each time the scrollbar visibility changes. I implemented it that way and it works pretty smoothly:

    private System.Windows.Forms.DataGridView dgCounterValues;
    private Int32 _DataGridViewScrollbarWidth;
    
    // get vertical scrollbar visibility handler
    foreach (Control c in dgCounterValues.Controls)
    if (c.GetType().ToString().Contains("VScrollBar"))
    {
        c.VisibleChanged += c_VisibleChanged;
    }
    

    do this somewhere after the InitializeComponent() In the handler, do whatever you need to do in response to the visibility change of the vertical scrollbar. Same works for the horizontal scrollbar (replace VScrollBar with HScrollBar):

    void c_VisibleChanged(object sender, EventArgs e)
    {
        VScrollBar vb = sender as VScrollBar;
        if (vb.Visible) _DataGridViewScrollbarWidth = vb.Width;
        else _DataGridViewScrollbarWidth = 0;
    } 
    

提交回复
热议问题