Poser une questionPoser une question
 

TraitéeControls.Add('fixed control type')

Réponses

  • dimanche 8 novembre 2009 13:29Olaf Rabbachin Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Hi there,

    actually, this sort of task is usually performed by means of a designer which is then applied to your UserControl. For instance, if you'd be developing your own TabControl, you'd only want to allow TabPages to be dropped into the TabControl-container. In this case, you would create a new class that inherits from ParentControlDesigner (which resides in the System.Windows.Forms.Design namespace). In this class, overriding the CanParent function and returning true only if the type of the control passed is on your "list" of allowed controls (like, for the TabControl, a TabPage) would do exactly what you want.
    The designer-class is also the place where you add all the stuff you might need in order to provide your UserControl-consumers with the so-called "design time experience", i.e. how drag'n'drop is performed, what properties might have to be removed or whether there should be that little arrow on your control that allows consumers to perform certain tasks (i.e., sticking with the TabControl, adding or removing TabPages).

    That said, at design-time, there wouldn't have to be an exception in the first place due to the fact that the control simply wouldn't accept anything but the desired control(s). It would trigger an exception at run-time though, i.e. if you were adding controls via code.


    Cheers,
    Olaf
  • dimanche 8 novembre 2009 05:24bdbodger Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
        Private Sub UserControl1_ControlAdded( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.ControlEventArgs) _
        Handles MyBase.ControlAdded
    
            If Not TypeOf e.Control Is Button Then
                Me.Controls.Remove(e.Control)
                MessageBox.Show("Button Controls only", _
                                "Control add error", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error)
            End If
    
        End Sub
    

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
  • dimanche 8 novembre 2009 05:29Omie Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée


        Private Sub UserControl1_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
            If Not TypeOf e.Control Is Button Then
                Me.Controls.Remove(e.Control)
                MsgBox("Invalid Control")
            End If
        End Sub

    Edit: stabbed by bdbodger

    lol, I wasted too much time editing posts

    Thanks

    My BlogMy FacebookYOUR Place to have fun time ! Awesome RPG Action Game

Toutes les réponses

  • dimanche 8 novembre 2009 02:17kaymaf Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Im not sure if this is what you looking for but try it
    Dim cont As Control
     If cont.GetType.Name = "Button" Then
     Me.Controls.Add(cont)
    Else
    MsgBox("invalid control,button only")
     End If

    kaymaf
    If that what you want, take it. If not, ignored it and no complain
  • dimanche 8 novembre 2009 05:24bdbodger Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
        Private Sub UserControl1_ControlAdded( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.ControlEventArgs) _
        Handles MyBase.ControlAdded
    
            If Not TypeOf e.Control Is Button Then
                Me.Controls.Remove(e.Control)
                MessageBox.Show("Button Controls only", _
                                "Control add error", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error)
            End If
    
        End Sub
    

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
  • dimanche 8 novembre 2009 05:29Omie Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée


        Private Sub UserControl1_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
            If Not TypeOf e.Control Is Button Then
                Me.Controls.Remove(e.Control)
                MsgBox("Invalid Control")
            End If
        End Sub

    Edit: stabbed by bdbodger

    lol, I wasted too much time editing posts

    Thanks

    My BlogMy FacebookYOUR Place to have fun time ! Awesome RPG Action Game
  • dimanche 8 novembre 2009 13:29Olaf Rabbachin Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Hi there,

    actually, this sort of task is usually performed by means of a designer which is then applied to your UserControl. For instance, if you'd be developing your own TabControl, you'd only want to allow TabPages to be dropped into the TabControl-container. In this case, you would create a new class that inherits from ParentControlDesigner (which resides in the System.Windows.Forms.Design namespace). In this class, overriding the CanParent function and returning true only if the type of the control passed is on your "list" of allowed controls (like, for the TabControl, a TabPage) would do exactly what you want.
    The designer-class is also the place where you add all the stuff you might need in order to provide your UserControl-consumers with the so-called "design time experience", i.e. how drag'n'drop is performed, what properties might have to be removed or whether there should be that little arrow on your control that allows consumers to perform certain tasks (i.e., sticking with the TabControl, adding or removing TabPages).

    That said, at design-time, there wouldn't have to be an exception in the first place due to the fact that the control simply wouldn't accept anything but the desired control(s). It would trigger an exception at run-time though, i.e. if you were adding controls via code.


    Cheers,
    Olaf