Iterate over numbered textboxes [duplicate] - vb.net

This question already has answers here:
Can I use variables to control which PictureBox I am using?
(2 answers)
Closed 5 years ago.
I created 10 textboxes which are entitled Txtb_1, Txtb_2.... and a table with 10 values. For example Dim table() as integer = {0,1,2,3....}
The first 5 textbox will contain a value as an integer. The other 5, the value will be incremented by x.
I want to show on screen the comparison before and after.
All my variables are declared as string for Txtb_1.,Txtb_2 etc.
For the variable Txtb_6 to Txtb_10, is it possible to put them in a for loop and change only the number in the name of the variable?
What i want is to be able to simplify my task.
For example :
for i = 6 to 10
Txtb_**i**.text = table(x)
next
So the loop will go from Txtb_6 to Txtb_10, reducing the coding.

For this you'll be able to use the Control.FindControl function.
Dim curControl As Control = Nothing
For i = 6 To 10
curControl = FindControl($"Txtb_{i}") 'or string format or &
If (Not curControl Is Nothing)
' your logic
(curControl As TextBox).Text = table(x) 'prob want to exception handle
End If
Next
More info here: https://msdn.microsoft.com/en-us/library/486wc64h(v=vs.110).aspx
Ways to cast: https://www.codeproject.com/Articles/5044/Cheat-Sheet-Casting-in-VB-NET-and-C

Related

Sort issue with integers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to make a leader board that puts highest scores at the top or left with this layout
99 - james,
90 - will,
80 - dan,
70 - phil,
60 - kevin,
570 - jim,
50 - ben,
40 - david,
30 - jose,
220 - peter,
20 - tony,
10 - nick,
The .sort command doesn't work for numbers 3 digits and up i have a list that i am tying to sort but it is not working.
This is what i am currently working with.
leaderboard.Sort()
leaderboard.Reverse()
It does sort numbers under 100 perfectly well this is the only issue i have.
Dim leaderboard As New List(Of String)
Using Reader As New StreamReader("C:\Users\1111\OneDrive\Documents\Leaderboard.txt")
While Reader.EndOfStream = False
leaderboard.Add(Reader.ReadLine())
End While
End Using
leaderboard.Sort()
leaderboard.Reverse()
First I made a Structure as a template to hold your data. It has 2 properties. One to hold the Score and one to hold the name.
Private Structure Leader
Public Property Score As Integer
Public Property Name As String
End Structure
The code starts out by creating a new list of Leader (the name of the structure).
I used the File class from System.IO (you will need to add this to the list of Imports at the top of the code file). .ReadAllLines returns an array of strings, each element is a single line from the text file.
Then we loop through each line, splitting the line by the hyphen. This will give your an array of strings with 2 elements. Before you try to convert the the first element to an Integer be sure to trim off any spaces. The second element of the array will contain the name and need to be trimmed. I also replaced the comma with an empty string.
Finally, a bit of Linq magic orders the list in descending order by score into another list. Function(lead) is a function that takes each item in the original list and tests its Score property. I called .ToList at the end so orderedLeader could be display in a DataGridView.
Private Sub OPCode()
Dim leaderboard As New List(Of Leader)
Dim lines = File.ReadAllLines("leaderboard.txt")
For Each line In lines
Dim splitLine = line.Split("-"c)
Dim sc = CInt(splitLine(0).Trim)
Dim nm = splitLine(1).Trim.Replace(",", "")
leaderboard.Add(New Leader With {.Score = sc, .Name = nm})
Next
Dim orderedLeaderbord = leaderboard.OrderByDescending(Function(lead) lead.Score).ToList
DataGridView1.DataSource = orderedLeaderbord
End Sub

Determine if an integer is a member of a list of integers

I need to determine if a particular integer does not exists in a datagridview column. I assume I should create an array of the integers from the dgv column, and then compare if the integer exists in the array. However, there is perhaps an easier or simpler way.
I have looked at many articles but none of them resolve my task. Some of the Stack Overflow articles show similar solutions but I can't quite determine what to do.
For a = 0 To Dgv1.RowCount - 1
If Not Dgv1(1, a).Value = Dgv0(1, m).Value Then
Dgv0(1, Dgv0.RowCount - 1).Value = Dgv0(1, m).Value
End If
Next
I hope to compare an integer with a column of integers in a datagridview and if it is present do nothing but if is not present add it to the datagrid view
Are you using wpf? If yes, create a model.
provide a checking mechanism at the setter, use observablecollection or list then bind it to the datagirdview
Get the row and column of the datagridview
then compare (means condtional statement) to the variable you wanna check
and of course it should be inside of loop, loop count is equal to the count of rows you have in the datagridview.
Here's an example code:
Dim column As String = "YourColumnNameHere"
' Assuming 2 is the number you wanna compare
Dim value As Integer = 2
For row As integer = 0 to dataGridView.RowCount - 1
If dataGridView.Rows(row).Cells(column).Value = value Then
' Do something here
Else
' Do something here
End If
Next

How to delete rows based on empty cell value in VBA [duplicate]

This question already has answers here:
Code in VBA loops and never ends. How to fix this?
(2 answers)
Closed 5 years ago.
I am trying to delete entire rows based on whether the cell value in the D column is NULL or not. My entire code so far is:
Sub DeleteNULL()
Dim i As Long
For i = 2 To 119713
If IsEmpty(Range("Di")) = True Then
Rows([i]).EntireRow.Delete
Else
If IsEmpty(Range("Di")) = False Then
Next i
End If
End Sub
I keep getting compile errors, either If without Else or Next without For, how should I fix this?
Thanks in advance.
A few things:
Placement of a lot of syntax is off.
When adding or deleting rows, you need to loop backwards based on how Excel handles these events.
See code below:
Sub DeleteNULL()
Dim i As Long
For i = 119713 To 2 Step -1
If IsEmpty(Range("D" & i)) Then Rows([i]).EntireRow.Delete
Next i
End Sub

Can't copy formula? [duplicate]

This question already has answers here:
How do I put double quotes in a string in vba?
(5 answers)
Closed 5 years ago.
I can't operate the code without it stating the sub or function is not defined in the formula I'm trying to paste down column v.
Sub SEALANTSCHEDULIZER()
' 1. Unhide All Columns
Columns.EntireColumn.Hidden = False
' 2. Clear All color highlights
ActiveSheet.UsedRange.Interior.ColorIndex = 0
' 3. Drag formulas in all columns down to last row (if necessary) – use column A (ID) as row count
LastRowColumnA = cell(Rows.count, 1).End(xlUp).Row
Range("V2:V" & LastRowColumnA).Formula = "=IF(OR(W2="Yes", AE2="Yes"), "Yes", "No")"
End Sub
You need to double the " around the text strings.
"=IF(OR(W2=""Yes"", AE2=""Yes""), ""Yes"", ""No"")"

Assign the counter number for object name [duplicate]

This question already has answers here:
For each textbox loop
(3 answers)
Closed 6 years ago.
I have 60 textboxes in vb and I want to export all of them into one text file. I use this code to do that:
My.Computer.FileSystem.WriteAllText("Filename.txt", TextBox1.Text)
Because I have 60 textboxes so I will take me a long time to do that, I use the For...Next to ease the procedure. I want to have a code like this:
For i as integer = 1 to 60
My.Computer.FileSystem.WriteAllText("Filename.txt", TextBox(i).Text)
Next
Can I do that in vb? Are there any alternative ways to do that? Thanks!
You can create a list of textboxes and add all the needed textboxes to the list. Use foreach loop on the list to access the textboxes one by one and use their text property as you wish.
You can get the TextBoxes, in order using Controls.Find() like this:
Dim fileName As String = "c:\some folder\path\Filename.txt"
Using sw As New System.IO.StreamWriter(fileName)
Dim tb As TextBox
Dim matches() As Control
For i As Integer = 1 To 60
matches = Me.Controls.Find("TextBox" & i, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
tb = DirectCast(matches(0), TextBox)
sw.WriteLine(tb.Text)
End If
Next
End Using
If you simply iterate over the Controls collection the TextBoxes may be returned in a different order (than their name) if you had cut them or copied/pasted some of them when you created the form.