How to group multiple objects - vb.net

I have been searching for a method to group multiple objects to change a common value but have not been successful. I have been forced to do things like this:
Label10.Visible = True
Label11.Visible = True
Label12.Visible = True
Label13.Visible = True
Label14.Visible = True
RectangleShape8.Visible = True
RectangleShape9.Visible = True
RectangleShape10.Visible = True
RectangleShape11.Visible = True
Ect, Is there a method to group, or declare multiple objects to refer to all of them at the same time? I have attempted to declare but i was unsuccessful. Thanks for your help in advanced.

You can use the following:
Dim RectangleShapeGroup() As String = {"RectangleShape8", "RectangleShape9", "RectangleShape10", "RectangleShape11"}
Dim LabelGroup() As String = {"Label10", "Label11", "Label12", "Label13", "Label14"}
For Each ctrl As Control In Me.Controls
If Array.IndexOf(RectangleShapeGroup, ctrl.Name) > -1 Or Array.IndexOf(LabelGroup, ctrl.Name) > -1 Then
ctrl.Visible = True
End If
Next
In case you want to show all Label controls you can use the following:
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Label Then
ctrl.Visible = True
End If
Next
... or all controls with names starting with Label or RectangleShape:
For Each ctrl As Control In Me.Controls
If ctrl.Name.StartsWith("Label") Or ctrl.Name.StartsWith("RectangleShape") Then
ctrl.Visible = True
End If
Next

Just add your controls to List(Of ControlType) then loop through to change any properties. This code is for Windows Forms. If this is a different type of application plead indicate that in your question.
Private lstLabels As New List(Of Label) From {Label10, Label11, Label12, Label13, Label14}
Private Sub MakeLabelsVisible()
For Each l In lstLabels
l.Visible = True
Next
End Sub

Related

Looping through Control Names and hiding all controls that contain the right number within a frame

What I am trying to do is on a Word Userform, if I select a number in a combo box (cb_CountCohorts) (options are 1-10) then any control (option button or textbox that contains that number +1 (so if I select 5, those controls that have 6-10) will not be visible.
With that being said, I did get it to work but I know that it is not efficient.
Below is the beginning but I realize for each case, there would have to be 10 more sets of what you see below times 10 different If statements. Is there a way to say something like if cb_Countcohrts ="1" find all controls in this frame that does not contain Cohort 1 and hide it...if cb_countcohorts ="5" then hide everything that contains cohort 6, 7, 8, 9, 10? Thanks in advance for all and any help
Private Sub cb_CountCohorts_Change()
If cb_CountCohorts = "1" Then
txt_cohort1.Visible = True
txt_cohort2.Visible = False
txt_cohort3.Visible = False
txt_cohort4.Visible = False
txt_cohort5.Visible = False
txt_cohort6.Visible = False
txt_cohort7.Visible = False
txt_cohort8.Visible = False
txt_cohort9.Visible = False
txt_cohort10.Visible = False
I tried this too but it doesnt seem to work like I want either
Private Sub cb_CountCohorts_Change()
For i = 2 To 10
Set VarText = frm_master.Controls("txt_cohort" & i)
If cb_CountCohorts.Value > VarText.Value Then
VarText.Visible = False
End If
Next i
End Sub
Something like this:
Private Sub cb_CountCohorts_Change()
Dim v As Long, i As Long
v = CLng(cb_CountCohorts.Value)
For i = 2 To 10
Me.Controls("txt_cohort" & i).Visible = (i <= v)
'any other controls here....
Next i
End Sub
If you want something generic for all controls (assuming a consistent naming convention) -
Private Sub cb_CountCohorts_Change()
Dim v As Long, c, i As Long, arr
v = CLng(cb_CountCohorts.Value)
For Each c In Me.Controls
If c.Name Like "txt_cohort#*" Then
arr = Split(c.Name, "_")
i = CLng(Replace(arr(1), "txt_cohort", ""))
c.Visible = (i <= v)
End If
Next c
End Sub
...basically expanded from Robert's suggestion
Untested, but this should work:
Dim c As Control
For Each c In Me.Controls
If InStr(TypeName(c),"cohort") Then
c.Visible = False
End If
Next

DropDownMenuItem check should uncheck other DropDownMenuItems

I'm creating a ToolStripMenuItem's DropDownItems at runtime.
Me.mnuExtrasSecondaryLCID.DropDownItems.Clear()
Dim iCount As Integer = -1
For Each nLang As clsLanguage In g_LCIDs
If nLang.IsLeader Then
iCount += 1
Dim n As New ToolStripMenuItem
n.Name = "mnuSecondaryLCID" & iCount.ToString()
n.Text = nLang.Title
n.Tag = nLang.LCID
n.Available = True
n.CheckOnClick = True
Me.mnuExtrasSecondaryLCID.DropDownItems.Add(n)
AddHandler n.Click, AddressOf Me.SecondaryLCIDClick
End If
Next
This works fine.
When I then check one of the DropDownItems at runtime, any other DropDownItems in the same "list" stay checked.
I would instead like to have only one checked (=the last clicked one).
Is there a property that would let me do this automatically, or do I need to code this by unchecking all other DropDropItems manually?
You need to code it manually. When clicking on a sub menu, uncheck all other siblings:
For index = 1 To 5
Dim subMenu = New ToolStripMenuItem(index.ToString())
subMenu.CheckOnClick= True
AddHandler subMenu.Click, Sub(obj, arg)
Dim item = DirectCast(obj, ToolStripMenuItem)
For Each sibling In item.Owner.Items.OfType(Of ToolStripMenuItem).Except({obj})
sibling.Checked = False
Next sibling
End Sub
Menu1ToolStripMenuItem.DropDownItems.Add(subMenu)
Next

Textbox Enabled True False

This is code am trying to solve
Dim i As Integer
i = ListBox1.SelectedIndex
If ListBox1.SelectedIndex = 0 Then
TextBox10.Enabled = False
TextBox25.Enabled = False
TextBox30.Enabled = False
TextBox40.Enabled = False
TextBox55.Enabled = False
TextBox65.Enabled = False
TextBox73.Enabled = False
TextBox84.Enabled = False
TextBox95.Enabled = False
TextBox100.Enabled = False
TextBox185.Enabled = False
Else
TextBox(??).Enabled = True
End If
How to other textboxes to enable True?
I mean
10,25,30,40... 185 are false and all other textboes Enabled true ?
You can find control of type textbox and assign default enabled= true and then write your code to set enabled=false.
For Each c As Control In Me.Controls
If c.GetType Is GetType(TextBox) Then
c.Enabled=true
End If
Next
OR
Dim allTextBox As New List(Of Control)
For Each c As TextBox In FindControl(allTextBox, Me,GetType(TextBox))
c.Enabled=true
Next
Public Shared Function FindControl(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
If parent Is Nothing Then Return list
If parent.GetType Is ctrlType Then
list.Add(parent)
End If
For Each child As Control In parent.Controls
FindControl(list, child, ctrlType)
Next
Return list
End Function
I'd go with two lists:
var falseList = new List<ListBox> { TextBox10, TextBox25, TextBox30, ... };
var trueList = new List<ListBox> { TextBox1, TextBox2, TextBox3, ... };
Then in each condition block in the if enumerate each list and set false:
foreach(var box in falseList) box.Enabled = false;
foreach(var box in trueList) box.Enabled = true;
Another option is to setup a DataSource with two booleans and set DataBinding on each of the true ListBoxes to one of the properties and the other set to the false property then setting the found property on on the datasource they all update in one statement.
Note: Sorry it's c# syntax but it's really close to VB.NET. I just don't know the exact VB syntax
You can iterate over the forms Controls collection. Something like:
Dim i As Integer
i = ListBox1.SelectedIndex
for each c in me.controls
if TypeOf c is TextBox then
dim Textbox as textbox = DirectCast(c, TextBox)
'Determine state of your textbox here.
'Perhaps use the TAG property to assign a selectedindex to each textbox
Textbox.enabled = (Textbox.Tag.ToString() <> i.ToString())
end if
next

Loop through Multiple Listbox Controls And Check Selected Index Of Each

I'm using the below code to try and loop through all ListBox controls in a panel on a WinForm. I want to check if any of them have a SelectedIndex above 0. If they do, I want to set a boolean value in an array to True, else set it to False:
Dim i As Integer = -1
For Each cntrl As Control In Form1.Panel3.Controls
If TypeOf cntrl Is ListBox Then
i = i + 1
If cntrl.selectedindex <> 0 Then
ReportArray(i) = True
Else
ReportArray(i) = False
End If
End If
Next
The issue I am having is that cntrl.selectedindex is not valid as .selectedindex is being picked up that it is not a member of Windows.Forms.Control
How do I get this to see it as a ListBox?
Try converting the cntrl to listbox first like this
Dim i As Integer = -1
For Each cntrl As Control In Form1.Panel3.Controls
If TypeOf cntrl Is ListBox Then
Dim TmpCntrl As ListBox = TryCast(cntrl, ListBox)
i = i + 1
If TmpCntrl.selectedindex <> 0 Then
ReportArray(i) = True
Else
ReportArray(i) = False
End If
End If
Next

Setting ReadOnly attribute to all Textboxes in Array of Controls

I have the following code looping through a variety of arrayed controls in a form:
For r As Long = LBound(ctrlArray) To UBound(ctrlArray)
If TypeOf ctrlArray(r) Is TextBox Then
ctrlArray(r).Text = ""
If ctrlArray(r).ReadOnly = False Then
ctrlArray(r).ReadOnly = True
End If
Else
If ctrlArray(r).Enabled = True Then
ctrlArray(r).Enabled = False
End If
End If
Next
I receive the error "'ReadOnly' is not a member of System.Windows.Forms.Control" when trying to set textboxes as read only.
Solved this right before I hit the submit button. Thought I would share anyway:
Dim tbx As TextBox
For r As Long = LBound(ctrlArray) To UBound(ctrlArray)
If TypeOf ctrlArray(r) Is TextBox Then
ctrlArray(r).Text = ""
tbx = ctrlArray(r)
If tbx.ReadOnly = False Then
tbx.ReadOnly = True
End If
Else
If ctrlArray(r).Enabled = True Then
ctrlArray(r).Enabled = False
End If
End If
Next