Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 ...

 

   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

.nerd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...