By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,191 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

ScrollableControl Problems

P: n/a
Hello,

I'm trying to create my own control derived from ScrollableControl
public partial class qViewer : ScrollableControl{
public qViewer() {
VScroll = true;
AutoScroll = true;
}
...
}

I override the OnPaint method with my own code,
however the scrollbars never shows though my drawings exceed the Control
size.

Thanks In Advance
Yehia A.Salm

Sep 26 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Yehia,

ScrollableControl ensures that scroll bars will be visible if nested controls exceed the visible client area. It has nothing to do
with what is drawn on the control.

If you want to have scroll bars appear when your drawing exceeds the size of the visible client area, then you can use two ScrollBar
Controls. Use one as the horizontal bar and one as the vertical bar. Calculate the appropriate size and position of each scroll
box from the size of your Control's client area (minus the size of the scroll bars) and the size of your drawing.

You'll have to handle the Resize event as well if you want your Control to function properly.

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message news:AA**********************************@microsof t.com...
Hello,

I'm trying to create my own control derived from ScrollableControl
public partial class qViewer : ScrollableControl{
public qViewer() {
VScroll = true;
AutoScroll = true;
}
...
}

I override the OnPaint method with my own code,
however the scrollbars never shows though my drawings exceed the Control size.

Thanks In Advance
Yehia A.Salm

Sep 26 '06 #2

P: n/a
but I always used to do it by the WS_VSCROLL style and then trapping the
WM_VSCROLL message in my old c++, but now it's really hard to accomplish
with the c# api limitation(no limitations here but rather hard to accomplish
requires more lines) , should I used the supplied VScrollBar control, is
there any limitation using this control, and what's the difference between
using the two methods?

>ScrollableControl ensures that scroll bars will be visible if nested
controls exceed the visible client area. It has nothing to do
with what is drawn on the control.

If you want to have scroll bars appear when your drawing exceeds the size
of the visible client area, then you can use two ScrollBar Controls. Use
one as the horizontal bar and one as the vertical bar. Calculate the
appropriate size and position of each scroll box from the size of your
Control's client area (minus the size of the scroll bars) and the size of
your drawing.

You'll have to handle the Resize event as well if you want your Control to
function properly.

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:AA**********************************@microsof t.com...
>Hello,

I'm trying to create my own control derived from ScrollableControl
public partial class qViewer : ScrollableControl{
public qViewer() {
VScroll = true;
AutoScroll = true;
}
...
}

I override the OnPaint method with my own code,
however the scrollbars never shows though my drawings exceed the Control
size.

Thanks In Advance
Yehia A.Salm

Sep 29 '06 #3

P: n/a
Hi Yehia,

You won't receive a WS_VSCROLL message unless you have scrollbars but they won't appear unless you have Controls that are partially
or completely outside of the ScrollableControl's visible area. In other words, you won't be able to accomplish this using the
scroll messages unless you add your own scrollbars as I suggested.

There might be an easier way that you can accomplish this but I haven't tested it myself. Please let me know if this works for you:

1. Derive from Control and perform all your drawing operations as necessary in an OnPaint method override (this will be your drawing
surface). Make sure that the Control's ClientRectangle is always equal to the calculated bounding rectangle of your drawing so that
the surface of the Control will not clip the drawing at all (i.e. the Control's size should never be smaller than your drawing's
size). You can handle the Resize event to prevent the Control from being resized smaller than your drawing's bounding rectangle.
Any time the size of your drawing must change, recalculate the size of the bounding rectangle and set the Control's size
accordingly.

2. Derive another class from Panel or ScrollableControl and add the previous Control as a private, nested class within its
declaration.
3. In the constructor of your second control add a new instance of your nested Control to the Controls collection and set its
location to {0, 0}.
4. Optionally, you can create a subclassed ControlCollection for your second control that prevents other controls from being added.
(Override CreateControlCollection method.)
5. Set the AutoScroll and other scroll properties of the second control (the ScrollableControl container) as you desire.

The trick here would be ensuring that the drawing surface control is always the size of the drawing. The container should
automatically handle the scrollbars for you.

HTH

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message news:7C**********************************@microsof t.com...
but I always used to do it by the WS_VSCROLL style and then trapping the WM_VSCROLL message in my old c++, but now it's really
hard to accomplish with the c# api limitation(no limitations here but rather hard to accomplish requires more lines) , should I
used the supplied VScrollBar control, is there any limitation using this control, and what's the difference between using the two
methods?

>>ScrollableControl ensures that scroll bars will be visible if nested controls exceed the visible client area. It has nothing to
do
with what is drawn on the control.

If you want to have scroll bars appear when your drawing exceeds the size of the visible client area, then you can use two
ScrollBar Controls. Use one as the horizontal bar and one as the vertical bar. Calculate the appropriate size and position of
each scroll box from the size of your Control's client area (minus the size of the scroll bars) and the size of your drawing.

You'll have to handle the Resize event as well if you want your Control to function properly.

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message news:AA**********************************@microsof t.com...
>>Hello,

I'm trying to create my own control derived from ScrollableControl
public partial class qViewer : ScrollableControl{
public qViewer() {
VScroll = true;
AutoScroll = true;
}
...
}

I override the OnPaint method with my own code,
however the scrollbars never shows though my drawings exceed the Control size.

Thanks In Advance
Yehia A.Salm


Sep 29 '06 #4

P: n/a
You won't receive a WS_VSCROLL message unless you have scrollbars but they
won't appear unless you have Controls that are partially or completely
outside of the ScrollableControl's visible area. In other words, you
won't be able to accomplish this using the scroll messages unless you add
your own scrollbars as I suggested.
I am not sure that my question was clear, WS_VSCROLL is a parameter used in
the CreateWindowEx used to give a control scrollbars with no hwnd and then I
can receive the WM_VSCROLL message normally, but this seems not encouraged
in c# with all the api limitation so I was just asking if there any
difference between the using the vscroll control or the api(except the
hwnd)?

<DIV>&quot;Dave Sexton&quot; &lt;dave@jwa[remove.this]online.com&gt; wrote
in message news:ec**************@TK2MSFTNGP05.phx.gbl...</DIV>Hi Yehia,
>
You won't receive a WS_VSCROLL message unless you have scrollbars but they
won't appear unless you have Controls that are partially or completely
outside of the ScrollableControl's visible area. In other words, you
won't be able to accomplish this using the scroll messages unless you add
your own scrollbars as I suggested.

There might be an easier way that you can accomplish this but I haven't
tested it myself. Please let me know if this works for you:

1. Derive from Control and perform all your drawing operations as
necessary in an OnPaint method override (this will be your drawing
surface). Make sure that the Control's ClientRectangle is always equal to
the calculated bounding rectangle of your drawing so that the surface of
the Control will not clip the drawing at all (i.e. the Control's size
should never be smaller than your drawing's size). You can handle the
Resize event to prevent the Control from being resized smaller than your
drawing's bounding rectangle. Any time the size of your drawing must
change, recalculate the size of the bounding rectangle and set the
Control's size accordingly.

2. Derive another class from Panel or ScrollableControl and add the
previous Control as a private, nested class within its declaration.
3. In the constructor of your second control add a new instance of your
nested Control to the Controls collection and set its location to {0, 0}.
4. Optionally, you can create a subclassed ControlCollection for your
second control that prevents other controls from being added. (Override
CreateControlCollection method.)
5. Set the AutoScroll and other scroll properties of the second control
(the ScrollableControl container) as you desire.

The trick here would be ensuring that the drawing surface control is
always the size of the drawing. The container should automatically handle
the scrollbars for you.

HTH

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:7C**********************************@microsof t.com...
>but I always used to do it by the WS_VSCROLL style and then trapping
the WM_VSCROLL message in my old c++, but now it's really hard to
accomplish with the c# api limitation(no limitations here but rather hard
to accomplish requires more lines) , should I used the supplied
VScrollBar control, is there any limitation using this control, and
what's the difference between using the two methods?

>>>ScrollableControl ensures that scroll bars will be visible if nested
controls exceed the visible client area. It has nothing to do
with what is drawn on the control.

If you want to have scroll bars appear when your drawing exceeds the
size of the visible client area, then you can use two ScrollBar
Controls. Use one as the horizontal bar and one as the vertical bar.
Calculate the appropriate size and position of each scroll box from the
size of your Control's client area (minus the size of the scroll bars)
and the size of your drawing.

You'll have to handle the Resize event as well if you want your Control
to function properly.

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:AA**********************************@micros oft.com...
Hello,

I'm trying to create my own control derived from ScrollableControl
public partial class qViewer : ScrollableControl{
public qViewer() {
VScroll = true;
AutoScroll = true;
}
...
}

I override the OnPaint method with my own code,
however the scrollbars never shows though my drawings exceed the
Control size.

Thanks In Advance
Yehia A.Salm


Sep 30 '06 #5

P: n/a
Hi Yehia,
I am not sure that my question was clear,
I mistakenly referred to WS_ instead of WM_ in my response. I guess my answer wasn't that clear either ;)

<snip>
but this seems not encouraged in c# with all the api limitation
I don't know of the limitations to which you are referring. I don't think there is any reason why you couldn't force scrollbars
using P/Invoke but it might not be recommended due to the fact that the managed control won't be "aware" of such a change. IMO,
it's perfectly reasonable for you to do so, if you're prepared to write the required functionality as well. Maybe someone else
would like to give some of their insight on this topic.
so I was just asking if there any difference between the using the vscroll control or the api(except the hwnd)?
For one thing, the VScrollBar control is managed and provides events and other ways to control and interact with the scroll bar so
that you won't have to respond to Windows messages. It doesn't make much sense to use the API directly unless you can't get what
you need from the managed control, which is doubtful. (I believe the VScrollBar class is just a managed wrapper for an unmanaged
vscroll class, IIRC).

Another thing is that since VScrollBar is a control you can place it anywhere you'd like, including outside of the control that it's
scrolling. You can't do that easily with the API, IMO.

Forcing scrollbars won't help you (via WS_VSCROLL) unless you're prepared to code all of the functionality yourself. I tried to
give you another solution that I had just thought of, so you wouldn't have to deal with messages and scrollbars in code. I know
that it didn't answer your question exactly, but I was just trying to help you avoid some extra work :)

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message news:A7**********************************@microsof t.com...
>You won't receive a WS_VSCROLL message unless you have scrollbars but they won't appear unless you have Controls that are
partially or completely outside of the ScrollableControl's visible area. In other words, you won't be able to accomplish this
using the scroll messages unless you add your own scrollbars as I suggested.

I am not sure that my question was clear, WS_VSCROLL is a parameter used in the CreateWindowEx used to give a control scrollbars
with no hwnd and then I can receive the WM_VSCROLL message normally, but this seems not encouraged in c# with all the api
limitation so I was just asking if there any difference between the using the vscroll control or the api(except the hwnd)?

<DIV>&quot;Dave Sexton&quot; &lt;dave@jwa[remove.this]online.com&gt; wrote in message
news:ec**************@TK2MSFTNGP05.phx.gbl...</DIV>Hi Yehia,
>>
You won't receive a WS_VSCROLL message unless you have scrollbars but they won't appear unless you have Controls that are
partially or completely outside of the ScrollableControl's visible area. In other words, you won't be able to accomplish this
using the scroll messages unless you add your own scrollbars as I suggested.

There might be an easier way that you can accomplish this but I haven't tested it myself. Please let me know if this works for
you:

1. Derive from Control and perform all your drawing operations as necessary in an OnPaint method override (this will be your
drawing surface). Make sure that the Control's ClientRectangle is always equal to the calculated bounding rectangle of your
drawing so that the surface of the Control will not clip the drawing at all (i.e. the Control's size should never be smaller than
your drawing's size). You can handle the Resize event to prevent the Control from being resized smaller than your drawing's
bounding rectangle. Any time the size of your drawing must change, recalculate the size of the bounding rectangle and set the
Control's size accordingly.

2. Derive another class from Panel or ScrollableControl and add the previous Control as a private, nested class within its
declaration.
3. In the constructor of your second control add a new instance of your nested Control to the Controls collection and set its
location to {0, 0}.
4. Optionally, you can create a subclassed ControlCollection for your second control that prevents other controls from being
added. (Override CreateControlCollection method.)
5. Set the AutoScroll and other scroll properties of the second control (the ScrollableControl container) as you desire.

The trick here would be ensuring that the drawing surface control is always the size of the drawing. The container should
automatically handle the scrollbars for you.

HTH

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message news:7C**********************************@microsof t.com...
>>but I always used to do it by the WS_VSCROLL style and then trapping the WM_VSCROLL message in my old c++, but now it's really
hard to accomplish with the c# api limitation(no limitations here but rather hard to accomplish requires more lines) , should I
used the supplied VScrollBar control, is there any limitation using this control, and what's the difference between using the
two methods?
ScrollableControl ensures that scroll bars will be visible if nested controls exceed the visible client area. It has nothing
to do
with what is drawn on the control.

If you want to have scroll bars appear when your drawing exceeds the size of the visible client area, then you can use two
ScrollBar Controls. Use one as the horizontal bar and one as the vertical bar. Calculate the appropriate size and position of
each scroll box from the size of your Control's client area (minus the size of the scroll bars) and the size of your drawing.

You'll have to handle the Resize event as well if you want your Control to function properly.

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message news:AA**********************************@microsof t.com...
Hello,
>
I'm trying to create my own control derived from ScrollableControl
public partial class qViewer : ScrollableControl{
public qViewer() {
VScroll = true;
AutoScroll = true;
}
...
}
>
I override the OnPaint method with my own code,
however the scrollbars never shows though my drawings exceed the Control size.
>
Thanks In Advance
Yehia A.Salm



Sep 30 '06 #6

P: n/a
yea u're right no pointing in reinventing the wheel, I'll just use the
VScrollBar control, Thanks for the answer

<DIV>&quot;Dave Sexton&quot; &lt;dave@jwa[remove.this]online.com&gt; wrote
in message news:%2******************@TK2MSFTNGP05.phx.gbl...</DIV>Hi
Yehia,
>
>I am not sure that my question was clear,

I mistakenly referred to WS_ instead of WM_ in my response. I guess my
answer wasn't that clear either ;)

<snip>
>but this seems not encouraged in c# with all the api limitation

I don't know of the limitations to which you are referring. I don't think
there is any reason why you couldn't force scrollbars using P/Invoke but
it might not be recommended due to the fact that the managed control won't
be "aware" of such a change. IMO, it's perfectly reasonable for you to do
so, if you're prepared to write the required functionality as well. Maybe
someone else would like to give some of their insight on this topic.
>so I was just asking if there any difference between the using the
vscroll control or the api(except the hwnd)?

For one thing, the VScrollBar control is managed and provides events and
other ways to control and interact with the scroll bar so that you won't
have to respond to Windows messages. It doesn't make much sense to use
the API directly unless you can't get what you need from the managed
control, which is doubtful. (I believe the VScrollBar class is just a
managed wrapper for an unmanaged vscroll class, IIRC).

Another thing is that since VScrollBar is a control you can place it
anywhere you'd like, including outside of the control that it's scrolling.
You can't do that easily with the API, IMO.

Forcing scrollbars won't help you (via WS_VSCROLL) unless you're prepared
to code all of the functionality yourself. I tried to give you another
solution that I had just thought of, so you wouldn't have to deal with
messages and scrollbars in code. I know that it didn't answer your
question exactly, but I was just trying to help you avoid some extra work
:)

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:A7**********************************@microsof t.com...
>>You won't receive a WS_VSCROLL message unless you have scrollbars but
they won't appear unless you have Controls that are partially or
completely outside of the ScrollableControl's visible area. In other
words, you won't be able to accomplish this using the scroll messages
unless you add your own scrollbars as I suggested.

I am not sure that my question was clear, WS_VSCROLL is a parameter used
in the CreateWindowEx used to give a control scrollbars with no hwnd and
then I can receive the WM_VSCROLL message normally, but this seems not
encouraged in c# with all the api limitation so I was just asking if
there any difference between the using the vscroll control or the
api(except the hwnd)?

<DIV>&quot;Dave Sexton&quot; &lt;dave@jwa[remove.this]online.com&gt;
wrote in message news:ec**************@TK2MSFTNGP05.phx.gbl...</DIV>Hi
Yehia,
>>>
You won't receive a WS_VSCROLL message unless you have scrollbars but
they won't appear unless you have Controls that are partially or
completely outside of the ScrollableControl's visible area. In other
words, you won't be able to accomplish this using the scroll messages
unless you add your own scrollbars as I suggested.

There might be an easier way that you can accomplish this but I haven't
tested it myself. Please let me know if this works for you:

1. Derive from Control and perform all your drawing operations as
necessary in an OnPaint method override (this will be your drawing
surface). Make sure that the Control's ClientRectangle is always equal
to the calculated bounding rectangle of your drawing so that the surface
of the Control will not clip the drawing at all (i.e. the Control's size
should never be smaller than your drawing's size). You can handle the
Resize event to prevent the Control from being resized smaller than your
drawing's bounding rectangle. Any time the size of your drawing must
change, recalculate the size of the bounding rectangle and set the
Control's size accordingly.

2. Derive another class from Panel or ScrollableControl and add the
previous Control as a private, nested class within its declaration.
3. In the constructor of your second control add a new instance of your
nested Control to the Controls collection and set its location to {0,
0}.
4. Optionally, you can create a subclassed ControlCollection for your
second control that prevents other controls from being added. (Override
CreateControlCollection method.)
5. Set the AutoScroll and other scroll properties of the second control
(the ScrollableControl container) as you desire.

The trick here would be ensuring that the drawing surface control is
always the size of the drawing. The container should automatically
handle the scrollbars for you.

HTH

--
Dave Sexton

"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:7C**********************************@micros oft.com...
but I always used to do it by the WS_VSCROLL style and then trapping
the WM_VSCROLL message in my old c++, but now it's really hard to
accomplish with the c# api limitation(no limitations here but rather
hard to accomplish requires more lines) , should I used the supplied
VScrollBar control, is there any limitation using this control, and
what's the difference between using the two methods?
>ScrollableControl ensures that scroll bars will be visible if nested
>controls exceed the visible client area. It has nothing to do
with what is drawn on the control.
>
If you want to have scroll bars appear when your drawing exceeds the
size of the visible client area, then you can use two ScrollBar
Controls. Use one as the horizontal bar and one as the vertical bar.
Calculate the appropriate size and position of each scroll box from
the size of your Control's client area (minus the size of the scroll
bars) and the size of your drawing.
>
You'll have to handle the Resize event as well if you want your
Control to function properly.
>
--
Dave Sexton
>
"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:AA**********************************@micr osoft.com...
>Hello,
>>
>I'm trying to create my own control derived from ScrollableControl
>public partial class qViewer : ScrollableControl{
> public qViewer() {
> VScroll = true;
> AutoScroll = true;
> }
> ...
>}
>>
>I override the OnPaint method with my own code,
>however the scrollbars never shows though my drawings exceed the
>Control size.
>>
>Thanks In Advance
>Yehia A.Salm
>
>


Sep 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.