How to position modal form

Heiko

Contributor
Joined
Feb 10, 2003
Messages
430
Location
Holstein, Germany.
I have a custom DateTimePicker Control. This control will - upon clicking a buttom - show a MonthCalendar. As I can not resize the usercontrol I need to show the calendar on an extra form.

Much to my embarrasment, I can not figure out, how to have this extra form position exactly at that position where the button was clicked (upper left corner == mouse.position).

Here's my pathetic attempt ...

Visual Basic:
    Private Sub btnCalendar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnCalendar.MouseDown
        mLoc = Nothing  'mLoc is a private variable in my class
        mLoc = New Point(e.X, e.Y)
    End Sub

    Private Sub btnCalendar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalendar.Click

        Dim text as string
        Dim fCalendar As New PopupCalendar() 'a form with a calendar on it

        fCalendar.Size = fCalendar.Calendar.Size
        fCalendar.Location = mLoc


        text = Trim(Me.txtDate.Text)
        If text <> vbNullString Then
            If (IsDate(Me.txtDate.Text)) Then
                fCalendar.Calendar.SelectionStart = CDate(Me.txtDate.Text)
                fCalendar.Calendar.SelectionEnd = CDate(Me.txtDate.Text)
            Else
                fCalendar.Calendar.SelectionStart = Today()
                fCalendar.Calendar.SelectionEnd = Today()
            End If
        Else
            fCalendar.Calendar.SelectionStart = Today()
            fCalendar.Calendar.SelectionEnd = Today()
        End If


        fCalendar.ShowDialog(Me)

'fCalendar will hide when date is selected or esc is pressed ...
'strange: screen will flicker whenever fCalendar hides.

        If Not fCalendar.Cancelled Then 'cancelled/SelectedData are custom properties of the form class
            Me.txtDate.Text = fCalendar.SelectedDate
        End If

        fCalendar = Nothing

    End Sub
 
You can get the absolute position of the mouse cursor (which is what the location of your form should be set to) using Cursor.Position.
 
Back
Top