Results 1 to 7 of 7

Thread: [RESOLVED] How to position a form over a specific area on another form?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [RESOLVED] How to position a form over a specific area on another form?

    Hi there folks! I am working on a program to teach students words for school. I've added a onscreen keyboard, so a teacher can easily add new words if needed. I made my own because the only thing needed was letters and a couple of symbols. Anyway, what I would like to do is when the keyboard form is loaded, have it load where its position is in the bottom left corner of the main form (so it is overlapping the main form that is loaded previously). Is there something I can code in the formload event for the keyboard so it could do that?

    Thanks a lot!

  2. #2
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: How to position a form over a specific area on another form?

    You can place a small invisible label where it's upper left corner is the same position where you want to place the keyboard form. When you are ready to load the keyboard Form just move it to Label.Left and Label.Top


    Keyboard.Move Label.Left, Label.Top
    Keyboard.Show
    Last edited by I Love VB6; Mar 8th, 2017 at 12:35 PM.

  3. #3
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: How to position a form over a specific area on another form?

    Actually, you don't even need the label

    Keyboard.Move Me.Left, Me.Top + n
    Keyboard.Show

    where n is the offset from the top of your main Form to the position where you want the top of the Keyboard Form

    Note that you do not need the Load Form statement as the .Show does that for you

    Now, if you move the main Form you will need to re-position the other Form

    EDIT:

    You may want to make the other Form always on top - there's an API for that

    Also, you may want to put the the code in the mouse move event of the main Form

    Code:
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     KeyBoard.Move Me.Left, Me.Top + n
     KeyBoard.Show
    End Sub
    I'm not going to go into detail about it but you can get more elaborate and have the keyboard Form move when you move the main Form but that could involve sub-classing which I really don't have the time right now to post

    EDIT: Magic's post #5 does exactly that without subclassing
    Last edited by I Love VB6; Mar 8th, 2017 at 01:24 PM.

  4. #4
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: How to position a form over a specific area on another form?

    You are probably going to want the Keyboard Form to stay on top of the Main Form so you need to Show it specifying the Owner parameter.
    Given that the simplest code will probably be something like;

    Code:
    Private Sub CbShowKB_Click()
    
        KbForm.Move MainForm.Left, MainForm.Top + MainForm.Height - KbForm.Height
        KbForm.Show False, MainForm
        
    End Sub
    Placing Forms exactly relative to each other can however get more complicated if they have different Border Styles ref. http://www.vbforums.com/showthread.p...g-Aero-Borders

  5. #5
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: How to position a form over a specific area on another form?

    If you would like the KeyBoard Form to move when the Main Form moves try something like this;

    Code:
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    
    Private Sub Form_Load()
    
        SetParent KbForm.hWnd, MainForm.hWnd
    
    End Sub
    
    Private Sub CbShowKB_Click()
    
        KbForm.Move 0, MainForm.ScaleHeight - KbForm.Height
        KbForm.Show False
        
    End Sub

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: How to position a form over a specific area on another form?

    Thank you all for your help! These are fantastic ideas. The students are going to love it!~

  7. #7
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: [RESOLVED] How to position a form over a specific area on another form?

    Very nice, Magic. Now if you will excuse me I need to go and learn VB

    One thing I will say: you need to close the keyboard Form when you close the app otherwise it will remain hanging

    Code:
    Private Sub Form_Unload(Cancel As Integer)
     Unload KeyBoard
    End Sub
    Last edited by I Love VB6; Mar 8th, 2017 at 01:28 PM.

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