July 1, 1995
The Text Box control in Microsoft® Visual Basic® is a mini-word-processing program that lets your user type either single or multiple lines of text. At design time, you set the MultiLine property either to False (single) or to True (multiline). This property cannot be changed at run time. However, during run time you can use Scroll Bar controls to give the impression that your Text Box can be made single or multiline at run time, regardless of the MultiLine property setting.
When designing a Microsoft® Visual Basic® application, you can set the MultiLine property of a Text Box to True or False. If this property is set to False, only a single line of text can be typed in the Text Box control. If the MultiLine property is set to True, many lines of text can be typed in the control. In addition, if the ScrollBars property is set to 3-Both, you can scroll through the text both vertically and horizontally.
There's only one problem—the MultiLine property cannot be dynamically switched at run time, which means that the Text Box is set to what it was in the design phase of the program.
However, by using the Microsoft Windows® application programming interface (API) SetScrollRange function, you can add code to your Visual Basic application that will allow you to create a work-around solution. This enables you to change the Text Box's appearance from single to multiline at run time.
The SetScrollRange function lets you set the minimum and maximum indicator positions of a scroll bar. To use this function, add the following Declare statement to the General Declarations section of your form (note that the Declare statement must be typed as a single line of code):
Private Declare Sub SetScrollRange Lib "User" (ByVal hWnd As Integer, ByVal nBar
As Integer, ByVal nMinPos As Integer, ByVal nMaxPos As Integer, ByVal
bRedraw As Integer)
The SetScrollRange function requires five arguments, as follows.
hWnd | An integer value containing the window or scroll bar's handle | |
nBar | An integer value set to one of the following values: | |
ESB_ENABLE_BOTH | Both arrows enabled | |
ESB_DISABLE_LTUP | Left or Up arrow disabled | |
ESB_DISABLE_RTDN | Right or Down arrow disabled | |
ESB_DISABLE_BOTH | Both arrows disabled | |
nMinPos | An integer value containing the minimum indicator position | |
nMaxPos | An integer value containing the maximum indicator position | |
bRedraw | An integer value, when set to True, to redraw the scroll bar |
To disable the scroll bars in this Visual Basic program, you simply call SetScrollRange with the minimum and maximum position indicators set to the same value. When you want to enable the scroll bars again, you call SetScrollRange with the minimum and maximum position indicators set to 1 and 100, respectively. When you change the position indictors to 1 and 100, you can scroll through the contents of the Text Box control regardless of the MultiLine property setting. This gives the impression that the Text Box control is MultiLine when it is indeed set to single-line status.
This program shows how to create a Text Box control that can be switched at run time from single-line to multiline status, with or without scroll bars.
Private Declare Sub SetScrollRange Lib "User" (ByVal hWnd As Integer,
ByVal nBar As Integer, ByVal nMinPos As Integer,
ByVal nMaxPos As Integer, ByVal bRedraw As Integer)
Const ESB_DISABLE_BOTH = 3
Const ESB_ENABLE_BOTH = 1
Private Sub Command1_Click()
Dim hWnd As Integer
Dim Min As Integer
Dim Max As Integer
Min = 1
Max = Min
Call SetScrollRange(Text1.hWnd, ESB_DISABLE_BOTH, Min, Max, 1)
End Sub
Private Sub Command2_Click()
Dim hWnd As Integer
Dim Min As Integer
Dim Max As Integer
Min = 1
Max = 100
Call SetScrollRange(Text1.hWnd, ESB_ENABLE_BOTH, Min, Max, 1)
End Sub
Run the example program by pressing F5. Click the Text Box control and type several lines of text. Notice that you can use the scroll bars to scroll through the text in the control. Click Disable. Now you cannot use the scroll bars to scroll through the Text Box control. To enable the scroll bars again, click Enable.
"SetScrollRange." (MSDN Library Archive, Product Documentation, SDKs, Windows 3.1 SDK, Programmer's Reference Volume 2: Functions)
"Scroll Bar Range and Position." (MSDN Library Archive, Books and Periodicals, Programming Windows 3.1 by Charles Petzold, PART 1 Getting Started)