none
UserControl does not show its controls when changing UC size in a Form designer RRS feed

  • Question

  • 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


    • Edited by Claudio111 Monday, March 4, 2019 7:23 PM
    Monday, March 4, 2019 7:22 PM

Answers

  • I found the problem. !

    I don't see the textbox and button just becaouse I change their position in a wrong way in the Format Property

      TextBox1.Location = New Point(Me.Location.X, Me.Location.Y + 2)

    But the TextBox X location refers to UC container so if  Me.Location.X  = 100 the TextBox Location X goes to 100 inside UC

    The right way should be

      TextBox1.Location = New Point(0, 2)

    So TextBox goes to X = 0 and Y = 2  inside the UC container

    Same thing for Location.Y

    :-)))

    • Marked as answer by Claudio111 Tuesday, March 5, 2019 12:55 PM
    Tuesday, March 5, 2019 12:55 PM

All replies

  • Hi,

    You should not change the size of UC,try to comment the following two lines of code.

      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(200, Me.Height)
                    TextBox1.Size = New Size(130, TextBox1.Size.Height)
                Else
                    'Me.Size = New Size(160, 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

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 5, 2019 2:07 AM
  • Hi Alex

    Even if I comment the lines you suggest I get the same problem.

    Tuesday, March 5, 2019 8:09 AM
  • Hi,

    I tried your code,it works well.Do you regenerate the dll and reference?The code you provided is less "property".

    <Browsable(true)>
        Public property TextDate As String
            Get
                Return TextBox1.Text
            End Get
            Set(value As String)
                TextBox1.Text = value
            End Set
        End Property

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 5, 2019 8:17 AM
  • I found the problem. !

    I don't see the textbox and button just becaouse I change their position in a wrong way in the Format Property

      TextBox1.Location = New Point(Me.Location.X, Me.Location.Y + 2)

    But the TextBox X location refers to UC container so if  Me.Location.X  = 100 the TextBox Location X goes to 100 inside UC

    The right way should be

      TextBox1.Location = New Point(0, 2)

    So TextBox goes to X = 0 and Y = 2  inside the UC container

    Same thing for Location.Y

    :-)))

    • Marked as answer by Claudio111 Tuesday, March 5, 2019 12:55 PM
    Tuesday, March 5, 2019 12:55 PM