Scrollbar position - it can't be determined !

C

Colin McGuire

Hello experts, this is a repost but I have been (much) more clear.

I want to know the position of the horizontal scrollbar in a textbox
as a percentage

- if the horizontal scrollbar is hard left,
I want to display 0% on the form.
- if the horizontal scrollbar is smack in the middle,
I want to display 50% on the form.
- if the horizontal scrollbar is 2/3rd along the X-axis,
I want to display 66% on the form.
- if the horozontal scrollbar is hard right,
I want to display 100% on the form.

etc.

I can't figure out how to do it and, currently, think it can't be
done.

These are the steps I have used to create my application.
1. Launch Visual Studio 2003 and create a new project
"Form1" will show.
2. Move the cursor to the toolbox, select a textbox, and
draw one on the form.
3. Go to the properties and set "Scrollbars" to "Horizontal",
"WordWrap" to "False", and "MultiLine" to "True"
4. Type in a sentence "the quick brown fox jumps over the
lazy dog..". Eventually the horizontal scrollbar will
appear.


Now how on earth do I get the current scrollbar position, and the
maximum position, to determine the percentage
(currentScrollbarPositionX/maxScrollbarPositionX)*100/1 ?

When I Google search microsoft.public.dotnet.languages.vb, no luck.
I'd rather have someone post "It can't be done" rather than the thread
sit there with one entry, mine :( So, can it be done?

Thanks, a desperate Colin !
 
B

Brian

'-- Get String width
Dim sz as SizeF = e.Graphics.MeasureString(TextBox1.Text,TextBox.Font)

'-- Then divide
Textbox1.width / sz.width
 
F

fred

No - this is not what he wants,. Th only returns a constant for a certain
text in a textbox - if the HScrollbar is half way across a long line, same
constant, if the HScrollbar is hard left to use his words, same constant.

Colin I don't have an answer - gut feeling is that you should subclass the
textbox and play around with the properties AutoScrollPosition and Location.

Here to help and be helped.
Frederic Marè
 
O

One Handed Man

I think you could do some experiementation and find the empirical data you
need.

Try filling a combo box with 10, 100, 200 and 400 strings. Thenwork out how
many clicks it takes to reach the end. This way you could get a round
percentage if it is a linear function.

OHM
 
O

One Handed Man

Sorry, I was thinking vertical.
I think you could do some experiementation and find the empirical
data you need.

Try filling a combo box with 10, 100, 200 and 400 strings. Thenwork
out how many clicks it takes to reach the end. This way you could get
a round percentage if it is a linear function.

OHM
 
B

Brian

Sorry for my first bone headed response. This is what you need. You have to
inherit a text box and the override the wndProc.
Here's the code that you need. Please note that the max position and the
thumb position won't be the same at the end.
This is what happened (when i tried it) when the scroll bar is at the start
and at very end.

Example: Begin Scroll min = 0 End Scroll min =0
max=184
max = 184
npos =0
npos =111 ' Diff of page 184-111 = 73 (close enough)
page =74
page = 74

Imports System.Runtime.InteropServices.Marshal

Public Class UserControl1
Inherits System.Windows.Forms.TextBox


'-- Declare
Private Declare Function GetScrollInfo Lib "user32" (ByVal hWnd As
IntPtr, ByVal n As Integer, ByRef lpScrollInfo As SCROLLINFO) As Integer

'-- Const
Private Const WM_HSCROLL = &H114 '276
Private Const SB_HORZ = 0
Private Const SIF_TRACKPOS = &H10
Private Const SIF_RANGE = &H1
Private Const SIF_POS = &H4
Private Const SB_THUMBTRACK = 5
Private Const SIF_PAGE = &H2
Private Const SIF_ALL = SIF_RANGE Or SIF_PAGE Or SIF_POS Or SIF_TRACKPOS

'-- Info Struct
Private Structure SCROLLINFO
Public cbSize As Integer
Public fMask As Integer
Public nMin As Integer
Public nMax As Integer
Public nPage As Integer
Public nPos As Integer
Public nTrackPos As Integer
End Structure

'-- Variable
Dim i As New SCROLLINFO()


'-- Override the windows procedure
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

'-- If scrolling ---------
If m.Msg = WM_HSCROLL Then

'-- Set Mask to all
i.fMask = SIF_ALL

'-- Set size of structure
i.cbSize = SizeOf(i)

'-- Get the info
GetScrollInfo(m.HWnd, 0, i)

'-- Min Value
Debug.WriteLine(i.nMin.ToString)

'-- Max Value
Debug.WriteLine(i.nMax.ToString)

'-- Page Info
Debug.WriteLine(i.nPage.ToString)

'-- Position
Debug.WriteLine(i.nPos)

End If

'-- Call Base
MyBase.WndProc(m)

End Sub


End Class
 

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