none
new control add to form RRS feed

  • Question

  • i put  textbox and label and button in the form

    I want to make sure they are there item and if there is another element, they are deleted
    During the work of the program

    Monday, February 26, 2018 8:23 PM

Answers

  • This is a rather strange requirement, but the best I can think of would be a custom Form that responds to the OnControlAdded method:

    Public Class LimitedControlForm
        Inherits Form
    
        Protected Overrides Sub OnControlAdded(e As ControlEventArgs)
            MyBase.OnControlAdded(e)
            Select Case e.Control.GetType
                Case GetType(Button)
                    If Me.Controls.OfType(Of Button).Count > 1 Then Throw New ControlNotAllowedException
                Case GetType(Label)
                    If Me.Controls.OfType(Of Label).Count > 1 Then Throw New ControlNotAllowedException
                Case GetType(TextBox)
                    If Me.Controls.OfType(Of TextBox).Count > 1 Then Throw New ControlNotAllowedException
                Case Else
                    Throw New ControlNotAllowedException
            End Select
        End Sub
    
        Public Class ControlNotAllowedException
            Inherits Exception
            Public Sub New()
                MyBase.New("The control already exists or cannot be added.")
            End Sub
        End Class
    End Class

    You would then have to manually edit the Form.Designer.Vb file for a form to inherit from this new limited form:

    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
    Partial Class Form2
        Inherits LimitedControlForm
    
        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()>
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    ...
    Now if you try to add more than 1 allowed control, or a control which is not allowed, during design time, the designer will throw an error message and not allow the new control.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by monemas Wednesday, March 28, 2018 2:05 PM
    Tuesday, February 27, 2018 4:44 PM
    Moderator

All replies

  • Hi

    Maybe this:

    ' Form1 with TextBox1, Label1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim NewControl As New CheckBox
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	If Not IsItAlreadyThere(TextBox1) Then
    	  MessageBox.Show("TextBox1 not there")
    	Else
    	  MessageBox.Show("TextBox1 already there")
    	End If
    	If Not IsItAlreadyThere(Label1) Then
    	  MessageBox.Show("Label1 not there")
    	Else
    	  MessageBox.Show("Label1 already there")
    	End If
    	If Not IsItAlreadyThere(Button1) Then
    	  MessageBox.Show("Button1 not there")
    	Else
    	  MessageBox.Show("Button1 already there")
    	End If
    	If Not IsItAlreadyThere(NewControl) Then
    	  MessageBox.Show("NewControl not there")
    	  With NewControl
    		.Location = New Point(2, 2)
    		.Text = "NewControl"
    		.Checked = True
    	  End With
    	  Controls.Add(NewControl)
    	  MessageBox.Show("NewControl added")
    	Else
    	  MessageBox.Show("NewControl already there")
    	End If
      End Sub
      Function IsItAlreadyThere(cont As Control) As Boolean
    	For Each c As Control In Controls
    	  If c.Name = cont.Name Then Return True
    	Next
    	Return False
      End Function
    End Class


    Regards Les, Livingston, Scotland

    Monday, February 26, 2018 8:36 PM
  • Hi

    Maybe this:

    ' Form1 with TextBox1, Label1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim NewControl As New CheckBox
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	If Not IsItAlreadyThere(TextBox1) Then
    	  MessageBox.Show("TextBox1 not there")
    	Else
    	  MessageBox.Show("TextBox1 already there")
    	End If
    	If Not IsItAlreadyThere(Label1) Then
    	  MessageBox.Show("Label1 not there")
    	Else
    	  MessageBox.Show("Label1 already there")
    	End If
    	If Not IsItAlreadyThere(Button1) Then
    	  MessageBox.Show("Button1 not there")
    	Else
    	  MessageBox.Show("Button1 already there")
    	End If
    	If Not IsItAlreadyThere(NewControl) Then
    	  MessageBox.Show("NewControl not there")
    	  With NewControl
    		.Location = New Point(2, 2)
    		.Text = "NewControl"
    		.Checked = True
    	  End With
    	  Controls.Add(NewControl)
    	  MessageBox.Show("NewControl added")
    	Else
    	  MessageBox.Show("NewControl already there")
    	End If
      End Sub
      Function IsItAlreadyThere(cont As Control) As Boolean
    	For Each c As Control In Controls
    	  If c.Name = cont.Name Then Return True
    	Next
    	Return False
      End Function
    End Class


    Regards Les, Livingston, Scotland

    i wanted any control found else three control (label ,text,button) removed it or end my program

    Monday, February 26, 2018 8:51 PM
  • Hi

    Sorry, I do not understand.


    Regards Les, Livingston, Scotland

    Monday, February 26, 2018 9:15 PM
  • Hi

    Sorry, I do not understand.


    Regards Les, Livingston, Scotland

    i wanted any control found else three control (label ,text,button) removed it or end my program

    if a add for example button 2 or any control label ,picture,listbox, any thing to the form remove it from form or end my program

    Unlike the previous three items, the name of the new item appears and is deleted or end

    Monday, February 26, 2018 9:29 PM
  • Hi

    Well, still very difficult to understand.

    If you have TextBox1,Label1 and Button1 on the Form, and another control of any type is added to the Form then you want the program to End - is that correct?


    Regards Les, Livingston, Scotland

    Monday, February 26, 2018 10:08 PM
  • if a add for example button 2 or any control label ,picture,listbox, any thing to the form remove it from form or end my program

    Unlike the previous three items, the name of the new item appears and is deleted or end

    Do you mean "...any control found other than the three controls label, text, button removed"?

    The only controls that could be there other than the controls you create at design time are the controls that you create at runtime.  Therefore, when you add those additional controls at runtime, keep track of any control that you will want to remove in a List(Of Control).  Then, when required, iterate over that list to remove them one at a time.

    You cannot reliably use the name of a control to identify it.  Use a reference to the control object, such as is created when you add it to a collection.


    • Edited by Acamar Tuesday, February 27, 2018 12:14 AM sp
    Tuesday, February 27, 2018 12:13 AM
  • Hi

    Sorry, I do not understand.


    Regards Les, Livingston, Scotland

    i wanted any control found else three control (label ,text,button) removed it or end my program

    if a add for example button 2 or any control label ,picture,listbox, any thing to the form remove it from form or end my program

    Unlike the previous three items, the name of the new item appears and is deleted or end

    Hi monemas,

    Do you mean that you just want to add three control, one label, one textbox, one button in the form, if another controls are added, you will remove these?

    Best Regards,

    Cherry


    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, February 27, 2018 3:03 AM
    Moderator
  • Hi

    Sorry, I do not understand.


    Regards Les, Livingston, Scotland

    i wanted any control found else three control (label ,text,button) removed it or end my program

    if a add for example button 2 or any control label ,picture,listbox, any thing to the form remove it from form or end my program

    Unlike the previous three items, the name of the new item appears and is deleted or end

    Hi monemas,

    Do you mean that you just want to add three control, one label, one textbox, one button in the form, if another controls are added, you will remove these?

    Best Regards,

    Cherry


    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.

    Hi

    Do you mean that you just want to add three control, one label, one textbox, one button in the form, if another controls are added, you will remove these?  yes

    Tuesday, February 27, 2018 6:50 AM
  • i put  textbox and label and button in the form

    I want to make sure they are there item and if there is another element, they are deleted
    During the work of the program

    You mean like if the ligth in the refrigerator is not burning when the door is closed. 

    If you have made the code to remove the element. It is gone. 

    If you are not sure of that. Make your code new, you have probably only added and removed nothing. It is probably to long and even confusing yourself.


    Success
    Cor



    • Edited by Cor Ligthert Tuesday, February 27, 2018 11:34 AM
    Tuesday, February 27, 2018 8:20 AM
  • This is a rather strange requirement, but the best I can think of would be a custom Form that responds to the OnControlAdded method:

    Public Class LimitedControlForm
        Inherits Form
    
        Protected Overrides Sub OnControlAdded(e As ControlEventArgs)
            MyBase.OnControlAdded(e)
            Select Case e.Control.GetType
                Case GetType(Button)
                    If Me.Controls.OfType(Of Button).Count > 1 Then Throw New ControlNotAllowedException
                Case GetType(Label)
                    If Me.Controls.OfType(Of Label).Count > 1 Then Throw New ControlNotAllowedException
                Case GetType(TextBox)
                    If Me.Controls.OfType(Of TextBox).Count > 1 Then Throw New ControlNotAllowedException
                Case Else
                    Throw New ControlNotAllowedException
            End Select
        End Sub
    
        Public Class ControlNotAllowedException
            Inherits Exception
            Public Sub New()
                MyBase.New("The control already exists or cannot be added.")
            End Sub
        End Class
    End Class

    You would then have to manually edit the Form.Designer.Vb file for a form to inherit from this new limited form:

    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
    Partial Class Form2
        Inherits LimitedControlForm
    
        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()>
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    ...
    Now if you try to add more than 1 allowed control, or a control which is not allowed, during design time, the designer will throw an error message and not allow the new control.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by monemas Wednesday, March 28, 2018 2:05 PM
    Tuesday, February 27, 2018 4:44 PM
    Moderator