VB.NET..... Loop help - vb.net

I am using My.Settings in visual studio 2008 to store information, for when the user runs the program again.
I have that working fine... but as I am using 12 textboxes I don't want to write...
my.settings.grade1 = textbox1.text
for each one, and I am also making calculations using the stored information, so I dont want to be writing my.settings.grade1 + my.settings.grade2 etc..
Any help welcome
Thanks =)

On your form that has the textboxes, add them to a collection or an array of textboxes when the form is initialised.
Then, iterate through the collection or array of textboxes to assign to the setting value.
If you don't want to manually code the assignment of the textboxes into the array, then in your form initialisation code, iterate through all controls on the form and check for the control type or a specfici 'Tag' you assign to each textbox, then add each textbox to the array that way.
For Each c as Control in Me.Controls
If c.Tag.ToString() = "Grade" Then
' Add Items to collection here '
End If
Next c

Have you considered using ApplicationSettings Binding to automatically bind your values to your Textboxes.Text properties. This will support 2-way binding and then all you have to do is Call Save when you close.

or you could do something like this:
given the your textboxes are named along the lines of: Grade1, Grade2, Grade3, etc.
you could store the Grades in an Array and then loop through the array:
((TextBox)form.findControl("Grade" + i.ToString())).Text = Grade(i)
Depending on your calculation, then you could also execute the calculation inside the loop.

Populate a List of grade textboxes:
'at the class level'
Public GradeBoxes(11) As TextBox
Const grade As String = "GRADE"
'when the form is created'
Dim i As Integer = 0
For Each ctr As Control In Controls
If TypeOf (ctr) Is TextBox AndAlso ctr.Name.ToUpper.StartsWith(grade) Then
i = CInt(ctr.Name.SubString(grade.Length))
If i >= 0 AndAlso i < GradeBoxes.Length Then GradeBoxes(i) = ctrl
End If
Next ctr
For Each box As TextBox in GradeBoxes
If box IsNot Nothing AndAlso My.Settings(box.Name) IsNot Nothing Then
box.Text = My.Settings(box.Name)
End If
Next box
Save grades:
For Each box As TextBox in GradeBoxes
If box IsNot Nothing AndAlso My.Settings(box.Name) IsNot Nothing Then
My.Settings(box.Name) = box.Text
End If
Next box
My.Settings.Save()

Do you mean something like?
Dim sum As Long
Dim grades(11) As Long
Dim i As Integer = 0
For Each ctr In Controls
If TypeOf (ctr) Is TextBox Then
grades(i) = CLng(ctr.Text)
sum = sum + grades(i)
i = i + 1
End If
Next

Related

Control name from Variable or Dataset. (Combobox)(.items.add)(.datasource)

I've checked for hours but I can't seem to find anything to help.
I want to loop through tables and columns from a dataset and use the column name in a combobox.items.add() line, however the eventual goal is to fill the combobox from the dataset itself possibly in a combobox.datasource line.
The first problem is that I can't get the code correct to setup the combobox control where it allows me to use .items.add("") and in extension .datasource
Error Message = "Object reference not set to an instance of an object"
dstcopt is the dataset from a oledbDataAdapter .fill(dstcopt,"table") line (which returns correct values)
tc_opt is a tab name on a tab control where the comboboxes are
For Each dstable In dstcopt.Tables
For Each dscolumn In dstable.Columns
Dim colName As String = dscolumn.ToString
MsgBox(colName) 'This retuns "aantigen"
Dim cb As ComboBox = Me.tc_opt.Controls("cb_" & colName)
cb.Items.Add(colName)
'cb_aantigen.DataSource = dstcopt.Tables(dstable.ToString)
'cb_aantigen.DisplayMember = "aantigen"
'cb_atarget.DataSource = dstcopt.Tables(dstable.ToString)
'cb_atarget.DisplayMember = "atarget"
Next
Next
The second problem comes when I do it manually (which works) using the exact combobox names cb_aantigen and cb_atarget as seen in the comments.
The problem is that once the form is loaded and the cb's are filled with the correct values, I can't change the value in any single cb individually, when I change one value it changes them all (there is 15 comboboxes in total) I know this is down to using a dataset, but I don't know away to 'unlink them from each other or the dataset'
Not sure if I need to split this into 2 questions, but help on either problem would be appreciated.
EDIT:
After looking at only this section of code for a day. This is what I have come up with to tackle both the problems at once.
The combobox control not working was down to using a tab tc_opt instead of a groupbox gp_anti
The issue with splitting the dataset up into individual comboboxes, I've worked around by taking the value of each cell in the database and adding it separately, probably a better way to do it though
For Each dstable As DataTable In dstcopt.Tables
For Each dscolumn As DataColumn In dstable.Columns
Dim colName As String = dscolumn.ToString
Dim cb(2) As ComboBox
cb(0) = CType(Me.gp_anti.Controls("cb_" & colName), ComboBox)
cb(1) = CType(Me.gp_rec.Controls("cb_" & colName), ComboBox)
cb(2) = CType(Me.gp_nat.Controls("cb_" & colName), ComboBox)
For icb = 0 To cb.Count - 1
If Not (IsNothing(cb(icb))) Then
For irow = 0 To dstable.Rows.Count - 1
If dstable.Rows(irow)(colName).ToString <> Nothing Then
Dim icbitemdupe As Boolean = False
If cb(icb).Items.Contains(dstable.Rows(irow)(colName).ToString) Then
icbitemdupe = True
End If
If icbitemdupe = False Then
cb(icb).Items.Add(dstable.Rows(irow)(colName).ToString)
End If
End If
Next
End If
Next
Next
Next

Find all numericupdown in form visual basic

you can search on the form all the NumericUpDown and therefore report the value of all zero?
I would like to do something like a for loop that controls the form and see how many objects of that type are available, then if the user presses a key, all the NumericUpDown must return a value of 0 do not know if you can do this, I ask the 'help of you experts.
Dim count As Integer
count = 0
For i = 0 To Me.GroupBox1.Controls.Count - 1
Dim name As String
name = Me.GroupBox1.Controls(i).GetType().ToString()
If name.Contains("NumericUpDown") Then
count = count + 1
End If
Next
Label1.Text = count.ToString()
The form has a Controls collection. You can loop through that and check the type by calling GetType() [inherited from System.Object]. If you want to handle subtypes of NumericUpDown, you could try casting the control to NumericUpDown while catching the exception you will see if the control is not a NumericUpDown.
Your VB coding skills are probably better than mine. Been a long time since I wrote VB. Here's a rough example. I put this code in a button click event. You can pull the whole solution from my GitHub repository: https://github.com/kc1073/Samples
Dim count As Integer
count = 0
For i = 0 To Me.Controls.Count - 1
Dim name As String
name = Me.Controls(i).GetType().ToString()
If name.Contains("NumericUpDown") Then
count = count + 1
End If
Next
Label1.Text = count.ToString()
KC

How to compare combobox value with items in listbox - vb.net

I have a combo box and a listbox.
All i want is, when i select a value(text) from the combobox to check if there is a same value(same text) at the listbox and if there is, then msgbox should display as "DATA FOUND"
i tried this code, but its not working
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
If ComboBox1.SelectedItem = ListBox1.ValueMember Then
MsgBox("DATA FOUND")
End If
Next i
Thanks in advance....
You are using two properties with a different meaning for your comparison.
SelectedItem is an object (could be anything depending on how you have filled the combo, ValueMember is just the name of a property to use as the actual value for the items in the ListBox.
However the two classes (ListBox and ComboBox) share the same pattern for storing their list items, so supposing that both are populated using a list of strings then your code could be
Dim curComboItem = ComboBox1.SelectedItem.ToString()
For i = 0 To ListBox1.Items.Count - 1
If curComboItem = ListBox1.Items(i).ToString() Then
MsgBox("DATA FOUND")
Exit For
End If
Next i
If ComboBox1.SelectedItem = ListBox1.ValueMember Then
should be
If ComboBox1.SelectedItem = ListBox1.Items(i) Then
Notice that
ComboBox1.SelectedItem
only works for items inside of the Collection. You can extend this to any text by the
.text
parameter.
P.D.
Next i '???
Using real objects in your ComboBox and ListBox can often lead to better flexibility of your application.
For example, you have a car lot application, in which you have long list of available cars and you don't want to browse the entire list - you use ComboBox to select make and model, and do something with the items in your list box.
(in pseudo-code)
Your car object.
class Car
ModelId
ModelMake
ModelName
FullName = ModelMake & " " & ModelName
End Class
class AvailableCar Inherits Car
IsOnTheLot
VIN
Price
'etc
End Class
In your Form class
comboCarMakes.DataSource = GetListOfMakesOfCars() ' List of Car classes
comboCarMakes.ValueMember = "ModelId"
comboCarMakes.DisplayMember = "FullName"
listAvbailableCars.DataSource = GetListOfAvailableCars() ' List of AvailableCar classes
listAvbailableCars.ValueMember = "VIN"
listAvbailableCars.DisplayMember = "FullName"
Sub comboCarMakes_SelectedIndexChanged
Dim car as Car = DirectCast(comboCarMakes.SelectedItem, Car)
For i = 0 To listAvbailableCars.Items.Count - 1
If car.ModelId = DirectCast(listAvbailableCars.Items(i), AvailableCar).ModelId Then
' Do something
End If
Next
End Sub
Advantage - you have lots of information available immediately.

Is possible to ignore the TextBox?

I'm creating a program to calculate the average. There are 12 TextBox and I want to create the possibility to leave some fields blank. Now there are only errors and the crash of the program. Is possible to create that?
This is part of code:
ItalianoScritto = (TextBox1.Text)
MatematicaScritto = (TextBox2.Text)
IngleseScritto = (TextBox3.Text)
InformaticaScritto = (TextBox4.Text)
ScienzeScritto = (TextBox5.Text)
FisicaScritto = (TextBox6.Text)
MediaScritto = (ItalianoScritto + MatematicaScritto + IngleseScritto + InformaticaScritto + ScienzeScritto + FisicaScritto) / 6
Label10.Text = Str(MediaScritto)
If i leave blank the textbox1 when I click on the button to calculate the average Vb says Cast not valid from the string "" to type 'Single' and the bar of te textbox1 become yellow
I would do the following:
Iterate over the textboxes and check if you can parse the value into an iteger. If yes, add it to a value list.
Then add all values from that list and divide it by the number of cases.
It is faster than big if-statements and resilient against error
dim TBList as new list(of Textbox)
'add your textboxes to the list here
TbList.add(Textbox1)
...
dim ValList as new List(Of Integer)
for each elem in Tblist
dim value as integer
If integer.tryparse(elem.text,value)=True
ValList.add(Value)
else
'report error or do nothing
end if
next
dim Result as Integer
Dim MaxVal as Integer =0
for each elem in ValList
Maxval +=elem
next
Result = MaxVal / ValList.count
If you need support for point values, just choose double or single instead of Integer.
Also: regardless what you do -CHECK if the values in the textboxes are numbers or not. If you omit the tryparse, somebody will enter "A" and your app will crash and burn
Also: You OPTION STRICT ON!
You just have to check if the TextBox is blank on each one before using the value:
If TextBox7.TextLength <> 0 Then
'Use the value inside
End If
The way to do it depends a lot of your code. You should consider editing your question giving more information (and code) in order to us to help you better.

Looping through textboxes in groupbox give odd results

I have the following code:
For Each control2 As Control In GroupBox3.Controls
If TypeOf control2 Is TextBox Then
Dim txt2 As TextBox = TryCast(control2, TextBox)
If counter > totalBoxes Then
totalBoxes += 1
txt2.Text = grade(totalBoxes)
End If
End If
Next
What I am doing is looping through each textbox in groupbox3. Then checking if the counter(total number of grades that are inputted in the form) are greater than the totalBoxes(which is set to 0) and finally I am putting the actual grade(A,B,C,D) into the textbox. The problem is that it is starting the loop at textbox 8(I have 10 textboxes) for some reason and going from there. Does anyone know what the problem is with my code?
Aaron
Well, the name of your textbox has no relation to its index in the .Controls collection of its parent.
One thing you could do is set the .tag property of each of your controls to the index you'd like to pass into your grade function.
textbox1.Tag = 1
textbox2.Tag = 2
...
That way you don't have to worry about which order the textboxes are in while iterating.
For Each box As TextBox In GroupBox3.Controls.OfType(Of TextBox).OrderBy(Function(t) t.Name)
If counter > totalBoxes Then
totalBoxes += 1
box.Text = grade(totalBoxes)
End If
Next box