How to change the order controls are read in .Net? - vb.net

I have a very textbox rich heavy panel (approx ~ 25 textboxes) for which I want to retrieve all my text from.
I've done so using this loop.
Dim AllItemsArray As New ArrayList
For Each txt As Control In Panel2.Controls
If txt.GetType Is GetType(TextBox) Then
AllItemsArray.Add(txt.Text)
End If
Next
However, for some odd reason it reads the textboxes in a completly random order which makes using the information extremely difficult.
I thought that the textboxes were read in the order they are made, but so far it hasn't done so.
Does anyone have any suggestions as to how I can alter it so that it reads the textboxes in order?
IE. textbox1.text, textbox2.text ...etc
and not
textbox5.text, textbox2.text, textbox 20 ....etc
Thanks

This gets the controls in Tab order sequence. It also gets controls that are in containers, i.e. GroupBox.
Dim ctrl As Control = Me.GetNextControl(ctrl, True)
Do While ctrl IsNot Nothing
Debug.WriteLine(ctrl.Name)
ctrl = Me.GetNextControl(ctrl, True)
Loop

Related

Test if Controls/fields are on the visible tab ms Access

I have a MS Access Form with various controls organised into tabs.
There are some text fields among them, and I need the text field to update (that is, the vba to update them) when their tab is opened. The text fields aren't bound, the updating is a bit more customised.
I've tried the below code but it returns True for text fields that aren't on the visible tab
Private Sub TabCtl2_Change()
Dim Ctl As Control
For Each Ctl In Me.Form.Controls
If Ctl.ControlType = acTextBox Then
'Want to do stuff here
Debug.Print Ctl.Visible 'This is returning 'True' for all text fields
'not just the ones on the active tab.
Debug.Print Ctl.Name
End If
Next
End Sub
As far as I can see the Tab objects don't have a Controls collection or whatever you call it. Can someone help me how to loop through controls on a tab? I don't want to code it all in manually if I can help it.
The Page object in the Pages collection in the tabcontrol does have a Controls property.
This means, if you want to do something for all controls on the active page, you can use the following:
For Each control In Me.MyTabcontrol.Pages(Me.MyTabcontrol.Value).Controls
'Do Stuff
Next

Visual Basic 2008

My previous question is not clear. So I'm going to repeat it.
The label on the right part are the transmuted grade and the textbox is the raw score percentage.
How do I do a short way for this codes (where I will not repeat it again in other textboxes)
Dim grade as Integer
This codes will be in the button so when pressed, the raw percentage will be transformed to transmuted grade.
If MathTextbox.Text = "100" then MathLabel.Text= "1"
Codes like this, my prob is how do I avoid repeating it in each texboxes.
If the problem is that you want one button to perform the same action to multiple controls then you can do something like this:
For Each ctrl1 As Control In Me.Controls
If TypeOf (ctrl1) Is TextBox Then
For Each ctrl2 As Control In Me.Controls
If TypeOf (ctrl2) Is Label AndAlso _
Microsoft.VisualBasic.Strings.Left(ctrl1.Name, 4) _
= Microsoft.VisualBasic.Strings.Left(ctrl2.Name, 4) Then
ctrl2.Text = ctrl1.Text
Exit For
End If
Next
End If
Next
Note that there are much better ways for identifying which label pairs with which textbox (I prefer the Tag property myself) and that if you're going to perform a numerical operation on a string (such as a textbox's Text property) you should validate it first, e.g. with IsNumeric.

loop through all comboboxes with specific name

is it possible to loop through all comboboxes with specific name. For example I have 25 comboboxes in a groupbox i need to loop through 20 of them (each of this 20 have name special_combo_1,special_combo_2 and etc. but another 5 have another names so i need to leave as they are)and change their width at once or change the text or anything else.
You can use Control.Controls to get all the controls the GroupBox contains.
Then, you should cast each Control object to the ComboBox type by using TryCast(Object, Object).
You can check the prefix with String.StartsWith(String).
For Each Item As Control In GroupBox1.Controls
Dim ComboBoxItem As ComboBox = TryCast(Item, ComboBox)
If ComboBoxItem IsNot Nothing Then
If ComboBoxItem.Name.StartsWith("special_combo_") Then
' Code here
End If
End If
Next
Use the OfType to make a collection of comboboxes from the control collection. No TryCast needed so increase in preformance. Then filter them with the Where clause to drill it down even further. Now you iterate only a small collection of controls.
Dim spComboboxes =
GroupBox1.Controls.OfType(Of Combobox)().
Where(Function(cb) cb.Name.StartsWith("special_combo_")).ToList()
Iterate the comboboxes:
For Each cb In spComboboxes
'do something
Next

Going through controls in order

I'm creating an XML file with the text fields of a form. When I go through them using a For Each loop:
For each Ctrl in Me.Controls
'dosomething
Next
it doesn't take them in order; that is, it first takes the TextBox in the middle, then the first one, then another and it keeps going that way.
Is there a way where I can take the values in order?
Me.Controls contains the controls in the order they were created.
To change that, select the first control (in the designer), click Send to Back, and repeat.
You could order by TabIndex:
Dim allTextBoxes = From txt In Me.Controls.OfType(Of TextBox)()
Order By txt.TabIndex
Another way is to order by the location:
allTextBoxes = From txt In Me.Controls.OfType(Of TextBox)()
Order By txt.Location.Y, txt.Location.X
For Each txt In allTextBoxes
' ...
Next
This might work for you. Not a VB guy but this is my attempt at ordering the controls by their location, top to bottom.
... Me.Controls.OrderBy(Function(c) c.Location.Y)

how to clear all tools in the vb.net from

i have used many of the controls in vb.net forms, including a textbox and combobox, but I want to clear all my text boxes at once, while not typing textbox1.clear() for each one.
Is there any other way to clear all my textboxes?
If I understand you question right, you should be able to loop through all the controls on your form and check to see what Control type they are. Based on their type, either set the textbox Text property to String.Empty, or your ComboBox to the index of a blank ListItem (presumably item zero).
Something like:
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
Else
' do something similar for your ComboBox
End If
Next
You can parse through each control on your form and test what type of control that is and handle each type separately, but it is easier to simply set each control manually.
Consider writing a ClearForm routine that does all of this, that way all you have to do is call the method.