[RESOLVED] Thanks and scrollbars
First of a big thanks to everyone on here, I only popped here a while ago to just ask a question, but have stayed, and now my project is better than I could have done without you guys :)
There doesn't seem to be any VB question that someone doesn't know the answer to...and is willing to help. :bigyello:
One little question on textbox scrollbars, is there an easy way to get them to disappear when not needed, rather than just going lowlighted?
Re: Thanks and scrollbars
I don't know if there is any way to 'properly' show/hide scrollbars of a normal textbox at runtime.
This code shows an alternate way to hide scrollbars in a Webbrowser control by using a picturebox container. I guess with very little modification it will work with normal textboxes too.
Re: Thanks and scrollbars
I just did this, using this code by Martinliss to show/hide scrollbars.
I used a textbox named text1.
In design mode, Set Multiline property = true and Scrollbars property = Both
VB Code:
Option Explicit
Private Declare Function ShowScrollBar Lib "user32" _
(ByVal hWnd As Long, ByVal wBar As Long, _
ByVal bShow As Long) As Long
Private Const SB_HORZ = 0
Private Const SB_VERT = 1
Private Const SB_HWND = 2
Private Const SB_BOTH = 3
Private Const EM_GETLINECOUNT = &HBA
Private Declare Function SendMessageAsLong Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam _
As Long, ByVal lParam As Long) As Long
Private Sub Form_Load()
CheckScrollBars
End Sub
Private Sub Text1_Change()
CheckScrollBars
End Sub
Private Sub CheckScrollBars()
Dim lngLineCount As Long
Static VVisible As Boolean
Static HVisible As Boolean
Dim lWidth As Long
Dim lHeight As Long
Dim i As Integer
lngLineCount = SendMessageAsLong(Text1.hWnd, EM_GETLINECOUNT, 0, 0)
For i = 1 To 2 '2 times, because if HScrollbar appeared, VScrollbar code will be different
'If Horizontal ScrollBar is present, Height = Text1.Height - [Height of the Hscrollbar]
If HVisible Then lHeight = Text1.Height - 250 Else lHeight = Text1.Height - 150
If Me.TextHeight("A") * lngLineCount > lHeight Then
ShowScrollBar Text1.hWnd, SB_VERT, True 'Higher than Height?
VVisible = True 'Make VScrollbar visible
Else
ShowScrollBar Text1.hWnd, SB_VERT, False 'Hide VScrollbar
VVisible = False
End If
'If Vertical ScrollBar is present width = Text1.width - [width of the Vscrollbar]
If VVisible Then lWidth = Text1.Width - 350 Else lWidth = Text1.Width - 100
If Me.TextWidth(Text1.Text) > lWidth Then 'Wider than width?
ShowScrollBar Text1.hWnd, SB_HORZ, True 'Make HScrollbar visible
HVisible = True
Else
ShowScrollBar Text1.hWnd, SB_HORZ, False 'Hide HScrollbar
HVisible = False
End If
Next
End Sub
Re: Thanks and scrollbars
Ah ! I didn't figured out that we need to put that code in Text_Change too !
Jcis, your code is running perfectly here. :thumb:
Edit: Here is a little modified version of jcis's code that works on SingleLine textboxes.
Re: Thanks and scrollbars
Although it doesn't work on form_resize.
I have a text box which just displays data (.locked=true), and the code just removes the scrollbars.
On loading if the text to too long for the box it desn't wrap the text, and the same is true on the resize. :(
Re: Thanks and scrollbars
I tryied with a locked textbox and its working fine.
You are resizing the textbox in form_resize event or something like that?
Quote:
Originally Posted by eusty
On loading if the text to too long for the box it desn't wrap the text..
You want it to wrap the text? So why you use scrollbars then?
Or maybe you want to use just the vertical scrollbar?
Re: Thanks and scrollbars
Yes I just want the vertical scrollbar, if it's needed.
I'm using the form_resize control to change the size of the whole form, including the textbox.
With the scrollbar code added, when you resize the text just 'disappears' under the form rather than the vertical scrollbar appearing.
The form_resize calls the scrollbar code and it runs it, but all values returned are false so it doesn't add them.
Re: Thanks and scrollbars
Just needed a new day and another look at the code :blush: