Results 1 to 18 of 18

Thread: VB6 Control Anchoring and Docking made Easy

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2018
    Location
    New Orleans, Austin, Santa Monica
    Posts
    34

    VB6 Control Anchoring and Docking made Easy

    This is my attempt to make control anchoring as quick and easy as possible in VB6.

    VB6 control anchoring can be achieved with one line of code per Control in the Form1 Resize subroutine, with the addition of this one short module "ModuleControlAnchor"

    To anchor a control, add this module to your project, and place this one line of code into the Sub Form_Resize():

    Anchor ControlName, AnchorTop (True or False) , AnchorLeft, AnchorRight, AnchorBottom

    Please check out the demo and use freely.

    SeabrookStan
    Attached Files Attached Files
    Last edited by SeabrookStan; Jan 11th, 2019 at 02:12 AM. Reason: Code updated

  2. #2
    gibra
    Guest

    Re: VB6 Control Anchoring and Docking made Easy

    Good job.

    I add some Label controls in Picture1 and in Frame1, then add related code in Resize event:
    Code:
      Anchor Label2, False, False, True, True   ' Picture1
      Anchor Label3, False, False, True, True   ' Picture1
      Anchor Label4, False, True, False, True   ' Frame1
    - in Picture1 both labels works as expected

    - in Frame1 a 438 error (property or method not supported by object) occur. Error line is:
    Code:
    If AnchorBottom Then dB(C) = OBJ.Container.ScaleHeight - OBJ.Top - OBJ.Height Else dB(C) = -1
    How to solve this error?

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB6 Control Anchoring and Docking made Easy

    Frames do not have ScaleWidth,ScaleHeight properties, nor a ScaleMode property (defaulted to twips). The fix might require testing for containers that are Frames and getting their height/width relative to the parent's scalemode (unless a frame in a frame scenario exists, then it gets to be a bit more fun). Looking at the code comments, appears it was intended to align controls on forms only, not in other containers.

    And just FYI. I haven't tried the project, just looked at the module. I think there will be problems if an arrayed control is added for anchoring, i.e., Label1(0) and Labe1(1). Reason is that the module finds controls by their name and both of those labels have the same name.

    Suggestion. Loop through the OB() array instead. And in doing so, I don't think you'll need the CTL() array.
    Code:
    If OBJ Is OB(X) Then ...
    Last edited by LaVolpe; Jan 10th, 2019 at 01:06 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2018
    Location
    New Orleans, Austin, Santa Monica
    Posts
    34

    Re: VB6 Control Anchoring and Docking made Easy

    Thank you for identifying the Scalemode bug, LaVolpe.

    The new attachment, V3, now accommodates for Controls contained within other Controls, such as Frames and Pictureboxes (but assumes TWIPS for all containers) – see Demo.

    Known issues are limitations when used on multiple Forms simultaneously, and for anchoring of Control Arrays, and for non-TWIP scales. I believe I can overcome those limitations and will submit an updated version when done.

    SeabrookStan
    Attached Files Attached Files

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB6 Control Anchoring and Docking made Easy

    Tips - as long as you are going to submit a new version

    1. Resolve control arrays: already provided in previous post

    2. Multiple forms: Passed control's Parent is always the form, usercontrol, propertypage, etc (top level container), regardless of its container

    3. Prevent restricting to Twips for scale mode: Specifically test for frame containers and default to twips for them, otherwise, use the scalemode of the container as needed. To test for a frame:
    Code:
    If TypeOf OBJ.Container Is VB.Frame Then ...
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,525

    Re: VB6 Control Anchoring and Docking made Easy

    SeabrookStan,

    Your demo is cool to watch it all work.

    Just some thoughts I had when looking at the code. Just take them for whatever you like.

    1) I agree with LaVolpe that the CTL() array is possibly causing confusion, with everything you need done by the OB() array.

    2) If it were me, I'd tend to use a Collection for that OB() array (rather than an array). That would allow a couple of things. You might use the ObjPtr() as your key, and then you could quickly check the collection (using it's binary tree) for the existence of controls. It would also allow you to get out-from-under your Redim Preserve statements.

    3) I thought about possibly using API calls for the sizing/moving. That would circumvent all your ScaleMode problems. However, it would be difficult (if not impossible) to do that with light-weight controls (such as labels and drawing objects).

    4) LaVolpe gave you a way to check for the Frame control. However, don't forget that UserControls can also be containers, and they don't necessarily have to expose a ScaleMode property either. In fact, they may be a problem in that they can have a ScaleMode other than Twips, but not necessarily expose that fact.

    Cool stuff. Take Care,
    Elroy
    Last edited by Elroy; Jan 10th, 2019 at 06:58 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7
    gibra
    Guest

    Re: VB6 Control Anchoring and Docking made Easy

    Quote Originally Posted by SeabrookStan View Post
    Known issues are limitations when used on multiple Forms simultaneously,
    For this you should use a Class, instead of a BAS module.
    Last edited by gibra; Jan 12th, 2019 at 03:58 AM.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,525

    Re: VB6 Control Anchoring and Docking made Easy

    Hi SeabrookStan,

    Just to give you a bit more information, you could use the following to move things around:

    Code:
    
    Public Type RECT
        Left   As Long
        Top    As Long
        Right  As Long ' This is +1 (right - left = width)
        Bottom As Long ' This is +1 (bottom - top = height)
    End Type
    '
    Public Declare Function MoveWindow Lib "user32" (ByVal hWnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
    Public Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
    
    

    Using those would completely circumvent any concern with ScaleMode, and you'd always be working in pixels.

    However, the problem you'd have is with light-weight controls (for example, Labels, Lines, and Shapes). Those light-weight controls don't have a hWnd. Therefore, they're difficult to resize with API calls.

    For what you're doing, you may be better off to just "manage" the ScaleMode of the containers. In other words, save it, set it to some standard for you (say Twips), do your work, and then put it back to what it was. With that approach, you could still resize your light-weight controls.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9
    Addicted Member
    Join Date
    Jan 2010
    Posts
    250

    Re: VB6 Control Anchoring and Docking made Easy

    i really need this, gonna try it first, i know already one year thread..and i am still using VB

  10. #10
    Lively Member
    Join Date
    Aug 2023
    Posts
    82

    Re: VB6 Control Anchoring and Docking made Easy

    VB6 Control Anchor Module Demo V5.zipHi, really nice a simple module!

    I know it's an old thread but I've made some mods so now it works without the CTL array and works with control arrays, multiple forms, etc.

  11. #11
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    519

    Re: VB6 Control Anchoring and Docking made Easy

    It does not work if the form it set to vbPixels.
    I am still fighting to resolve that.

  12. #12
    Addicted Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    202

    Re: VB6 Control Anchoring and Docking made Easy

    Quote Originally Posted by tmighty2 View Post
    It does not work if the form it set to vbPixels.
    I am still fighting to resolve that.
    The solution is very simple, replace the code in resize event with:
    Code:
    Private Sub Form_Resize()
     Dim osm As Long: osm = Me.ScaleMode
      Me.ScaleMode = vbTwips
     'Useage Example:
     'Anchor ControlName, AnchorTop, AnchorLeft, AnchorRight, AnchorBottom
      Anchor Text1, False, False, True, True
      Anchor Command1, , , True, True
      Anchor Picture1, True, True, True, False
      Anchor Picture4, True, False, True, False
      Anchor Picture5, True, True, True
      Anchor Frame1, True, True, False, True
      Anchor Check1, False, True, False, True
      Anchor Text2, False, True, False, True
      Anchor Picture2, True, True, True, True
      Anchor Picture3, False, True, True, True
      Anchor Text3, True, , True, True
      Anchor Text4, False, True, False, True
      Anchor Command2, False, , True, True
      Anchor Label1(0), True, , True, True
      Anchor Label1(1), True, , True, True
      Anchor Label1(2), True, , True, True
      Me.ScaleMode = osm
    End Sub

  13. #13
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    519

    Re: VB6 Control Anchoring and Docking made Easy

    Here is first version that is class based hopefully drawscale independent.
    Attached Files Attached Files

  14. #14
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    519

    Re: VB6 Control Anchoring and Docking made Easy

    Thank you. I thought the problem was deeper, and I rewrote it. Perhaps I just wasted my time.
    Thank you for that quick fix.

  15. #15
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    519

    Re: VB6 Control Anchoring and Docking made Easy

    Sorry to ask back: Have you tried your code in production already? I don't like my code. It requires 2 classes while the original code does not even require 1...
    If your fix works, then I would prefer yours.

  16. #16
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    519

    Re: VB6 Control Anchoring and Docking made Easy

    Your fix does not work correctly if e. g. a picturebox is set to vbPixels.

  17. #17
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    519

    Re: VB6 Control Anchoring and Docking made Easy

    I had to change really much to get it working in all cases.
    Byproduct is the ability to preserve initial size and position and prevent going offscreen.

    If you should switch from an earlier version to this one, please note that I change the arguments in Sub Anchor:

    Previously: Top, Left, Bottom, Right
    Now: Left, Top, Bottom, Right

    Edit: I found one situation that made me rewrite it.
    In my previous version a situation was not covered where a form has WindowStateMaximized.
    My old code was not able to handle such an event.

    My code takes place in Form_Load now and uses the form as a private variable to get its resize event.

    Edit: I fixed a flaw that occured if a control has a hwnd.
    Attached Files Attached Files
    Last edited by tmighty2; Sep 22nd, 2024 at 06:12 PM.

  18. #18
    Addicted Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    202

    Re: VB6 Control Anchoring and Docking made Easy

    Quote Originally Posted by tmighty2 View Post
    Your fix does not work correctly if e. g. a picturebox is set to vbPixels.
    Thanks for reporting the bug.
    I have my own solution and it is working fine, but I never used Picturebox as container, and never changed the containers ScaleMode property to a value distinct of vbTwips, the atached file contains the versión with it fixed.
    My solutions uses a unique class and the tag property to specify the anchor type, sets the minimum form size to the desing size.
    Attached Files Attached Files

Tags for this Thread

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