Detect if vertical scrollbar currently shows on the control

M

MuZZy

Hi,

I was wondering if anyone could help me on this: i need to know if a
particular Control currently shows a vertical scroll bar. I need some
generic way as that Control can be of any type (grid, textbox, listbox,
checkedlistbox, etc.). I didn't find a way to do that with .NET so i
would assume there might be a WinAPI function to invoke.

Basically i need to place another control in the top right corner of
that control and i need to know if i put it in the corner or offset to
the left by the width of the scroll bar.

Any ideas?

Thank you in advance,
Andrey
 
L

Linda Liu [MSFT]

Hi Andrey,

From your post, my understanding on this issue is that you need some
generic way to detect if vertical scrollbar currently shows on the control,
which can be of any type, for example, TextBox, ListBox, CheckedListBox and
etc. You also mentioned that you need to place another control in the top
right corner of that control.

Do you mean that you'd like to place a control in i.e a textbox or listbox?
Generally speaking, we place a control in another control which could be
taken as a container, such as a Panel or GroupBox.

If you mean placing a control in a textbox or listbox and would like to
detect if vertical scrollbar currently shows, I think the easiest way is to
set the ScrollBars property of the textbox to Vertical or the
ScrollAlwaysVisible property of the listbox to True to make the vertical
scrollbar always visible on the textbox and listbox.

If you mean placing a control in another control which could acts as a
container, we should research on the class ScrollableControl which is the
base class for controls that supports auto-scrolling behavior. The
ScrollableControl has a VerticalScroll property which gets the
characteristics associated with the vertical scrollbar. Since this property
is of ScrollableControl class, using this property is a generic way for us
to determine if the vertical scrollbar currently shows on the control. The
following is a sample.

Panel panel1 = new Panel();
bool verticalScrollbarVisible = panel1.VerticalScroll.Visible;

Hope this helps.
If you have any concerns, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Claes Bergefall

The GetScrollBarInfo API method might be useful for this. Not sure if it is
generic enough though.

/claes
 
L

Linda Liu [MSFT]

Hi Andrey,

I agree with Claes's opinion. The GetScrollBarInfo API function is a
generic method to get the scrollbar information of a control.

The following is a sample of using the GetScrollBarInfo function. This
requires you to add a Panel named panel1 and a Button named button1 on the
form.

using System.Runtime.InteropServices;
using System.Drawing;

public partial class Form1 : Form
{
const uint CONST_OBJID_VSCROLL = 0xFFFFFFFB;
const uint CONST_OBJID_HSCROLL = 0xFFFFFFFA;

const int STATE_SYSTEM_UNAVAILABLE = 0x00000001;
const int STATE_SYSTEM_PRESSED = 0x00000008;
const int STATE_SYSTEM_INVISIBLE = 0x00008000;
const int STATE_SYSTEM_OFFSCREEN = 0x00010000;

//API Structires.
[StructLayout(LayoutKind.Sequential)]
public struct SCROLLBARINFO
{
public int cbSize;
public Rectangle rcScrollBar;
public int dxyLineButton;
public int xyThumbTop;
public int xyThumbBottom;
public int reserved;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public int[] rgstate;
}

//API Calls.
[DllImport("user32.dll", EntryPoint = "GetScrollBarInfo")]
public static extern int GetScrollBarInfo(IntPtr hWnd, uint
idObject, ref SCROLLBARINFO psbi);

private void button1_Click(object sender, EventArgs e)
{
//bool visible = this.panel1.VerticalScroll.Visible;

SCROLLBARINFO holdScroll = new SCROLLBARINFO();
holdScroll.cbSize = Marshal.SizeOf(holdScroll);
int returnValue = GetScrollBarInfo(this.panel1.Handle,
CONST_OBJID_VSCROLL, ref holdScroll);
int result = holdScroll.rgstate[0];
if (result == STATE_SYSTEM_INVISIBLE)
{
MessageBox.Show("The vertical scroll bar is invisible!");
}
else
{
MessageBox.Show("The vertical scroll bar is visible!");
}
}
}

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top