Answered by:
location of new form

Question
-
Hi
I want to click on button1 on form1 ,show the form2 at Indicated bythered box (left of button1 position and below it)
1- When the right button1 is enough space there to show (below)
2- When the right button1 is not enough space , form2 most be show at left of button1 (bellow)
i write a below code on form2_load , but it not work carefully
Me.Location = New Point(form1.button1.location)
can you help me to write code
thanks
- Edited by Ashkan209 Monday, April 21, 2014 5:08 AM
Monday, April 21, 2014 5:07 AM
Answers
-
thanks
it was work carefully ,
BUT
WhenI put the Button1 into a Groupbox , code does not work properly
Hi, you can use button1.Parent.PointToScreen to get the correct location if you put it into a GroupBox or other containers.
Private Sub form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim x As Integer = form1.button1.Parent.PointToScreen(form1.button1.Location).X Dim y As Integer = form1.button1.Parent.PointToScreen(form1.button1.Location).Y + form1.button1.Height If x + Me.Width > Screen.PrimaryScreen.WorkingArea.Width Then 'When the right button1 is not enough space , form2 most be show at left of button1 x = x + form1.button1.Width - Me.Width End If Me.Location = New Point(x, y) End Sub
- Marked as answer by Ashkan209 Monday, April 21, 2014 9:19 AM
Monday, April 21, 2014 8:37 AM
All replies
-
I would guess you may be able to figure out how to do that with this code. Or maybe not.
Anyhow this moves Form1 when Form2 is shown so that your issues don't occur. But it can be altered to do the other stuff probably.
Form1 code.
Option Strict On Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.CenterToScreen() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Form2.Show() End Sub End Class
Form2 code.
Option Strict On Public Class Form2 Dim Form1TitlebarHeight As Integer = 0 Dim Form1ABordersWidth As Integer = 0 Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load Form1ABordersWidth = CInt((Form1.Width - Form1.ClientSize.Width) / 2) Form1TitlebarHeight = Form1.Height - Form1.ClientSize.Height - 2 * Form1ABordersWidth Form1.WindowState = FormWindowState.Normal Form1.Top = 0 Form1.Left = CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Form1.Width / 2)) Me.Left = CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)) Me.Top = Form1.Button1.Bottom + Form1TitlebarHeight + Form1ABordersWidth End Sub End Class
Hello. I'm old and retired. I like to program if you could call what I do programming. However I'd like to code for you! If you've got the dime then I've got the time. Call me, landline, @ BR-549.
- Edited by Mr. Monkeyboy Monday, April 21, 2014 5:50 AM
Monday, April 21, 2014 5:25 AM -
Hi, this is the sample code:
Private Sub form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim x As Integer = form1.PointToScreen(form1.button1.Location).X Dim y As Integer = form1.PointToScreen(form1.button1.Location).Y + form1.button1.Height If x + Me.Width > Screen.PrimaryScreen.WorkingArea.Width Then 'When the right button1 is not enough space , form2 most be show at left of button1 x = x + form1.button1.Width - Me.Width End If Me.Location = New Point(x, y) End Sub
Monday, April 21, 2014 5:53 AM -
thanks
it was work carefully ,
BUT
WhenI put the Button1 into a Groupbox , code does not work properly
Monday, April 21, 2014 7:46 AM -
thanks
it was work carefully ,
BUT
WhenI put the Button1 into a Groupbox , code does not work properly
Well once a control is placed on another control then it becomes that controls control AFAIK. So information about it changes. Like its top is now the distance between the control it is ons top and its top. No longer the distance between the Forms top and its top. Same with Left, Right, Bottom, etc.
When you place the Button in a GroupBox the code works correctly as it was designed to do. It's up to you, the programmer, to resolve what you think is wrong.
Regardless you should propose the post or posts that answered your question as the answer or answers. And any post that was not the answer but assisted you then you should vote it.
Hello. I'm old and retired. I like to program if you could call what I do programming. However I'd like to code for you! If you've got the dime then I've got the time. Call me, landline, @ BR-549.
- Edited by Mr. Monkeyboy Monday, April 21, 2014 8:12 AM
Monday, April 21, 2014 8:11 AM -
thanks
it was work carefully ,
BUT
WhenI put the Button1 into a Groupbox , code does not work properly
Hi, you can use button1.Parent.PointToScreen to get the correct location if you put it into a GroupBox or other containers.
Private Sub form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim x As Integer = form1.button1.Parent.PointToScreen(form1.button1.Location).X Dim y As Integer = form1.button1.Parent.PointToScreen(form1.button1.Location).Y + form1.button1.Height If x + Me.Width > Screen.PrimaryScreen.WorkingArea.Width Then 'When the right button1 is not enough space , form2 most be show at left of button1 x = x + form1.button1.Width - Me.Width End If Me.Location = New Point(x, y) End Sub
- Marked as answer by Ashkan209 Monday, April 21, 2014 9:19 AM
Monday, April 21, 2014 8:37 AM