vb.net find textbox name that begins with specified string in a TabControl - vb.net

I have a TabControl in a form with 4 tabpages. Each tabpage has multiple GroupBox. Each GroupBox has tableLayoutPanel. Each tableLayoutPanel has pragmatically generated array of textbox. if checkbox in a tableLayoutPanel is checked by the user then textboxes in the respective row will be generated. Suppose, the name of one of my textbox array is txtMax(0), txtMax(1).......upto txtMax(42). I need to know how many txtMax(?) (along with their index array) has been generated and become visible. I have tried the the following:
Dim coutGene as integer = 0
Dim coutParameter as integer = 0
Dim indx As Integer
Dim cntl1, cntl2, cntl3 As Control
For Each cnn As TabPage In tabParameters.TabPages
cntl1 = DirectCast(cnn, TabPage)
For Each c2 As Control In cntl1.Controls
If TypeOf (c2) Is GroupBox Then
cntl2 = DirectCast(c2, GroupBox)
For Each c3 As Control In cntl2.Controls
If TypeOf (c3) Is TableLayoutPanel Then
cntl3 = DirectCast(c3, TableLayoutPanel)
For Each c4 As Control In cntl3.Controls
If TypeOf (c4) Is TextBox Then
Dim txt As TextBox = DirectCast(c4, TextBox)
If txt.Name.StartsWith("txtMax") Then
If txt.Visible = True Then
indx = CInt(Between(txt.Name, "(", ")"))
countGene = CInt(countGene + Val(txtGene(indx).Text))
countParameter = countParameter + 1
txtMax(indx).Tag = ""
End If
End If
End If
Next
End If
Next
End If
Next
Next
Function Between(value As String, a As String, b As String) As String
' Get positions for both string arguments.
Dim posA As Integer = value.IndexOf(a)
Dim posB As Integer = value.LastIndexOf(b)
If posA = -1 Then
Return ""
End If
If posB = -1 Then
Return ""
End If
Dim adjustedPosA As Integer = posA + a.Length
If adjustedPosA >= posB Then
Return ""
End If
' Get the substring between the two positions.
Return value.Substring(adjustedPosA, posB - adjustedPosA)
End Function
But the every time code is not getting inside the loop for this condition If txt.Name.StartsWith("txtMax") Then
I am stuck up here. Any assistance will be highly appreciated. Regards. Tariq

You should simplify that code to this:
For Each tp As TabPage In tabParameters.TabPages
For Each gb In tp.Controls.OfType(Of GroupBox)()
For Each tlp In gb.Controls.OfType(Of TableLayoutPanel)()
For Each tb In tlp.Controls.OfType(Of TextBox)().Where(Function(c) c.Visible AndAlso c.Name.StartsWith("txtMax"))
'If you get here, tb is definitely a visible TextBox with a Name starting with "txtMax".
Next
Next
Next
Next
That code will find every visible TextBox with a Name that starts with ""txtMax" inside a TableLayoutPanel, inside a GroupBox, inside a TabPage, inside tabParameters, guaranteed. If that code doesn;t find any such controls, it's because there are no such controls, so that's what you need to investigate.

Thank you for all yours guidelines and suggestion. I tried to implement all,,, but the code could not find the control even though the control exists and visible. I tried this simple code
For k = 1 To 42
If txtMax(k).Visible = True Then
countGene = CInt(countGene + Val(txtGene(k).Text))
countParameter = countParameter + 1
txtMax(k).Tag = ""
End If
Next
But i find that, it search and count the control that exists only in the present tabpage of the TabControl. So i modified the code, though not efficient code, to solve my problem.
I have 4 tabpages in the TabControl. So i tried to select each tabpage by its index and searched 4 times.
For tp As Integer = 0 To 3
tabParameters.SelectedIndex = tp
For k = 1 To 42
If txtMax(k).Visible = True Then
countGene = CInt(countGene + Val(txtGene(k).Text))
countParameter = countParameter + 1
txtMax(k).Tag = ""
End If
Next
Next
Though i solved my problem, but still i seek your advise for efficient code.

Related

Is there a way to identify which dropdown boxes aren't empty when hitting 'search' on a VBA GUI?

I'm working on an application in VBA that takes in information from an excel sheet, populates a dropdown combobox, then based on the selected information from the dropbox, retrieves the full information for matching values. There are 6 dropboxes and I'm looking for a way to find out which dropboxes have a value (not empty) without rewriting dozens of if statements with the same code but different conditions (i.e combo 1 and 3 have values, so the program will only look for the records based on those two selected fields)
I know this can be achieved with re-writing if statements, but I'm hoping there's an easier way that doesn't take hours?
Private Sub Search_Page1_Click()
Dim year As String
Dim location As String
Dim snap As String
Dim city As String
Dim group As String
Dim endyear As String
year = Multipage1.Cmb_Year.Value
location = Multipage1.Cmb_Location.Value
snap = Multipage1.Cmb_Snapshot.Value
city = Multipage1.Cmb_City.Value
group = Multipage1.Cmb_Group.Value
endyear = Multipage1.Cmb_LeaseEnd.Value
If year = Empty And location = Empty And snap = Empty And city = Empty
And group = Empty And endyear = Empty Then
MsgBox ("Please fill in at least one field")
End If
End Sub
If you can work with a Collection of ComboBox controls, then whip up a custom function like and call it like:
Dim populatedBoxes as New Collection
Set populatedBoxes = GetPopulatedThings(Multipage1, "ComboBox")
Dim cb as MSForms.ComboBox
For Each cb in populatedBoxes
MsgBox cb.Value
Next
In your code, you could replace:
If year = Empty And location = Empty And snap = Empty And city = Empty And group = Empty And endyear = Empty Then
With this:
Set populatedBoxes = GetPopulatedThings(Multipage1, "ComboBox")
If populatedBoxes.Count = 0 Then Exit Sub
Here's the function:
Private Function GetPopulatedThings(container As Object, Optional ctrlType As String = "ComboBox") As Collection
Dim c As New Collection
Dim ctrl As MSForms.Control
For Each ctrl In container.Controls
If TypeName(ctrl) = ctrlType Then
Select Case ctrlType
Case "ComboBox"
If ctrl.ListIndex > -1 Then
c.Add ctrl
End If
Case Else
' TBD
' Additional cases will require separate logic...
End Select
End If
Next
Set GetPopulatedThings = c
End Function

VB.Net Loop controls by names using variables

I need to set text of some controls.
I have a Form with some CheckBoxes an some TextBoxes.
In VBA (If I have 5 TextBoxes named "TextBox1", "TextBox2", ... "TextBox5") I can use something like this:
For n = 1 To 5
Me("TextBox" & n).Text = NeededValue
Next n
I know that something like this is also possible in VB.Net but I wasn't able to find the right syntax (and I didn't find similar codes on SO).
I've tryed using
Me.Controls()
But I can't insert control name this way
Me.Controls.Find("TextBox" & n, True)
would be the similar approach to your VBA Style.
Use For Each and then test with TypeOf to find all TextBoxes in your form like :
For Each myObject1 As [Object] In Me.Controls
If TypeOf myObject1 Is TextBox Then
TryCast(myObject1, TextBox).Text = "NeededValue"
End If
Next
Also :
Dim myText = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
For Each btn In myText
btn.Text = "NeededValue"
Next
For i As Int32 = 1 To 5
Dim Txt = Me.Controls.Find("TextBox" & i, True)
If Txt.Length > 0 Then
Txt(0).Text = "blah"
End If
Next
Or :
For i As Int32 = 1 To 5
Dim Txt = Me.Controls.Find("TextBox" & i, True)
If Txt.Length > 0 Then
Txt(0).Text = "NeededValue"
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

Reference an object using string (the object is under the tab control)

I have a working code here that can put test to all textboxes from 1 to 10. The name of the textboxes are Tb_Hour_1, Tb_Hour_2, Tb_Hour_3, Tb_Hour_4, etc. up to 10.
Dim textboxes As TextBox
For i As Integer = 1 To 10
textboxes = Me.Controls("TB_Hour_" & i)
textboxes.Text = "Test" & i
Next
But the problem comes in when I put my text boxes under the tab control, by searching I've found out that by adding in I can specify the place of the object that I want to call. Here's the working code.
For Each c As TextBox In TabPage1.Controls.OfType(Of TextBox)()
If c.Text = String.Empty Then c.Text = "a"
Next
My question is how can I integrate the 2 codes so I can have something like this
Dim textboxes As TextBox In TabPage1.Controls.OfType(Of TextBox)()
For i As Integer = 1 To 10
textboxes = Me.Controls("TB_Hour_" & i)
textboxes.Text = "Test" & i
Next
My VB is a little rusty, but it should be something like this. You can loop through all of the controls on the tab, look for ones that starts with "TB_Hour_", cast it to a textbox and do whatever it is you want to do.
For Each c As Control In TabPage1.Controls
If c.Name.StartsWith("TB_Hour_") Then
' it's a textbox
Dim tb as Textbox = DirectCast(c, Textbox)
' do whatever you're going to do
End If
Next
Now that you tucked the TextBoxes into a TabContol, don't use Me.Controls(). Instead try:
Dim textboxes As TextBox
For i As Integer = 1 To 10
textboxes = TabPage1.Controls("TB_Hour_" & i)
textboxes.Text = "Test" & i
Next
Otherwise, you can do something like:
Dim textboxes As TextBox
For i As Integer = 1 To 10
textboxes = Me.TabControl1.Controls("TabPage1").Controls("TextBox" & i)
textboxes.Text = "Test" & i
Next

VB manipulate lots of labels inside panel

So i have like 100 labels inside a panel and i need to to change their texts.I Tried an for and i tried Tab Index like an array without success, any ideas how I can select and change properties of these labels?
Sub setCartela(ByVal numeros As Integer)
For cont As Integer = 0 To numeros Step 1
//change labels text inside panel
Next
End Sub
Try like this ...
Dim i as Integer = 1
For Each ctrl As Control In Panel1.Controls
If ctrl.GetType.ToString = "System.Windows.Forms.Label" Then
ctrl.Text = "Text" & format(i)
End If
i += 1
Next
try this code :
dim _countLbl as integer = 1
For each Lbl as Label in Panel1.Controls.Oftype(Of Label)()
Lbl.text="Label" & _countLbl
_countLbl += 1
Next