[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!
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
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
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
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
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!~
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:D
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