Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Resizing the control in Runtime !?

Posted on 1999-07-27
Medium Priority
Last Modified: 2010-04-30
Hello, I am a beginner of VB, I just got a problem...
I am writing a program and i want the listview/text box in the interface can be resized in runtime by using the mouse drap and drop.
Pls give me some suggestion !!! can i do it ??
Pls give me a EXAMPLE and Descriptions!!!

Question by:rng
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
LVL 10

Accepted Solution

caraf_g earned 300 total points
ID: 1527687
With thanks to waty:

1 - BAS Module Code

Option Explicit

'windows constants  
Public Const SWP_DRAWFRAME = &H20
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOZORDER = &H4
                         SWP_NOMOVE Or SWP_DRAWFRAME
Public Const GWL_STYLE = (-16)
Public Const WS_THICKFRAME = &H40000

Public Declare Function GetWindowLong Lib _
    "user32" Alias "GetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Public Declare Function SetWindowLong Lib _
    "user32" Alias "SetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long, _
     ByVal dwNewLong As Long) As Long

Public Declare Function SetWindowPos Lib "user32" _
    (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
     ByVal X As Long, ByVal Y As Long, ByVal cx As Long, _
     ByVal cy As Long, ByVal wFlags As Long) As Long

1 - Form Code

'To a form add a text box (Text1), a command '
'button (cmdEnd), 2 menu items (mnuSize & 
'mnuRestore), and 2 labels (Label1 & Label2).
'Add the following code to the form:

Option Explicit

  'initial Form_Load variables  
   Dim initBoxStyle As Long
   Dim initLeft As Integer
   Dim initTop As Integer
   Dim initWidth As Integer
   Dim initHeight As Integer

Private Sub Form_Load()

  'position the form  
   Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
  'set up a few variables needed later  
   initBoxStyle = GetWindowLong(Text1.hwnd, GWL_STYLE)
   initLeft = Text1.Left
   initTop = Text1.Top
   initWidth = Text1.Width
   initHeight = Text1.Height

  'make sure the defaults are set  
   SetControlStyle initBoxStyle, Text1
   Label1 = "Text Width  : " & Text1.Width
   Label2 = "Text Height : " & Text1.Height

End Sub

Private Sub Form_Unload(Cancel As Integer)

  'restore the control to a normal textbox  
   SetControlStyle initBoxStyle, Text1

End Sub

Private Sub Form_Click()

  'restore the control to a normal textbox  
   SetControlStyle initBoxStyle, Text1
   Label1 = "Text Width  : " & Text1.Width
   Label2 = "Text Height : " & Text1.Height
End Sub

Private Sub mnuSize_Click()

   Dim style As Long
  'get the current style attributes for the textbox  
   style = GetWindowLong(Text1.hwnd, GWL_STYLE)
  'modify the style to show the sizing frame  
   style = style Or WS_THICKFRAME
  'set the control to the chosen style  
   SetControlStyle style, Text1

End Sub

Private Sub mnuRestore_Click()

  'restore the control to a normal textbox  
   SetControlStyle initBoxStyle, Text1
End Sub

Private Sub cmdEnd_Click()
    Unload Me
End Sub

Private Sub SetControlStyle(style, X As Control)
  Dim r
  If style Then
    Call SetWindowLong(X.hwnd, GWL_STYLE, style)
    Call SetWindowPos(X.hwnd, Form1.hwnd, 0, 0, 0, 0, SWP_FLAGS)
  End If

End Sub

'Save the app & run. Clicking the 'Size' menu '
'item will cause the textbox border to change'
'into a sizeable frame. After resizing, clicking
'the form or selecting 'Restore' will return the
'textbox to the normal textbox style at the
'selected size and position.

'This same technique can also be successfully
'applied to lists, listviews, treeviews,
'pictureboxes ... pretty well most VB controls.


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question