Results 1 to 32 of 32

Thread: Control Resize Code

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Control Resize Code

    The following code will reposition and resize almost all controls on a form. Any comments and/or suggestions on improving this code will be greatly appreciated:

    VB Code:
    1. Option Explicit
    2. Dim sngFrmTop As Single
    3. Dim sngFrmLeft As Single
    4. Dim sngFrmHeight As Single
    5. Dim sngFrmWidth As Single
    6.  
    7. Dim sngCntTop() As Single
    8. Dim sngCntLeft() As Single
    9. Dim sngCntHeight() As Single
    10. Dim sngCntWidth() As Single
    11. Dim sngCntFont() As Single
    12.  
    13. Private Sub Form_Load()
    14. Dim a As Long
    15.    
    16.    On Error GoTo Form_Load_Error
    17.  
    18.     sngFrmTop = Top
    19.     sngFrmLeft = Left
    20.     sngFrmHeight = Height
    21.     sngFrmWidth = Width
    22.    
    23.     ReDim sngCntTop(Controls.Count)
    24.     ReDim sngCntLeft(Controls.Count)
    25.     ReDim sngCntHeight(Controls.Count)
    26.     ReDim sngCntWidth(Controls.Count)
    27.     ReDim sngCntFont(Controls.Count)
    28.    
    29.     For a = 0 To Controls.Count - 1
    30.         With Me.Controls(a)
    31.             sngCntTop(a) = (.Top / sngFrmHeight)
    32.             sngCntLeft(a) = (.Left / sngFrmWidth)
    33.             sngCntHeight(a) = (.Height / sngFrmHeight)
    34.             sngCntWidth(a) = (.Width / sngFrmWidth)
    35.             sngCntFont(a) = (.FontSize / sngFrmHeight)
    36.         End With
    37.     Next a
    38.  
    39.    On Error GoTo 0
    40.    Exit Sub
    41.  
    42. Form_Load_Error:
    43. If Err.Number = 13 Then
    44.     Resume Next
    45. Else
    46.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load of Form Form1"
    47. End If
    48. End Sub
    49.  
    50. Private Sub Form_Resize()
    51. Dim a As Long
    52.    
    53.    On Error GoTo Form_Resize_Error
    54.  
    55.     sngFrmTop = Top
    56.     sngFrmLeft = Left
    57.     sngFrmHeight = Height
    58.     sngFrmWidth = Width
    59.    
    60.     For a = 0 To Me.Controls.Count - 1
    61.         With Me.Controls(a)
    62.             .Top = sngFrmHeight * sngCntTop(a)
    63.             .Left = sngFrmWidth * sngCntLeft(a)
    64.             .Height = sngFrmHeight * sngCntHeight(a)
    65.             .Width = sngFrmWidth * sngCntWidth(a)
    66.             .FontSize = Int(sngFrmHeight * sngCntFont(a))
    67.         End With
    68.     Next a
    69.  
    70.    On Error GoTo 0
    71.    Exit Sub
    72.  
    73. Form_Resize_Error:
    74. If Err.Number = 383 Then 'Catch Error for controls with read only properties
    75.     Resume Next
    76. Else
    77.     MsgBox "Error " & Err.Number & " (" & Err.Description & _
    78.             ") in procedure Form_Resize of Form Form1"
    79. End If
    80. End Sub
    Last edited by Mark Gambo; Dec 15th, 2005 at 05:14 PM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Control Resize Code

    I like it a lot. I tried it with a form full of controls (practically everything in the standard toolbox) and it work like a charm.

    My only suggestion would be that the variables declared at the Form level should be declared using Private. Dim really should be reserved for event level variables.

    Having been a little anal and picky, I still like it and gave reps!

  3. #3
    Banned
    Join Date
    Dec 2005
    Location
    india
    Posts
    35

    Re: Control Resize Code

    well i am not quailified to comment on such hi fi code but its great and i will definetly use it in my project

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,858

    Re: Control Resize Code

    This works very nicely (I'm sure a link will be coming here from the FAQ soon!), I have a few comments for improvements in addition to Hacks tho -
    • Form_Resize (and form_load) should ignore error 438 too ("Object doesn't support this property or method" - caused by the Shape control).
    • Try this with a small form and maximize it, then restore it. You will see that text boxes are taller than they should be It's an easy fix tho, just put the .FontSize line before the .Top line.
    • There is no need to resize controls if the form is minimized, so you could add this to the top of the Form_Resize event:
      If Me.WindowState = vbMinimized Then Exit Sub
    • You should add comments to say "other code goes here", so people know where to put their own code
    • ..and one (silly) picky one - you should rename sngCntFont to sngCntFontSize

  5. #5

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Control Resize Code

    Hack and Si,
    Thanks for your suggestions and kind words , they will be implemented in Version 2. I have been doing some searching and I will probably wrap the code into a class. I was thinking about using a Private Type instead of the arrays that I am currently using similiar to Martin's code in this POST.

    tux_newbie Thank You.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Control Resize Code

    Quote Originally Posted by Mark Gambo
    I have been doing some searching and I will probably wrap the code into a class.
    If you do this, please make sure you give a good, clear, example of how to reference and use it from within a standard VB project.

    There will be a lot of folks looking at this who will have no idea how to actually implement it if it is in a class without a good, working, example.

  7. #7

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Control Resize Code

    Quote Originally Posted by Hack
    If you do this, please make sure you give a good, clear, example of how to reference and use it from within a standard VB project.

    There will be a lot of folks looking at this who will have no idea how to actually implement it if it is in a class without a good, working, example.

    Will do!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  8. #8

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Control Resize Code

    Ok, here is version 2.0.0:

    In a Class Module (Leave the name as Class1):
    VB Code:
    1. Option Explicit
    2. Public a As Long
    3. Public sngFrmTop As Single
    4. Public sngFrmLeft As Single
    5. Public sngFrmHeight As Single
    6. Public sngFrmWidth As Single
    7.  
    8. Public Sub ControlResize(FormName As Form)
    9. On Error GoTo ControlResize_Error
    10.  
    11. With FormName
    12.     If .WindowState = vbMinimized Then Exit Sub
    13.    
    14.         sngFrmTop = .Top
    15.         sngFrmLeft = .Left
    16.         sngFrmHeight = .Height
    17.         sngFrmWidth = .Width
    18.        
    19.         For a = 0 To .Controls.Count - 1
    20.             With .Controls(a)
    21.                 .FontSize = Int(sngFrmHeight * ControlMetrix(a).sngCntFontSize)
    22.                 .Top = sngFrmHeight * ControlMetrix(a).sngCntTop
    23.                 .Left = sngFrmWidth * ControlMetrix(a).sngCntLeft
    24.                 .Height = sngFrmHeight * ControlMetrix(a).sngCntHeight
    25.                 .Width = sngFrmWidth * ControlMetrix(a).sngCntWidth
    26.             End With
    27.         Next a
    28.        
    29. End With
    30.  
    31. On Error GoTo 0
    32. Exit Sub
    33.  
    34. ControlResize_Error:
    35. If Err.Number = 383 Or Err.Number = 438 Or _
    36.         Err.Number = 380 Then
    37.      'Catch Error for controls with read only properties
    38.     Resume Next
    39. Else
    40.     MsgBox "Error " & Err.Number & " (" & Err.Description & _
    41.           ") in procedure ControlResize of Class Module Class1"
    42. End If
    43.  
    44. End Sub
    45.  
    46. Public Sub GetFormMetrix(FormName As Form)
    47. On Error GoTo GetFormMetrix_Error
    48.  
    49. With FormName
    50.     sngFrmTop = .Top
    51.     sngFrmLeft = .Left
    52.     sngFrmHeight = .Height
    53.     sngFrmWidth = .Width
    54.    
    55.     ReDim ControlMetrix(.Controls.Count - 1)
    56.        
    57.     For a = 0 To .Controls.Count - 1
    58.    
    59.         With .Controls(a)
    60.             ControlMetrix(a).sngCntTop = (.Top / sngFrmHeight)
    61.             ControlMetrix(a).sngCntLeft = (.Left / sngFrmWidth)
    62.             ControlMetrix(a).sngCntHeight = (.Height / sngFrmHeight)
    63.             ControlMetrix(a).sngCntWidth = (.Width / sngFrmWidth)
    64.             ControlMetrix(a).sngCntFontSize = (.FontSize / sngFrmHeight)
    65.         End With
    66.    
    67.     Next a
    68. End With
    69.  
    70. On Error GoTo 0
    71. Exit Sub
    72.  
    73. GetFormMetrix_Error:
    74. If Err.Number = 13 Or Err.Number = 383 Or Err.Number = 438 Or _
    75.         Err.Number = 380 Then
    76.      'Catch Error for controls with read only properties
    77.     Resume Next
    78. Else
    79.     MsgBox "Error " & Err.Number & " (" & Err.Description & _
    80.           ") in procedure GetFormMetrix of Class Module Class1"
    81. End If
    82. End Sub

    In a BAS Module:
    VB Code:
    1. Option Explicit
    2.  
    3. Public Type ControlMetrix
    4.     sngCntTop As Single
    5.     sngCntLeft As Single
    6.     sngCntHeight As Single
    7.     sngCntWidth As Single
    8.     sngCntFontSize As Single
    9. End Type
    10.  
    11. Public ControlMetrix() As ControlMetrix

    and in the Form copy the following code and
    place some controls in various positions on the form:
    VB Code:
    1. Option Explicit
    2.  
    3. Private FormResize As Class1
    4.  
    5. Private Sub Form_Load()
    6.     Set FormResize = New Class1
    7.     Call FormResize.GetFormMetrix(Me)
    8. End Sub
    9.  
    10. Private Sub Form_Resize()
    11.     Call FormResize.ControlResize(Me)
    12. End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Control Resize Code

    Even pickier: Metrix is spelled Metrics.

    Haven't tried it yet, but will do soon.

  10. #10

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Control Resize Code

    Quote Originally Posted by dglienna
    Even pickier: Metrix is spelled Metrics.

    Haven't tried it yet, but will do soon.

    I know that, "FormMetrix" is my brand name . So that when I see this code posted to the board by someone else, I know where they got it .
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  11. #11
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    Hi Mark, thanks very much for this code and your work too

    I am trying to impliment this into my project.
    I have an MDI form and numerous child forms

    I have copied your code exactly into a test project with one MDI form and one child form.

    On the child form I have the following:
    Command button
    Text box
    Listbox
    Combo box
    Picture box with picture
    Label
    Dir box

    In a frame I have:
    command button
    combo box
    text box
    listbox

    When I run this I get the error:
    Runtime error 383 'Height' property is read only

    I see you have this in the error handler, but for some reason it is not getting that far. It doesn't error out straight away - 'a' = 6.
    The project stops on the line
    Code:
    Public Sub ControlResize(FormName As Form)
    
    On Error GoTo ControlResize_Error
    
    With FormName
        If .WindowState = vbMinimized Then Exit Sub
        
        sngFrmTop = .Top
        sngFrmLeft = .Left
        sngFrmHeight = .Height
        sngFrmWidth = .Width
        
        For a = 0 To .Controls.Count - 1
            With .Controls(a)
                .FontSize = Int(sngFrmHeight * ControlMetrix(a).sngCntFontSize)
                .Top = sngFrmHeight * ControlMetrix(a).sngCntTop
                .Left = sngFrmWidth * ControlMetrix(a).sngCntLeft
                .Height = sngFrmHeight * ControlMetrix(a).sngCntHeight
    Any idea what I am doing wrong?

    Does this handle Flexgrids and Listviews?
    Last edited by aikidokid; Sep 29th, 2007 at 01:00 PM. Reason: add value of counter for controls
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,858

    Re: Control Resize Code

    It will get an error like that if the control doesn't support setting the value (some controls are fixed height/width).

    The error handler should cope with it, so I guess you just have the IDE's break option set incorrectly. Right-click in a code window, and select "Toggle", and select either "Break in Class Module" or "Break on Unhandled Errors".

  13. #13
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    Thanks si_the_geek, that sorted it.

    How does resizing work with MDI forms?
    Do you have to put something in the form_activate of the child form to say, if I am active and MDI form changes size, resize me?
    If this is along the right lines, how about when I open another child form after the MDI form has been resized?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,858

    Re: Control Resize Code

    You don't need to do anything in the _activate events.

    If the MDI child forms are maximised, they will be resized (and so their own resize events will fire) when the MDI parent is resized.

    If the MDI child forms are not maximised, they will stay the same size when the parent is resized (and scrollbars will be shown in the parent if apt). If you want to change that behaviour, you will need to write code in the Resize event of the parent.


    All non-minimised MDI child forms have the same windowstate (you can't have a mixture of 'maximised' and 'normal'), so if one is maximised they all will be. If you open a new child (without specifying a windowstate) it will be shown either maximised or normal, matching the windowstate of the existing forms.

  15. #15
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    Quote Originally Posted by si_the_geek
    If the MDI child forms are maximised, they will be resized (and so their own resize events will fire) when the MDI parent is resized.
    Now you say it, it makes sense

    Quote Originally Posted by si_the_geek
    All non-minimised MDI child forms have the same windowstate (you can't have a mixture of 'maximised' and 'normal'), so if one is maximised they all will be. If you open a new child (without specifying a windowstate) it will be shown either maximised or normal, matching the windowstate of the existing forms.
    Ok, just looked at this and I understand, so, why when I open the MDI form, I have one of my childforms open with it, but it only shows when the windowstate is either normal or minmised. If it is maximised it doesn't show.
    I can then open other childforms and they are maximised!
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,858

    Re: Control Resize Code

    That doesn't sound right.. I'd assume that you have code somewhere that is changing the state/position etc, but don't know quite what is going on. It is also possible that your child form isn't actually a child (check the properties window).

    I'd recommend debugging it.. step thru and check the windowstate/position/etc at each stage.

  17. #17
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    Quote Originally Posted by si_the_geek
    I'd assume that you have code somewhere that is changing the state/position etc, but don't know quite what is going on. It is also possible that your child form isn't actually a child (check the properties window).

    I'd recommend debugging it.. step thru and check the windowstate/position/etc at each stage.
    Firstly, check it and it is a child form.
    What do you mean by:
    you have code somewhere that is changing the state/position etc
    I thought that if the childform was set to maximised, then the form should resize with the MDI form, or does the childform still have to call it's own resize code, as in the MDI resize event?

    I know how to debug, or I thought I did, but how do I debug the windowstate?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  18. #18
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    Hmm, that's interesting, I have solved the probelm by changing the form borderstyle to 2. It doesn't seem to want to play with a borderless childform

    Not really want I wanted to do!
    Last edited by aikidokid; Sep 30th, 2007 at 01:24 PM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  19. #19
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,858

    Re: Control Resize Code

    I don't use borderless forms, but I just tried it and got the same.. not sure what is going on there!
    Quote Originally Posted by aikidokid
    What do you mean by:
    you have code somewhere that is changing the state/position etc
    I thought perhaps you had code somewhere (perhaps in a resize event) that was changing the windowstate etc.
    I thought that if the childform was set to maximised, then the form should resize with the MDI form,
    Correct.
    or does the childform still have to call it's own resize code, as in the MDI resize event?
    The child form would never need to call it's own resize event - it would be called automatically when the form resizes.

    Depending on the program, you may want to resize the child form (which would automatically fire its resize event) from the MDI parent.
    I know how to debug, or I thought I did, but how do I debug the windowstate?
    Add formX.windowstate as a Watch (making sure Procedure is 'all', and preferably the same for Module), and then use breakpoints and/or step the code.

  20. #20
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    Sorry to be a bit dim here.
    I have tried to work this out, mainly by trial and error.

    If I have the following code in place in the ChildForm.
    Code:
    Option Explicit 
    
    Private FormResize As Class1 
    
    Private Sub Form_Load()    
    
    Set FormResize = New Class1    
    Call FormResize.GetFormMetrix(Me)
    
    End Sub 
    
    Private Sub Form_Resize()    
    
    Call FormResize.ControlResize(Me)
    
    End Sub
    All ChildForm properties are:
    Windowstate = maximised
    Borderstyle = 2
    Controlbox = False

    I can get the form to load maximised, but when I try to minimise the form or select the 'Restore Down' button the MDI form changes but the ChildForm stays the same.
    Also I am getting a subscript error sometimes.

    Could this be because I have this code in all of the childforms (17) and it's still get the previous number of controls in memory?
    I wouldn't have thought so, but .....

    Also, when I change resolution from 1280X1024 to 1024X768 the forms stay the same size as they were for the higher resolution!

    I am totally confused now. This is why (originally) I made the form a fixed size without letting the user resize it! (not a good idea I know )
    Last edited by aikidokid; Oct 1st, 2007 at 10:01 AM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  21. #21
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,858

    Re: Control Resize Code

    Each form has their own instance of the class, so they wont get each others values. I don't know where the subscript error is coming from (but then I haven't looked at the class yet).

    If you resize the MDI Parent, the child forms will only resize if they are maximised. If their WindowState is Normal, they will stay the same size (and the parent will get scrollbars if apt); if you want to change that, you will need to add code to the resize event of the parent (check if Me.ActiveForm.WindowState is Normal, if it is then resize the child forms as you want).

  22. #22
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    I have been playing with this for a couple of hours and only a little the wiser to be honest!

    I have ALL 17 childforms set to Maximised.
    I have the same code (as above) in All 17 childforms.
    I have set my resolution to 1024 x 768 and redesigned the childform so all controls fit onto the screen.
    If I resize the form all works as I have been hoping for.

    If I change the screen resolution to 1152 x 864 (the next size available) the form loads maximised, but the controls are not centred as with the design resolution.
    How do I make it resizable and screen resolution independent (hope that is the correct term)

    I hope this explanation of my problem is clear, if not ask away!
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  23. #23
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,858

    Re: Control Resize Code

    I think the issue is that the size of 'maximised' varies, and as such the positions of controls may not be appropriate.

    Change the Child form properties, so that their initial Windowstate is Normal (and controls arranged appropriately). That will allow the code in Form_Load to store the relative (design-time) positions appropriately, and any subsequent resizing should work properly.

    If you want the child forms to be maximised when they are shown, you can change the windowstate at the end of form_load (but this will apply every time they open).

  24. #24
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    Quote Originally Posted by si_the_geek
    I think the issue is that the size of 'maximised' varies, and as such the positions of controls may not be appropriate.

    Change the Child form properties, so that their initial Windowstate is Normal (and controls arranged appropriately). That will allow the code in Form_Load to store the relative (design-time) positions appropriately, and any subsequent resizing should work properly.

    If you want the child forms to be maximised when they are shown, you can change the windowstate at the end of form_load (but this will apply every time they open).
    I have changed the windowstate to Normal and added the line:
    Me.Windowstate = 2
    to the Form_load and to the Form_Activate, as more than one childform could be open at any time.
    I still get the complete form in the upper left of the screen.

    The startupposition = 0 'Manual' as this is all it will let me have.
    Also, in my formload I have;
    Me.Top = 0
    Me.Left = 0
    But even taking this out, it's still the same.

    Is this anything to do with the design of my form, as it looks centered at the 1024 x 768 resolution?
    Last edited by aikidokid; Oct 1st, 2007 at 11:47 AM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  25. #25
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,858

    Re: Control Resize Code

    To be honest, I think I need to see the project to get my head around it clearly.

  26. #26
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    Quote Originally Posted by si_the_geek
    To be honest, I think I need to see the project to get my head around it clearly.
    Do you mind if I email it to you?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  27. #27
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    Hi Si_the_geek

    Just had to pop out for half hour then!

    Attached is the MDI form and one of the child forms.
    I am working on one form at the moment. If I can get that to work, then I can make the same adjustments to the others.

    The page is designed in screen resolution 1024 x 768

    What screen resolution would you mormally work in when designing forms?

    If needs be let me know and I will post the whole project.
    Attached Files Attached Files
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  28. #28
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,858

    Re: Control Resize Code

    I design forms in whatever resolution I am using - the resize code is what really matters (but if there are lots of controls, I try to make sure they fit in 800x600, as that is the minimum resolution users should have). I personally write resize code each time (and only move controls, not size them) rather than using something generic like this.. but once the issues are solved, this generic method should be fine.


    After a little play I can see that the declaration of the ControlMetrix() array should actually be in the class (as Private) rather than the module - as it is specific to each form (and thus class instance). The variables that are already within the class should also be Private.

    Ideally the code in the class would refer to .ScaleWidth/.ScaleHeight rather than .Width/.Height, so that the title bar and borders do not have an effect. Also, as the scalemode may change (and the controls are measured in the scalemode), you should use ScaleX/ScaleY to ensure they are in a constant scale, eg:
    Code:
        sngFrmHeight = .ScaleY(.ScaleHeight, .ScaleMode, vbTwips)
        sngFrmWidth = .ScaleX(.ScaleWidth, .ScaleMode, vbTwips)
    (this should be changed in both subs in the class).


    In my testing the controls were in almost the correct position, just a little larger than they should be (so "Notes" was half off-screen, and the buttons were right off).

    One problem I found is that your controls are on a frame.. which is a control, and as such will also be resized itself, and affect the position of the controls that are on it. By removing the frame, the display was more accurate (but still not quite right). Alternatively, the code in the class could be modified to deal with parent objects (like frames and pictureboxes), but that would take a fair amount of effort.

    Another issue is that the statusbar seems to actually hide part of the form, rather than limiting the size as would be expected. Unfortunately I cannot see a way to correct the effect that has on this code (as the MDI parent scaleheight etc act as if it isn't there).


    I don't think this will solve your issues completely, but hopefully things will be improved.

  29. #29
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    Thanks ever so much for all your help with this problem.

    I really do appreciate your time and effort

    I will look at this tomorrow and see what I can do.

    I will let you know here how I get on.

    BTW Can't rep you as only done it recently
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  30. #30
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    Quote Originally Posted by si_the_geek
    I design forms in whatever resolution I am using - the resize code is what really matters (but if there are lots of controls, I try to make sure they fit in 800x600, as that is the minimum resolution users should have).
    OK, got that, and I assume that the screen size would make no difference.
    I have a 19" monitor, and a friend testing this has a 15" monitor.

    Quote Originally Posted by si_the_geek
    I personally write resize code each time (and only move controls, not size them)
    Would this be ok for such a difference in screen resloution (1024 x 768 - 1280 x 1024)
    Althought that said, the resolution is down to the user - and their eye sight!

    Quote Originally Posted by si_the_geek
    After a little play I can see that the declaration of the ControlMetrix() array should actually be in the class (as Private) rather than the module - as it is specific to each form (and thus class instance). The variables that are already within the class should also be Private.
    Done.

    Quote Originally Posted by si_the_geek
    Ideally the code in the class would refer to .ScaleWidth/.ScaleHeight rather than .Width/.Height, so that the title bar and borders do not have an effect. Also, as the scalemode may change (and the controls are measured in the scalemode), you should use ScaleX/ScaleY to ensure they are in a constant scale, eg:
    Code:
        sngFrmHeight = .ScaleY(.ScaleHeight, .ScaleMode, vbTwips)
        sngFrmWidth = .ScaleX(.ScaleWidth, .ScaleMode, vbTwips)
    (this should be changed in both subs in the class).
    Done.

    Quote Originally Posted by si_the_geek
    In my testing the controls were in almost the correct position, just a little larger than they should be (so "Notes" was half off-screen, and the buttons were right off).
    I didn't get this!
    At 1024 x 768 it looked as it does in the IDE.

    Quote Originally Posted by si_the_geek
    One problem I found is that your controls are on a frame.. which is a control, and as such will also be resized itself, and affect the position of the controls that are on it. By removing the frame, the display was more accurate (but still not quite right).
    Removed this frame.

    Quote Originally Posted by si_the_geek
    Another issue is that the statusbar seems to actually hide part of the form, rather than limiting the size as would be expected. Unfortunately I cannot see a way to correct the effect that has on this code (as the MDI parent scaleheight etc act as if it isn't there).
    The status bar is not essential, but I would prefer to have it.
    I suppose one option would be to add it to all forms

    Quote Originally Posted by si_the_geek
    I don't think this will solve your issues completely, but hopefully things will be improved.
    To be honest, I don't seem to have this any further forward than before.
    It looks ok at the lower of the two resolutions, but anything else just doesn't look correct!

    I tried to comment out the resizing of the size of the controls, just leaving the top and left to be altered, but as I said, the complete form still sits in the top left hand of the MDI form, when at a higher resolution.

    Did you alter any of the code on the actual form?

    I am beginning to wonder if I should just design the forms for the lower of these resolutions and disable the Resize as I had it before.

    Your comment on any of this please si_the_geek!
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  31. #31
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,858

    Re: Control Resize Code

    Quote Originally Posted by aikidokid
    OK, got that, and I assume that the screen size would make no difference.
    I have a 19" monitor, and a friend testing this has a 15" monitor.
    Correct, the size of the monitor is irrelevant - only the resolution (and various components of it, such as "large fonts") matters.
    Would this be ok for such a difference in screen resloution (1024 x 768 - 1280 x 1024)
    Althought that said, the resolution is down to the user - and their eye sight!
    Standard Windows dialogs are not resized (unless using Large Fonts etc), so if the user can't see them then they have bigger issues than just your program!
    The status bar is not essential, but I would prefer to have it.
    I suppose one option would be to add it to all forms
    True, but that would take a lot of effort.

    Did you alter any of the code on the actual form?
    Not really (only enough to stop errors from code which used other forms/modules).

    I didn't get this!
    At 1024 x 768 it looked as it does in the IDE.
    Well to be honest I had only have solved the potential issues with ScaleMode... you should also convert all values for Top/Height using ScaleY and all values of Left/Width using ScaleX, eg:
    Code:
    .Top = ScaleY(sngFrmHeight * ControlMetrix(a).sngCntTop, vbTwips, .ScaleMode)
    Code:
    ControlMetrix(a).sngCntTop = ScaleY(.Top / sngFrmHeight, .ScaleMode, vbTwips)
    I am beginning to wonder if I should just design the forms for the lower of these resolutions and disable the Resize as I had it before.
    That is roughly the way I do it, I never bother with changing font sizes, or expanding controls in terms of percentage... the only time I resize/move controls is when there is a control which has scrollbars (such as a FlexGrid or ListView) to expand into the space, and then simply have code like: FG.Height = Me.ScaleHeight - FG.Top - MyBorderSize

    To deal with resolution options like Large Fonts, I tend to re-position controls based on their run-time size (in form_initialise). In your form, I would set all labels to Autosize, then (if needed) increase the .Left of the first 'column' of textboxes to allow for their width, then the same for the second column etc, and similar for the 'rows'.

  32. #32
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Control Resize Code

    ok, been working on this fro 3 days now

    I have it pretty much sorted I think.
    Just a couple of problems to overcome:

    1, When the MDI loads, in the FormLoad event it opens a ChildForm. This has, as suggested above, in the Form_Load and in the Form_Activate (not sure if I needed it here as well)
    Code:
    Me.WindowState = 2
    Now when the MDI form and the first ChildForm are opened, and I then click on the Maximise button for the MDI form, the first ChildForm is maximised.
    When the next ChildForm is opened it is not maximised?

    If I maximize the MDI after I open another ChildForm it works.

    Not sure what I am missing here!

    2, How to resize the columns of a Flexgrid and a Listview.
    Where would this go, in the childform resize, or in the control resize event?
    Last edited by aikidokid; Oct 7th, 2007 at 05:31 AM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

Posting Permissions

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



Click Here to Expand Forum to Full Width