Click here to Skip to main content
16,017,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Using VB 2008 Express.
I have a Main form that has a number of Group Boxes.

As desired, I can successfully position a Wait Cursor on a control on the Main Form that is NOT within a group box using the following code lines: (assume Button1 is NOT within a Group Box)

Cursor.Position = PointToScreen(Button1.Location)
Cursor = Cursors.WaitCursor
Application.DoEvents()

But, if Button1 is positioned within a Group Box (eg. GroupBox1), I have not discovered how to get the cursor over that button. Instead it gets placed at some other location on the Main Form that is not within GroupBox1.

I've tried variations of the following without success:

Cursor.Position = PointToScreen(Button1.Location), and
Cursor.Position = PointToScreen(GroupBox1.Button1.Location)

No errors are generated but the desired result is not obtained.
I believe it has to do with the fact that the location property of the button within the group box is relative to the group box upper left corner. But VB places the cursor as though button's location property were relative to the Main Form upper left corner.
Would appreciate guidance - Thanks, Mike

What I have tried:

Cursor.Position = PointToScreen(Button1.Location), and
Cursor.Position = PointToScreen(GroupBox1.Button1.Location)
Posted

When seeing your Solution I want to suggest you some little modifications :
VB
Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
    Cursor.Position = GetCenterOfControl(Button6)
End Sub

Function GetCenterOfControl(xControl As Control) As Point
    Dim p As New Point

    If xControl.Parent IsNot Nothing Then p = xControl.Parent.Location
    If xControl.Parent.Parent IsNot Nothing Then
        Dim f As Form = xControl.Parent.Parent
        Dim s As Size = GetFormMargin(f)
        p.X += f.Location.X + s.Width
        p.Y += f.Location.Y + s.Height
      End If
    p.X += xControl.Location.X + xControl.Width / 2
    p.Y += xControl.Location.Y + xControl.Height / 2

    Return p
End Function

Function GetFormMargin(f As Form) As Size
    Dim screenRectangle As Rectangle = f.RectangleToScreen(f.ClientRectangle)

    Dim s As New Size
    s.Height = screenRectangle.Top - f.Top
    s.Width = screenRectangle.Left - f.Left

    Return s
End Function


This code assumes that your Groupbox is directly placed on your Form and the Button is placed inside this Groupbox (Form - GroupBox - Button)
It also assumes that your Form as a FormBorderStyle selected.
If you have more than one Container then you have to iterate through your Control.Parents to find the base.

I hope you will find it interesting ...
 
Share this answer
 
v2
Comments
Member 16123720 13-Feb-24 13:03pm    
Ralf, Very nice. Includes more aspects than I had considered. Also, introduced me to the usage of keyword 'parent' which I had not encountered before.
One note though for others, if the button (or desired control) is on the parent, then the simpler Cursor.Position = PointToScreen(Button1.Location) seems to work just fine.
Ralf Meier 13-Feb-24 13:24pm    
You are welcome.
I would be nice if you rate my Solution also ...
0x01AA 13-Feb-24 14:22pm    
No rate from OP, but at least my small 5 ;)
Ralf Meier 13-Feb-24 14:53pm    
Thanks a lot ... :-)
The Location of a control is always relative to its container: if you have a GroupBox on a Form, and the GroupBox contains a Button, then myGroupBox.Location is relative to the top left corner of myForm.ClientRectangle, and myButton.Location is relative to the top left corner of myGroupBox.ClientRectangle.

So to identify the Location of myButton relative to myForm, you need to add myButton.Location to myGroupBox.Location - the result is relative to the top left corner of the Form.ClientArea. You can then use PointToScreen to identify where it is relative to the top left corner of the actual monitor.

Make sense?
 
Share this answer
 
Thanks to OriginalGriff and Ralf Meier...

Their guidance led me to the solution pasted below.
But first, I need to clarify my originally posted scenario. It seems that the code line below WILL work if there is no other form opened during the process, such as a Filedialog window for instance.
Cursor.Position = PointToScreen(Button1.Location)


But if another form will be opened during the process, even if it is before the Cursor.Position statement, the cursor position is not positioned properly to the button1 location within the groupbox.
There may be a simpler way to bring the orientation back from the opened window so that code line will work, but I haven't found it.

As a result, I worked out the following solution (Note: I am a hobbyist so there likely a more correct way to code this):

In the Class Statement the following was added:
Dim WaitCurPt As New Point(0, 0)


In the code where I wanted to set the cursor position, the below was added wherein the actual names of the desired Groupbox and Button are used (without any quotations around their Names):
Call WaitCursorPoint(GroupBox1, Button1)
Cursor.Position = PointToScreen(WaitCurPt)
Cursor = Cursors.WaitCursor
Application.DoEvents()


Finally, the following subroutine was added:
Private Sub WaitCursorPoint(ByVal grp As Object, ByVal Obj As Button)
    Dim x, y As Integer
    x = grp.Location.X
    y = grp.Location.Y
    x = x + Obj.Location.X
    y = y + Obj.Location.Y
    x = x + Obj.Size.Width / 2
    y = y + Obj.Size.Height / 2
    WaitCurPt.X = x
    WaitCurPt.Y = y
End Sub
 
Share this answer
 
Comments
Ralf Meier 11-Feb-24 5:14am    
based on this Solution I posted some little modifications for you - see below ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900