Results 1 to 12 of 12

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

  1. #1

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

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

    I am working on a UserControl and I am looking for a way to prevent developer to place this UserControl more than one time into a form, in Design Time. In other words, how can I detect if my UserControl is already placed into ParentForm or not, in Design Time and prevent the second placement if there is already one there?

    I tried something like this example below... First I am not sure if this is the "correct" way and second, I can't find how to remove or stop the placement of UserControl in case there is already one.

    Again, all this, in Design Time!!!

    vb.net Code:
    1. Private Sub MyUserControl_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     Dim _Count As Integer
    3.     Dim _UserControl As MyUserControl
    4.     For Each _UserControl In Me.ParentForm.Controls
    5.         If _UserControl.Name.Contains("MyUserControl") Then
    6.             _Count += 1
    7.         End If
    8.     Next
    9.     If _Count > 1 Then
    10.         MsgBox("Control have been placed.")
    11.     Else
    12.         MsgBox("Control haven't placed yet.")
    13.     End If
    14. End Sub
    Last edited by Simonetos The Greek; Apr 17th, 2018 at 06:10 AM.

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

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

    Try this:

    VB.Net Code:
    1. Private Sub Me_ParentChanged(sender As Object, e As EventArgs) Handles Me.ParentChanged
    2.     Dim f = DirectCast(Me.FindForm, Control)
    3.     If f IsNot Nothing Then
    4.         'check to see if we are the only one on the form
    5.         Dim c = f
    6.         Do Until c Is Nothing
    7.             If TypeOf c Is UserControl1 AndAlso c IsNot Me Then
    8.                 Throw New NotSupportedException("You cannot have more than one of these controls on your form at a time")
    9.             End If
    10.             c = f.GetNextControl(c, True)
    11.         Loop
    12.     End If
    13. End Sub
    Change UserControl1 to the name of your control...

    The above will work ... but...
    ... You may want to show a message box before you throw the exception for an easier to understand message...
    ... I don't know of a way to cancel the control creation and only show a nice handled error though...
    If you show your own message you can also choose to set Me.Parent = Nothing instead of throwing the exception to not insert the control ... but it also shows another error after stating: "'child' is not a child control of this parent"

    Kris

  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

    Thank you very much my friend for your answer, it worked!!! Here is how I used your example with some changes...
    vb.net Code:
    1. Private Sub Me_ParentChanged(sender As Object, e As EventArgs) Handles Me.ParentChanged
    2.         If Me.DesignMode AndAlso 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 MyUserControl 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
    One more question...
    vb.net Code:
    1. Private Sub Me_ParentChanged(sender As Object, e As EventArgs) Handles Me.ParentChanged
    2.     'Blah blah blah...
    3. End Sub
    and
    vb.net Code:
    1. Protected Overrides Sub OnParentChanged(e As EventArgs)[INDENT]MyBase.OnParentChanged(e)
    2.     'Blah blah blah...
    3. End Sub
    is the same thing?
    Last edited by Simonetos The Greek; Apr 17th, 2018 at 06:07 AM.

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

    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

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

  6. #6

    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.

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

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

  8. #8

    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.

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

    Re: [RESOLVED] Prevent developer from placing a UserControl more than one time in Des

    What is f and what is c? If f is the ParentForm and c the UserControl, then why c=f?
    That confused me too but it's not necessarily wrong. It means that the first pass through the loop will check that the user control isn't the form and I can't think why you'd do that. But subsequent passes through the loop will check the controls because of this line within the loop: c = f.GetNextControl(c, True).

    So at worst i00's code will do an extra pointless iteration but will still check the controls correctly. At best, he's thought of something I've missed and there's a good reason for checking against the form itself on that first pass.

    Personally I think i00's solution is correct except that that one line should be Dim c = f.GetNextControl(c, True)
    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

  10. #10

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

    Re: [RESOLVED] Prevent developer from placing a UserControl more than one time in Des

    Quote Originally Posted by FunkyDexter View Post
    That confused me too but it's not necessarily wrong. It means that the first pass through the loop will check that the user control isn't the form and I can't think why you'd do that. But subsequent passes through the loop will check the controls because of this line within the loop: c = f.GetNextControl(c, True).

    So at worst i00's code will do an extra pointless iteration but will still check the controls correctly. At best, he's thought of something I've missed and there's a good reason for checking against the form itself on that first pass.

    Personally I think i00's solution is correct except that that one line should be Dim c = f.GetNextControl(c, True)
    Let's see if he will give us a logical explanation like yours!!!

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

    Re: [RESOLVED] Prevent developer from placing a UserControl more than one time in Des

    Ah, just had a thought. GetNextControl gets the next control in tab order after the control passed in, in this case c. On the first pass c won't be anything. By setting it to the form he gives it a start point.

    Personally, I would just chance the Do Until loop to a For Each loop. Again, I'm afraid I don't have an ide in front of me but it should be something like:-
    vb.net Code:
    1. For Each c as Control in f.controls
    2.    'check it's not your user control
    3. Next
    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

  12. #12

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

    Re: [RESOLVED] Prevent developer from placing a UserControl more than one time in Des

    Quote Originally Posted by FunkyDexter View Post
    Ah, just had a thought. GetNextControl gets the next control in tab order after the control passed in, in this case c. On the first pass c won't be anything. By setting it to the form he gives it a start point.

    Personally, I would just chance the Do Until loop to a For Each loop. Again, I'm afraid I don't have an ide in front of me but it should be something like:-
    vb.net Code:
    1. For Each c as Control in f.controls
    2.    'check it's not your user control
    3. Next
    I think you are right my friend!!! I tried this and works 100%...
    vb.net Code:
    1. Dim _ParentForm = DirectCast(Me.FindForm, Control)
    2. If _ParentForm IsNot Nothing Then
    3.     For Each _Control As Control In _ParentForm.Controls
    4.         If TypeOf _Control Is MyUserControl AndAlso _Control IsNot Me Then
    5.             Throw New ArgumentOutOfRangeException("", "You can place only one " & _Control.Name & " control per form.")
    6.         End If
    7.         _Control = _ParentForm.GetNextControl(_Control, True)
    8.     Next
    9. End If
    Last edited by Simonetos The Greek; Apr 17th, 2018 at 06:14 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