Resizing form and textboxes ...-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 72

Thread: Resizing form and textboxes ...

Hybrid View

  1. #1
    nottheboss
    Guest

    Question Resizing form and textboxes ...

    Can someone tell me how to get the text box to resize along with the form? I also have 2 command buttons under the text box which I want to stay centered and at the bottom of the form when I resize it.

    Right now, the textbox stays the same size when I resize the Main Form and the command buttons stay in the same place directly under the textbox.

    I attached the form here.

    Thanks,

    Eric
    Attached Files Attached Files

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    32,867
    Code:
    Option Explicit
    
    Private Type CtrlProportions
        HeightProportions As Single
        WidthProportions As Single
        TopProportions As Single
        LeftProportions As Single
    End Type
    
    Dim ProportionsArray() As CtrlProportions
    
    Sub InitResizeArray()
    
        Dim I As Integer
        
        On Error Resume Next
        
        ReDim ProportionsArray(0 To Controls.Count - 1)
        
        For I = 0 To Controls.Count - 1
            With ProportionsArray(I)
                .HeightProportions = Controls(I).Height / ScaleHeight
                .WidthProportions = Controls(I).Width / ScaleWidth
                .TopProportions = Controls(I).Top / ScaleHeight
                .LeftProportions = Controls(I).Left / ScaleWidth
            End With
        Next I
        
    End Sub
    
    Sub ResizeControls()
    
        On Error Resume Next
        
        Dim I As Integer
        
        For I = 0 To Controls.Count - 1
            With ProportionsArray(I)
                ' move and resize controls
                Controls(I).Move .LeftProportions * ScaleWidth, _
                .TopProportions * ScaleHeight, _
                .WidthProportions * ScaleWidth, _
                .HeightProportions * ScaleHeight
            End With
        Next I
        
    End Sub
    
    'Form initialize event
    Private Sub Form_Initialize()
    
        InitResizeArray
        
    End Sub
    
    'Form resize event
    Sub Form_Resize()
    
        ResizeControls
        
    End Sub

  3. #3
    PWNettle
    Guest
    If you want controls on your form to resize along with the form you'll need to add some code to accomplish this in the Form_Resize event. In your case you could determine the size of the textboxes in proportion to the dimensions of the form and center the command buttons based on their width(s) and the width of the form.

    Good luck,
    Paul

  4. #4
    nottheboss
    Guest

    Thanks a Zillion!!!

    Thanks Martin....

    You certainly could not have made that any easier. At first I thought .. ok.. How will I work this in to my program?

    Then, after reading it, I realized that i just had to paste it in!

    Wow, that made my life easy.

    Again,

    Thank you very much.


  5. #5

  6. #6

  7. #7
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335
    You could have kept that a secret Martin. I wouldn't have told anyone!
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    3,890
    Martin

    I'm looking at your code here and it appears that the problem you are having with the combo boxes not resizing is quite simple. The height property of a combo box is read only. If you try to use the move method on a combo box it will cause an error. Since you have resume next, it will just skip that control. Something like this might be better.
    VB Code:
    1. Public Sub ResizeControls()
    2.     Dim I As Integer
    3.    
    4.     On Error Resume Next
    5.  
    6.     For I = 0 To Controls.Count - 1
    7.         ' move and resize controls
    8.         Controls(I).Left = ProportionsArray(I).LeftProportions * Me.ScaleWidth
    9.         Controls(I).Top = ProportionsArray(I).TopProportions * Me.ScaleHeight
    10.         Controls(I).Width = ProportionsArray(I).WidthProportions * Me.ScaleWidth
    11.         Controls(I).Height = ProportionsArray(I).HeightProportions * Me.ScaleHeight
    12.     Next I    
    13. End Sub
    Last edited by MarkT; Jan 29th, 2002 at 11:43 AM.

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    32,867
    Thanks for the reply, but I knew the reason for the problem and I was just alerting people to it. My On Error Resume Next code, while avoiding an error when the combobox is encountered was actually put there for menu items etc. that don't have a Move method. BTW, what do you see as the advantage of your code over mine?

  10. #10
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    3,890
    BTW, what do you see as the advantage of your code over mine?
    The only real advantage is it will reposition combo boxes instead of leaving them in the same place.

  11. #11
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    LoCal
    Posts
    280
    That code works great Martin!

    I was able to put it to use in one of my projects and added a couple of simple mods to it that I thought others might find use for.

    I wrapped it up into a class and added another method to be able to specify whether or not you want a particular control (or controls) to move or resize.

    Hope this is of use to someone...

    Here's the class module code:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type CtrlProportions
    4.     Name As String
    5.     Move As Boolean
    6.     Resize As Boolean
    7.     HeightProportions As Single
    8.     WidthProportions As Single
    9.     TopProportions As Single
    10.     LeftProportions As Single
    11. End Type
    12.  
    13. Private mProportionsArray() As CtrlProportions
    14.  
    15. Public Sub InitResizeArray(objForm As Form)
    16.  
    17.     Dim I As Integer
    18.    
    19.     On Error Resume Next
    20.    
    21.     ReDim mProportionsArray(0 To objForm.Controls.Count - 1)
    22.    
    23.     For I = 0 To objForm.Controls.Count - 1
    24.         With mProportionsArray(I)
    25.             .Name = objForm.Controls(I).Name
    26.             .Move = True
    27.             .Resize = True
    28.             .HeightProportions = objForm.Controls(I).Height / objForm.ScaleHeight
    29.             .WidthProportions = objForm.Controls(I).Width / objForm.ScaleWidth
    30.             .TopProportions = objForm.Controls(I).Top / objForm.ScaleHeight
    31.             .LeftProportions = objForm.Controls(I).Left / objForm.ScaleWidth
    32.         End With
    33.     Next I
    34.    
    35. End Sub
    36.  
    37. Public Sub ResizeControls(objForm As Form)
    38.  
    39.     On Error Resume Next
    40.    
    41.     Dim I As Integer
    42.    
    43.     For I = 0 To objForm.Controls.Count - 1
    44.         With mProportionsArray(I)
    45.             ' move and resize objcontrols
    46.             If .Move Then
    47.                 objForm.Controls(I).Left = .LeftProportions * objForm.ScaleWidth
    48.                 objForm.Controls(I).Top = .TopProportions * objForm.ScaleHeight
    49.             End If
    50.             If .Resize Then
    51.                 objForm.Controls(I).Width = .WidthProportions * objForm.ScaleWidth
    52.                 objForm.Controls(I).Height = .HeightProportions * objForm.ScaleHeight
    53.             End If
    54.         End With
    55.     Next I
    56.    
    57. End Sub
    58.  
    59. Public Sub SetControlProperties(strName As String, flgMove As Boolean, flgResize As Boolean)
    60.  
    61.     Dim I As Integer
    62.    
    63.     For I = 0 To UBound(mProportionsArray())
    64.         If mProportionsArray(I).Name = strName Then
    65.             mProportionsArray(I).Move = flgMove
    66.             mProportionsArray(I).Resize = flgResize
    67.         End If
    68.     Next I
    69.            
    70. End Sub

    To use it inside a form:
    VB Code:
    1. Option Explicit
    2.  
    3. Private mobjResizer As Resizer
    4.  
    5. Private Sub Form_Resize()
    6.  
    7.     mobjResizer.ResizeControls Me
    8.  
    9. End Sub
    10.  
    11. Private Sub Form_Load()
    12.  
    13.     Set mobjResizer = New Resizer
    14.    
    15.     mobjResizer.InitResizeArray Me
    16.  
    17.     ' move but don't resize the Ok button
    18.     mobjResizer.SetControlProperties "cmdOk", True, False
    19.  
    20.     ' resize but don't move the Close button
    21.     mobjResizer.SetControlProperties "cmdClose", False, True
    22.  
    23. End Sub
    Last edited by Achichincle; Jun 4th, 2002 at 02:33 PM.
    Achichincle

    VB6 (VSEE SP5, W2KPro)
    ASP
    HTML

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    32,867
    I've recently added the following modification to that code that adjusts the font size within the controls.
    VB Code:
    1. [b]Dim dblFontSize As Double[/b]
    2.    
    3.     ' 8310 is the default ScaleHeight of my form
    4.     dblFontSize = 8.5 * (frmMain.ScaleHeight / 8310)
    5.  
    6.     For intIndex = 0 To frmMain.Controls.Count - 1
    7.         With typCtrlProportions(intIndex)
    8.             ' move and resize frmmain.Controls
    9.             [b]frmMain.Controls(intIndex).FontSize = dblFontSize[/b]
    10.             Select Case True
    11.  
    12. ' etc.

  13. #13
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,599
    I just found this while doing a search; works great! Bravo, Martin.
    "It's cold gin time again ..."

    Check out my website here.

  14. #14
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    Also hiting the maximise button does not resize the form..

  15. #15

  16. #16
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    Ok, will do. The thing is I use dozens of controls and potentially two or three hundred on the same form depending how the user configures his form. I realise this is puting a great burden on the system.
    Would you have a suggestion as to a why to the SSTab issue. I will adapt your code and see what comes out. Thanks.

  17. #17
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    Yes the form does get maximised. Sorry my mistake.

  18. #18
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    Please Martin, if you have a second can you look at the screenshots for my app using your resize code. As you can see, what i did was to maximise then minimise the window. We see that on tab one all three lines of controls appear correctly but then on tab 3 the same three lines appear when they really belong to tab 3. also on the last shot the frames are all wrong. Any ideas as to why this is and how to remedy it? Thanks again.
    Attached Files Attached Files

  19. #19

  20. #20
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    Ok. Thanks. I got the tab #'s wrong, it should read:

    We see that on tab one all three lines of controls appear correctly but then on tab 3 the same three lines appear when they really belong to tab 1.

  21. #21
    New Member
    Join Date
    Nov 2002
    Posts
    1
    Well done all,
    specail thanks goes to Martinliss and MarkTMarkT and Achichincle .

    Only mixture of all of these gives a neat solution to the problem.

  22. #22
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    32,867
    Originally posted by MikeGarvin
    Ok. Thanks. I got the tab #'s wrong, it should read:

    We see that on tab one all three lines of controls appear correctly but then on tab 3 the same three lines appear when they really belong to tab 1.
    How did you create those controls that you want to be on tab 3? Did you draw (or copy/paste) them on that tab, or did you just double-click the control in the toolbar? The latter doesn't work properly for a container control like the tabbed dialog. To see if that is your problem do something like this:

    MsgBox MyControlThatsOnATab.Container.Tab

    and see what tab it's actually on. If that shows you have a problem then do the following if you want the control on tab 3.

    MyControlThatsOnATab.Container.Tab = 3

  23. #23
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    Thanks Martin. The controls are created at run time from the first line, which is there at design time. I simply create new lines of controls from what the user chooses.

    I will try what you suggest. Thanks.

    Would you know why the frames don't appear on the other sstabs. I believe they are there but are offset to the left completely as explained in my initial post. Cheers.

  24. #24
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    http://www.vbforums.com/showthread.p...ppear+controls

    Have found a link with exactly the problem I have. Cheers. Mike.

  25. #25

  26. #26
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289
    The flickering was not very nice before but now with 6*24 frames on my app it's just bad . I find it incredible the left property on tabs should not just stay to a the value you assign it at design time. VB, eh, some language!

    Would you have a better way?

  27. #27
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by MartinLiss
    Does that resolve your problem?
    Sorry to re-open this thread...but whats the final code here?

    Should this just be added to a public module and called in any resize event of any form?

    Thanks,
    Jon

  28. #28
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by MartinLiss
    Does that resolve your problem?
    Sorry to re-open this thread...but whats the final code here?

    Should this just be added to a public module and called in any resize event of any form? Also I read in someone's response that this does not work when someone hits the maximize button. Is this true?

    Thanks,
    Jon

  29. #29
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    32,867
    Yes that all you need to do and if you call it from the form's resize event it will work fine when you maximize the form. In my app I found that I need to do the following when the form is minimized however.

    VB Code:
    1. On Error Resume Next ' Needed if the form is minimized
    2.     If Me.Width < 900 Then
    3.         Me.Width = 900
    4.     End If
    5.     If Me.Height < 900 Then
    6.         Me.Height = 900
    7.     End If
    8.    
    9.     ResizeControls

  30. #30
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Hi Martin,

    Sorry to be a bother...but in various places you have added code.

    Anyway you can lump it all togethr with an end result of all the code in this LAST post

    Thanks,
    Jon

  31. #31
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Is it me..or does this code totally not work for tabbed pane layout ? The controls on all other tabs disappear. The flex grids disappear. The combo boxes dont resize.

    I dont think im going to use this.

    Jon

  32. #32

  33. #33
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Figured that this was too good to be true

    Anyone know of any 3rd party software to help me re-scale everything? Including tabs, combo boxes, and Grids?

    Jon

  34. #34
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    SSTab .Left property quirks

    Here's why controls on tab panes are so flukey.

    From: Microsoft Knowledge Base Article - 187562
    "HOWTO: Resize the Controls in SSTab When Form is Resized"

    When a form is resized, the controls present in the form can be resized to fit into the form by using the Move method. However, it is not straightforward to resize the controls present inside an SSTab control because the Left property values of controls present in the inactive tabs are less than 0. This article describes a way to resize the controls present in an SSTab control.

    The SSTab control hides the controls present in the inactive tabs by setting their Left property values less by 75000. Hence, changing the Left property of a control on an inactive tab in the form's resize event may cause that control to stop showing in a particular tab.
    =======================================

    I made some Rube Goldberg changes to Achichincle's class above, see "SSTab" in code:

    VB Code:
    1. 'To use, put the dozen or so lines below inside your form:
    2. '
    3. Private mobjResizer As Resizer
    4.  
    5. Private Sub Form_Resize()
    6.     mobjResizer.ResizeControls Me
    7. End Sub
    8.  
    9. Private Sub Form_Load()
    10.  
    11.     Set mobjResizer = New Resizer
    12.     mobjResizer.InitResizeArray Me
    13.     ' Optional: move but don't resize the Ok button
    14.     mobjResizer.SetControlProperties "cmdOk", True, False
    15.     ' Optional: resize but don't move the Close button
    16.     mobjResizer.SetControlProperties "cmdClose", False, True
    17. End Sub
    18.  
    19. '===========================================================
    20.  
    21.  
    22. ' Put everything below into a class module named "Resizer"
    23. Private Type CtrlProportions
    24.     Name As String
    25.     Move As Boolean
    26.     Resize As Boolean
    27.     HeightProportions As Single
    28.     WidthProportions As Single
    29.     TopProportions As Single
    30.     LeftProportions As Single
    31. End Type
    32.  
    33. Private mProportionsArray() As CtrlProportions
    34.  
    35.  
    36. Public Sub InitResizeArray(objForm As Form)
    37.  
    38.     Dim I As Integer
    39.     Dim ObjLeft As Long
    40.     Dim ObjParentTyp As String
    41.     Dim LeftOffset As Long
    42.  
    43.     On Error Resume Next
    44.    
    45.     ReDim mProportionsArray(0 To objForm.Controls.Count - 1)
    46.    
    47.     For I = 0 To objForm.Controls.Count - 1
    48.         With mProportionsArray(I)
    49.             .Name = objForm.Controls(I).Name
    50.             .Move = True
    51.             .Resize = True
    52.  
    53.             .HeightProportions = objForm.Controls(I).Height / objForm.ScaleHeight
    54.             .WidthProportions = objForm.Controls(I).Width / objForm.ScaleWidth
    55.             .TopProportions = objForm.Controls(I).Top / objForm.ScaleHeight
    56.  
    57.             ' Have to be a little careful with .Left here. Controls on an SSTab
    58.             ' control have -75000 added to their .Left prop to make them invisible.
    59.             LeftOffset = 0
    60.             ObjLeft = objForm.Controls(I).Left
    61.             ObjParentTyp = TypeName(objForm.Controls(I).Container)
    62.             If ObjParentTyp = "SSTab" And ObjLeft < 0 Then LeftOffset = 75000
    63.             .LeftProportions = (ObjLeft + LeftOffset) / objForm.ScaleWidth
    64.         End With
    65.     Next I
    66.  
    67. End Sub
    68.  
    69.  
    70. Public Sub ResizeControls(objForm As Form)
    71.  
    72.     On Error Resume Next
    73.    
    74.     Dim I As Integer
    75.     Dim ObjParentTyp As String
    76.     Dim LeftOffset As Long
    77.  
    78.     For I = 0 To objForm.Controls.Count - 1
    79.         With mProportionsArray(I)
    80.             ' move and resize objcontrols
    81.             If .Move Then
    82.                 LeftOffset = 0
    83.                 ObjParentTyp = TypeName(objForm.Controls(I).Container)
    84.                 If ObjParentTyp = "SSTab" And objForm.Controls(I).Left < 0 Then LeftOffset = 75000
    85.  
    86.                 objForm.Controls(I).Left = .LeftProportions * objForm.ScaleWidth - LeftOffset
    87.                 objForm.Controls(I).Top = .TopProportions * objForm.ScaleHeight
    88.             End If
    89.             If .Resize Then
    90.                 objForm.Controls(I).Width = .WidthProportions * objForm.ScaleWidth
    91.                 objForm.Controls(I).Height = .HeightProportions * objForm.ScaleHeight
    92.             End If
    93.         End With
    94.     Next I
    95.    
    96. End Sub
    97.  
    98.  
    99. Public Sub SetControlProperties(strName As String, flgMove As Boolean, flgResize As Boolean)
    100.  
    101.     Dim I As Integer
    102.    
    103.     For I = 0 To UBound(mProportionsArray())
    104.         If mProportionsArray(I).Name = strName Then
    105.             mProportionsArray(I).Move = flgMove
    106.             mProportionsArray(I).Resize = flgResize
    107.         End If
    108.     Next I
    109.            
    110. End Sub
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  35. #35
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Multiple forms and run-time controls

    I think Achichincle's class scheme has the advantage of working with multiple forms, i.e. they'll each have their own instance and won't interfere.

    I was wondering how this would handle dynamically created controls at run-time ...
    By just adding a call to
    VB Code:
    1. mobjResizer.InitResizeArray Me
    after creating them it all seems to work beautifully.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  36. #36
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    Re: Multiple forms and run-time controls

    Even for combo boxes?

    Jon

  37. #37
    PowerPoster
    Join Date
    Nov 2001
    Posts
    2,086
    Would someone pls post which of the codes is the final one.

    Thank you.
    John

  38. #38
    Addicted Member Cimperiali's Avatar
    Join Date
    Oct 2002
    Location
    Milan, Italy, Europe
    Posts
    188

    By the way: coding form resize...

    in resize event of a form:
    VB Code:
    1. On Error Resume Next ' Needed if the form is minimized'  
    2.   ' for the minimizing: better code:
    3.    if me.windowstate<>vbMinimized then
    4.       'as you do not need to size non visible controls....
    5.    
    6.       If Me.Width < 900 Then
    7.          Me.Width = 900
    8.          Exit Sub  '<---Do not forget this, or you will execute the
    9.                        'remaining code more times than needed
    10.       End If
    11.       If Me.Height < 900 Then
    12.          Me.Height  = 900
    13.          Exit Sub  '<---Do not forget this, or you will execute the
    14.                        'remaining code more times than needed
    15.  
    16.       End If
    17.    
    18.       ResizeControls
    19.     End If
    Special thanks to some wonderful people,
    such as Lothar the Great Haensler, Aaron Young,
    dr_Michael, Chris Eastwood, TheOnlyOne ClearCode....

  39. #39
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Marty,

    I noticed y ou made some changes including font size.
    Can you post the entire code...so I get a clean copy.

    Thanks,
    Jon

  40. #40

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.