Results 1 to 10 of 10

Thread: VB6 Control Anchoring and Docking made Easy

  1. #1

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

    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
    Junior Member
    Join Date
    Jun 2018
    Location
    New Orleans, Austin, Santa Monica
    Posts
    29

    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
    9,853

    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
    9,853

    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
    Member
    Join Date
    Aug 2023
    Posts
    47

    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.

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