none
How to declare a variable RRS feed

All replies

  • Drag it from the toolbox on the form

    Success
    Cor

    Tuesday, March 27, 2018 10:10 AM
  • Hello,

    You question is too broad. What I will say is if you wanted to clear say all TextBox controls on a form you could use the following extension method.

    Public Module ControlExtensions
        ''' <summary>
        ''' Provides access to all controls on a form including
        ''' controls within containers e.g. panel and groupbox etc.
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="control"></param>
        ''' <returns></returns>
        <Runtime.CompilerServices.Extension> _
        Public Iterator Function Descendants(Of T As Class)(control As Control) As IEnumerable(Of T)
            For Each child As Control In control.Controls
    
                Dim currentChild = TryCast(child, T)
                If currentChild IsNot Nothing Then
                    Yield currentChild
                End If
    
                If child.HasChildren Then
                    For Each descendant As T In child.Descendants(Of T)()
                        Yield descendant
                    Next
                End If
            Next
        End Function
    End Module
    

    To clear all TextBox controls on a form

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Descendants(Of TextBox)().ToList().ForEach(Sub(b) b.Text = "")
    End Sub



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, March 27, 2018 10:14 AM
    Moderator
  • Karen,

    With the find in the controls.controlcollection it is much easier. That one is always difficult to find on MSDN

    https://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.find(v=vs.110).aspx


    Success
    Cor

    Tuesday, March 27, 2018 10:18 AM
  • Hi Cor, 

    Yes I know about this yet if (and I didn't want to get this deep) we wanted to clear all controls of type TextBox, ListBox, RaidioButton etc it would be simple to use a language extension such as the following.

    Module FormControlExtensions
    		Private ReadOnly _controlDefaults As New Dictionary(Of Type, Action(Of Control))() From _ 
    		    {
    		        {GetType(TextBox), sub(c) CType(c, TextBox).Clear()}, 
    		        {GetType(CheckBox), sub(c) CType(c, CheckBox).Checked = False}, 
    		        {GetType(ListBox), sub(c) CType(c, ListBox).Items.Clear()}, 
    		        {GetType(RadioButton), sub(c) CType(c, RadioButton).Checked = False}, 
    		        {GetType(GroupBox), sub(c) CType(c, GroupBox).Controls.ClearControls()}, 
    		        {GetType(Panel), sub(c) CType(c, Panel).Controls.ClearControls()}}
    
    		Private Sub FindAndInvoke(pType As Type, pControl As Control)
    			If _controldefaults.ContainsKey(pType) Then
    				_controldefaults(pType).Invoke(pControl)
    			End If
    		End Sub
    
    		<Runtime.CompilerServices.Extension> _
    		Public Sub ClearControls(pControls As Control.ControlCollection)
    			For Each control As Control In pControls
    				FindAndInvoke(control.GetType(), control)
    			Next
    		End Sub
    
    
    End Module
    

    Call the method

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    Controls.ClearControls()
    
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, March 27, 2018 10:43 AM
    Moderator
  • How do one declare?

    How to one create a clear button in an windows application form?


    Dim x As Integer = 100 ' Global instantiation
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       x = 0
    End Sub


    La vida loca

    Tuesday, March 27, 2018 11:19 PM