Click here to Skip to main content
15,886,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using VB.NET, Visual Studio 2022, my skill level is newbie, and the application that I am building is a Windows Forms application.

Can you please help with the following:

I have 100 off windows controls (gauges or meters) called aGauge on a single Windows Form. Rather than altering the value of each gauge individually, is there a way of creating a loop to alter the value? The gauges represent the answers to ten surveys. Each survey has ten questions, hence 100 gauges. Each gauge has been named ga_(survey name 1..10)_(question number 1..10) or ga_s1_q1.. ga_s1_q10 etc.

Rather than write the code on the main form, I would like to put this into a separate module and then call it from the main form.

I am using the code below to find the presence of the aGauge control, but I am unable to change the value as the "control" does not support value changes only text.

What I have tried:

The following will find the presence of the aGauge Control. I am unable to change the value.

VB
' Update gauge values
            Dim iLoop, iSurvey As Integer
            Dim sGaugename As String                    
sGaugename = "ga_" & "s" & iSurvey & "_" & "q" & iQuestion

                    If frm_main.Controls.Find(sGaugename, True).Count = 0 Then
                        MsgBox("No control named " & sGaugename)
                    Else
                        MsgBox(frm_main.Controls.Find(sGaugename, True)(0).Text)
' Code to update found control goes here
Posted
Updated 15-Feb-22 9:57am
v2

If the gauge control you are using doesn't support changes, I'd find that unlikely - it may not be called "value" or "Value" but there will almost certainly be some way to set a "level" - or it would be pretty useless as a gauge control!

The generic Control that all UI items (including forms) derives from however doesn't "know" what you will do with it - a DataGridView will ignore a "Text" property for example where a TextBox will not.
So all you need to do is cast the generic Control that is returned by the Find method to the aGauge class, and that will support it's full range of properties and methods.
Casting and Data Type Conversions in VB.NET[^]
 
Share this answer
 
Comments
Maciej Los 15-Feb-22 10:32am    
5ed!
Please, read this: WinForms get all controls of a specific type VB.NET - TechNet Articles - United States (English) - TechNet Wiki[^]

VB.NET
Imports System.Windows.Forms

Public Module ControlExtensions
    ''' <summary>
    ''' Get list of TextBox on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of TextBox or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function TextBoxList(pControl As Control) As List(Of TextBox)
        Return pControl.Descendants(Of TextBox)().ToList()
    End Function
    ''' <summary>
    ''' Get list of Label on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of Label or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function LabelList(pControl As Control) As List(Of Label)
        Return pControl.Descendants(Of Label)().ToList()
    End Function
    ''' <summary>
    ''' Get list of CheckBox on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of CheckBox or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function CheckBoxList(pControl As Control) As List(Of CheckBox)
        Return pControl.Descendants(Of CheckBox)().ToList()
    End Function
    ''' <summary>
    ''' Get list of DataGridView on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of DataGridView or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function DataGridViewList(pControl As Control) As List(Of DataGridView)
        Return pControl.Descendants(Of DataGridView)().ToList()
    End Function
    ''' <summary>
    ''' Get list of ComboBox on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of ComboBox or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function ComboBoxList(pControl As Control) As List(Of ComboBox)
        Return pControl.Descendants(Of ComboBox)().ToList()
    End Function
    ''' <summary>
    ''' Get list of ListBox on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of ListBox or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function ListBoxList(pControl As Control) As List(Of ListBox)
        Return pControl.Descendants(Of ListBox)().ToList()
    End Function
    ''' <summary>
    ''' Get list of DateTimePicker on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of DateTimePicker or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function DateTimePickerList(pControl As Control) As List(Of DateTimePicker)
        Return pControl.Descendants(Of DateTimePicker)().ToList()
    End Function
    ''' <summary>
    ''' Get list of PictureBox on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of PictureBox or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function PictureBoxList(pControl As Control) As List(Of PictureBox)
        Return pControl.Descendants(Of PictureBox)().ToList()
    End Function
    ''' <summary>
    ''' Get list of Panel on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of Panel or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function PanelList(pControl As Control) As List(Of Panel)
        Return pControl.Descendants(Of Panel)().ToList()
    End Function
    ''' <summary>
    ''' Get list of GroupBox on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of GroupBox or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function GroupBoxList(pControl As Control) As List(Of GroupBox)
        Return pControl.Descendants(Of GroupBox)().ToList()
    End Function
    ''' <summary>
    ''' Get list of ListView on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of ListView or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function ListViewList(pControl As Control) As List(Of ListView)
        Return pControl.Descendants(Of ListView)().ToList()
    End Function
    ''' <summary>
    ''' Get list of Button on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of Button or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function ButtonList(pControl As Control) As List(Of Button)
        Return pControl.Descendants(Of Button)().ToList()
    End Function
    <Runtime.CompilerServices.Extension>
    Public Function RadioButtonList(pControl As Control) As List(Of RadioButton)
        Return pControl.Descendants(Of RadioButton)().ToList()
    End Function
    ''' <summary>
    ''' Get selected RadioButton
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <param name="pChecked">True to get checked</param>
    ''' <returns>Get selected RadioButton</returns>
    <Runtime.CompilerServices.Extension>
    Public Function RadioButtonChecked(pControl As Control, Optional pChecked As Boolean = True) As RadioButton
        Return pControl.Descendants(Of RadioButton)().ToList().FirstOrDefault(Function(radioButton) radioButton.Checked = pChecked)
    End Function
    ''' <summary>
    ''' Get list of NumericUpDown on control
    ''' </summary>
    ''' <param name="pControl">Control to find controls on</param>
    ''' <returns>List of NumericUpDown or empty list</returns>
    <Runtime.CompilerServices.Extension>
    Public Function NumericUpDownList(pControl As Control) As List(Of NumericUpDown)
        Return pControl.Descendants(Of NumericUpDown)().ToList()
    End Function
    ''' <summary>
    ''' Get names of controls 
    ''' </summary>
    ''' <param name="pControls"></param>
    ''' <returns></returns>
    <Runtime.CompilerServices.Extension>
    Public Function ControlNames(pControls As IEnumerable(Of Control)) As String()
        Return pControls.Select(Function(c) c.Name).ToArray()
    End Function
    ''' <summary>
    ''' Is control a container
    ''' </summary>
    ''' <param name="pControl"></param>
    ''' <returns></returns>
    <Runtime.CompilerServices.Extension>
    Public Function IsContainer(pControl As Control) As Boolean
        Return pControl.HasChildren
    End Function
End Module


Here you'll find very interesting function, which gets all controls of specifi type: vb.net - Enumerate all controls in a form (redundant) - Stack Overflow[^]

VB.NET
Enum SearchMethod
    StartsWith = 1
    EndsWith = 2
    Contains = 3
End Enum

Function GetAllControls(Of T As Control)(ctrl As Control, key As String, method As SearchMethod, Optional useTag As Boolean = True) As IEnumerable(Of T)
    ' TODO validate args
    Dim upperKey = key.ToUpper

    Dim searchPredicates() As Func(Of String, Boolean) = {
        Function(src, tgt) src.StartsWith(upperKey),
        Function(src, tgt) src.EndsWith(upperKey),
        Function(src, tgt) src.Contains(upperKey)
    }
    Dim ctrlSelector As Func(Of Control, String) = If(useTag, Function(c) c.Tag.ToString.ToUpper, Function(c) c.Name.ToUpper)

    Return GetAllControlsIterator(Of T)(ctrl, ctrlSelector, searchPredicates(CInt(method) - 1))
End Function

Private Iterator Function GetAllControlsIterator(Of T As Control)(ctrl As Control, ctrlSelector As Func(Of Control, String), searchPredicate As Func(Of String, Boolean)) As IEnumerable(Of T)
    For Each child In ctrl.Controls
        If searchPredicate(ctrlSelector(child)) AndAlso TypeOf child Is T Then Yield DirectCast(child, T)

        For Each grandChild In GetAllControlsIterator(Of T)(child, ctrlSelector, searchPredicate)
            Yield DirectCast(grandChild, T)
        Next
    Next
End Function


Good luck!
 
Share this answer
 
v2
Comments
Sean Ewington 15-Feb-22 15:10pm    
Thanks very much for your answer. While the link you've shared may be exactly the answer OP needs, we ask that answers be self-contained. The issue with links is that over time, they break. Using a link as a reference is fine, but we ask that the bulk of the answer be available here.
Maciej Los 15-Feb-22 16:00pm    
OK, Sean. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900