Microsoft Developer Network >
Página principal de foros
>
Visual Basic General
>
Controls.Add('fixed control type')
Controls.Add('fixed control type')
I've make a UserControl and want only buttons can be added to its ControlCollection, if others controls are added, it will throw an exception.
Can this work?
Respuestas
- 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- Marcado como respuestaNet Developer lunes, 09 de noviembre de 2009 5:24
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 .- Marcado como respuestaNet Developer lunes, 09 de noviembre de 2009 5:24
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 Blog ♦ My Facebook ♦ YOUR Place to have fun time ! ♦ Awesome RPG Action Game- Marcado como respuestaNet Developer lunes, 09 de noviembre de 2009 5:24
Todas las respuestas
- 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 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 .- Marcado como respuestaNet Developer lunes, 09 de noviembre de 2009 5:24
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 Blog ♦ My Facebook ♦ YOUR Place to have fun time ! ♦ Awesome RPG Action Game- Marcado como respuestaNet Developer lunes, 09 de noviembre de 2009 5:24
- 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- Marcado como respuestaNet Developer lunes, 09 de noviembre de 2009 5:24

