Results 1 to 12 of 12

Thread: [RESOLVED] Prevent developer from placing a UserControl more than one time in Design Time

Hybrid View

  1. #1
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Prevent developer from placing a UserControl more than one time in Design Time

    I don't know of a way to cancel the control creation
    Haven't got an ide in front of me to check but you could presumably just remove it:-
    Code:
    f.Controls.Remove(Me)
    By the way, I think there's a typo in your code:-
    Code:
    Dim c = f
    Should be:-
    Code:
    Dim c = f.GetNextControl(c, True)
    Unless you actually want to check the control isn't the form for some reason


    edit> crossed over

    You may or may not want this:-
    If Me.DesignMode
    While that's in they could add a second control to the form at run time.

    is the same thing?
    Not quite. The "On" methods fire the events and are the intended method for firing the event from code, rather than calling the handling event directly. The correct way to wire up an event handler is the first one.
    Last edited by FunkyDexter; Apr 16th, 2018 at 09:55 AM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  2. #2

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: Prevent developer from placing a UserControl more than one time in Design Time

    Quote Originally Posted by FunkyDexter View Post
    You may or may not want this:-
    If Me.DesignMode
    While that's in they could add a second control to the form at run time.
    Do you mean if developer try to add this control programmatically?

  3. #3

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: Prevent developer from placing a UserControl more than one time in Design Time

    Quote Originally Posted by FunkyDexter View Post
    You may or may not want this...
    So I ended up on this...
    vb.net Code:
    1. Private Sub Me_ParentChanged(sender As Object, e As EventArgs) Handles Me.ParentChanged
    2.     If Me.ParentForm IsNot Nothing Then
    3.         Dim _UserControl = DirectCast(Me.ParentForm, Control)
    4.         If _UserControl IsNot Nothing Then
    5.             Do Until _UserControl Is Nothing
    6.                 If TypeOf _UserControl Is CmosTitleBar AndAlso _UserControl IsNot Me Then
    7.                     Throw New ArgumentOutOfRangeException("", "You can place only one " & _UserControl.Name & " control per form.")
    8.                 End If
    9.                 _UserControl = _UserControl.GetNextControl(_UserControl, True)
    10.             Loop
    11.         End If
    12.     End If
    13. End Sub
    Last edited by Simonetos The Greek; Apr 17th, 2018 at 06:00 AM.

  4. #4
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: Prevent developer from placing a UserControl more than one time in Design Time

    Quote Originally Posted by Simonetos The Greek View Post
    So I ended up on this...
    Code:
    Private Sub Me_ParentChanged(sender As Object, e As EventArgs) Handles Me.ParentChanged
            If Me.ParentForm IsNot Nothing Then
                Dim _UserControl = DirectCast(Me.ParentForm, Control)
                If _UserControl IsNot Nothing Then
                    Do Until _UserControl Is Nothing
                        If TypeOf _UserControl Is CmosTitleBar AndAlso _UserControl IsNot Me Then
                            Throw New ArgumentOutOfRangeException("", "You can place only one " & _UserControl.Name & " control per form.")
                        End If
                        _UserControl = _UserControl.GetNextControl(_UserControl, True)
                    Loop
                End If
            End If
        End Sub
    This is incorrect : _UserControl = _UserControl.GetNextControl(_UserControl, True)
    ... it will only go into controls... look @ my first example...

  5. #5

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: Prevent developer from placing a UserControl more than one time in Design Time

    Quote Originally Posted by i00 View Post
    This is incorrect : _UserControl = _UserControl.GetNextControl(_UserControl, True)
    ... it will only go into controls... look @ my first example...
    Yes but I can't understand this part
    vb.net Code:
    1. Dim c = f
    What is f and what is c? If f is the ParentForm and c the UserControl, then why c=f?
    Last edited by Simonetos The Greek; Apr 17th, 2018 at 06:09 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width