C
Claudio111
Guest
Hi all
I have create a user control with a TextBox and a Button as a DateTimePicker that allows me to handle backcolor, readonly and other graphics
When the button is clicked a MonthCalendar is shown in the UserControl Parent Form and the date selected is copied to the Textbox.text
It works well.
This is the UC code
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Public Class MyDate
Private datascelta As String
Private ButtonState As String = "NO"
Private ParentControl As Object
Private WithEvents MC1 As MonthCalendar
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
mFormat = 1
MC1 = New MonthCalendar
Button1.FlatStyle = FlatStyle.Flat
Button1.FlatAppearance.BorderColor = Color.DimGray
Button1.FlatAppearance.MouseDownBackColor = Color.Transparent
Button1.FlatAppearance.MouseOverBackColor = Color.Transparent
Button1.FlatAppearance.BorderSize = 0
End Sub
<Browsable(true)>
Public TextDate As String
Get
Return TextBox1.Text
End Get
Set(value As String)
TextBox1.Text = value
End Set
End Property
Public Enum FormatType
ShortFormat = 1
LongFormat = 2
End Enum
Private mFormat As FormatType
Public Property Format As FormatType
Get
Return mFormat
End Get
Set(value As FormatType)
mFormat = value
If mFormat = 2 Then
Me.Size = New Size(160, Me.Height)
TextBox1.Size = New Size(130, TextBox1.Size.Height)
Else
Me.Size = New Size(100, Me.Height)
TextBox1.Size = New Size(70, TextBox1.Size.Height)
End If
TextBox1.Location = New Point(Me.Location.X, Me.Location.Y + 2)
Button1.Location = New Point(TextBox1.Location.X + TextBox1.Size.Width + 5, TextBox1.Location.Y)
End Set
End Property
Private Sub Me_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick
TextBox1.Focus()
End Sub
' add monthcalendar to Form
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ButtonState = "NO" Then
MC1.MaxSelectionCount = 1
SetMCLocation()
ParentControl = Me.Parent
ParentControl.Controls.Add(MC1)
MC1.BringToFront()
MC1.Visible = True
ButtonState = "SI"
Return
End If
If ButtonState = "SI" Then
ParentControl.controls.Remove(MC1)
ButtonState = "NO"
End If
End Sub
' draw a rectangle around the button
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
Dim bloc = Button1.Location
Dim bsize = Button1.Size
Dim rect As New Rectangle(bloc.X - 1, bloc.Y - 1, bsize.Width + 2, bsize.Height + 2)
Dim mpen = New Pen(Color.DimGray, 1)
e.Graphics.DrawRectangle(mpen, rect)
End Sub
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
ButtonState = "NO"
ParentControl = Me.Parent
ParentControl.controls.Remove(MC1)
End Sub
Private Sub MyDate_Leave(sender As Object, e As EventArgs) Handles Me.Leave
ButtonState = "NO"
ParentControl = Me.Parent
ParentControl.controls.Remove(MC1)
End Sub
' update the date selected to Textbox1.text
Private Sub MC1_DateChanged(sender As Object, e As DateRangeEventArgs) Handles MC1.DateChanged
If Format = 2 Then
datascelta = MC1.SelectionRange.Start.ToLongDateString
Else
datascelta = MC1.SelectionRange.Start.ToShortDateString
End If
TextBox1.Text = datascelta
TextBox1.Focus()
TextBox1.SelectionStart = TextBox1.Text.Length
ParentControl.controls.Remove(MC1)
ButtonState = "NO"
End Sub
' set location for MonthCalendar checking if it goes out of the Form
Private Sub SetMCLocation()
' si deve usare il Width di MC1 e non la size che e' inferiore se non entra nel form
Dim MCrigthmargin = Me.Location.X + 230
Dim Parentrigthmargin = Me.Parent.Size.Width
Dim MarginDiff = MCrigthmargin - Parentrigthmargin
Dim loc = Me.Location.X - MarginDiff
If MarginDiff > 0 Then
MC1.Location = New Point(loc, Me.Location.Y + 25)
Else
MC1.Location = New Point(Me.Location.X, Me.Location.Y + 25)
End If
End Sub
End Class
The problem is the following :
I add the UC to a simple Form and I see the UC with its Textbox and Button as the image shown above
Now in the Form Designer I change the UC Format Property to 'LongFormat' ( to get a Long format from Month Calendar) Doing so the UC appears as the following image in the form. I don't see textBox and button anymore, even if at run time I see both of them
How to do to avoid this
Thanks for help
Continue reading...
I have create a user control with a TextBox and a Button as a DateTimePicker that allows me to handle backcolor, readonly and other graphics
When the button is clicked a MonthCalendar is shown in the UserControl Parent Form and the date selected is copied to the Textbox.text
It works well.
This is the UC code
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Public Class MyDate
Private datascelta As String
Private ButtonState As String = "NO"
Private ParentControl As Object
Private WithEvents MC1 As MonthCalendar
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
mFormat = 1
MC1 = New MonthCalendar
Button1.FlatStyle = FlatStyle.Flat
Button1.FlatAppearance.BorderColor = Color.DimGray
Button1.FlatAppearance.MouseDownBackColor = Color.Transparent
Button1.FlatAppearance.MouseOverBackColor = Color.Transparent
Button1.FlatAppearance.BorderSize = 0
End Sub
<Browsable(true)>
Public TextDate As String
Get
Return TextBox1.Text
End Get
Set(value As String)
TextBox1.Text = value
End Set
End Property
Public Enum FormatType
ShortFormat = 1
LongFormat = 2
End Enum
Private mFormat As FormatType
Public Property Format As FormatType
Get
Return mFormat
End Get
Set(value As FormatType)
mFormat = value
If mFormat = 2 Then
Me.Size = New Size(160, Me.Height)
TextBox1.Size = New Size(130, TextBox1.Size.Height)
Else
Me.Size = New Size(100, Me.Height)
TextBox1.Size = New Size(70, TextBox1.Size.Height)
End If
TextBox1.Location = New Point(Me.Location.X, Me.Location.Y + 2)
Button1.Location = New Point(TextBox1.Location.X + TextBox1.Size.Width + 5, TextBox1.Location.Y)
End Set
End Property
Private Sub Me_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick
TextBox1.Focus()
End Sub
' add monthcalendar to Form
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ButtonState = "NO" Then
MC1.MaxSelectionCount = 1
SetMCLocation()
ParentControl = Me.Parent
ParentControl.Controls.Add(MC1)
MC1.BringToFront()
MC1.Visible = True
ButtonState = "SI"
Return
End If
If ButtonState = "SI" Then
ParentControl.controls.Remove(MC1)
ButtonState = "NO"
End If
End Sub
' draw a rectangle around the button
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
Dim bloc = Button1.Location
Dim bsize = Button1.Size
Dim rect As New Rectangle(bloc.X - 1, bloc.Y - 1, bsize.Width + 2, bsize.Height + 2)
Dim mpen = New Pen(Color.DimGray, 1)
e.Graphics.DrawRectangle(mpen, rect)
End Sub
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
ButtonState = "NO"
ParentControl = Me.Parent
ParentControl.controls.Remove(MC1)
End Sub
Private Sub MyDate_Leave(sender As Object, e As EventArgs) Handles Me.Leave
ButtonState = "NO"
ParentControl = Me.Parent
ParentControl.controls.Remove(MC1)
End Sub
' update the date selected to Textbox1.text
Private Sub MC1_DateChanged(sender As Object, e As DateRangeEventArgs) Handles MC1.DateChanged
If Format = 2 Then
datascelta = MC1.SelectionRange.Start.ToLongDateString
Else
datascelta = MC1.SelectionRange.Start.ToShortDateString
End If
TextBox1.Text = datascelta
TextBox1.Focus()
TextBox1.SelectionStart = TextBox1.Text.Length
ParentControl.controls.Remove(MC1)
ButtonState = "NO"
End Sub
' set location for MonthCalendar checking if it goes out of the Form
Private Sub SetMCLocation()
' si deve usare il Width di MC1 e non la size che e' inferiore se non entra nel form
Dim MCrigthmargin = Me.Location.X + 230
Dim Parentrigthmargin = Me.Parent.Size.Width
Dim MarginDiff = MCrigthmargin - Parentrigthmargin
Dim loc = Me.Location.X - MarginDiff
If MarginDiff > 0 Then
MC1.Location = New Point(loc, Me.Location.Y + 25)
Else
MC1.Location = New Point(Me.Location.X, Me.Location.Y + 25)
End If
End Sub
End Class
The problem is the following :
I add the UC to a simple Form and I see the UC with its Textbox and Button as the image shown above
Now in the Form Designer I change the UC Format Property to 'LongFormat' ( to get a Long format from Month Calendar) Doing so the UC appears as the following image in the form. I don't see textBox and button anymore, even if at run time I see both of them
How to do to avoid this
Thanks for help
Continue reading...