How to find the maximum of each column under that specific column in a text box just by pressing one button?

• Question

• I have five column in my VB Form, and i want to find the maximum for each column just by pressing of one button. I don't want prepared buttons as i named a,b,c,d,e. one button needs

*Note: in uploaded figure the numbers are random and alternative according to user inserted numbers in previous steps.  (SEE SECOND FIGURE)

Amranullah

• Edited by Monday, January 13, 2020 12:55 PM
Monday, January 13, 2020 11:13 AM

All replies

• Hello,

Here are examples for TextBoxes aligned in columns and another example for doing a column in a DataGridView, repeat for each column.

If each column is a column of TextBoxes then give them names such as Column1TextBox1, Column1TextBox2 etc then a total TextBox for each column etc Total1TextBox.

Code per column

```Dim Column1Max = Controls.OfType(Of TextBox).
Where(Function(tb) tb.Name.StartsWith("Column1")).
Select(Function(tb)
Dim result As Decimal = 0
If Decimal.TryParse(tb.Text, result) Then
Return result
Else
Return 0
End If
End Function).Max()

Total1TextBox.Text = Column1Max.ToString()```

DataGridView is similar

```Total1TextBox.Text = DataGridView1.
Rows.
Cast(Of DataGridViewRow).
Max(Function(row)
Dim result As Decimal = 0
If Decimal.TryParse(CStr(row.Cells(0).Value), result) Then
Return result
Else
Return 0
End If
End Function).ToString()
```

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

Monday, January 13, 2020 11:33 AM
• I means how can get the maximum of text boxes which locates as column in a separate text-box which is white color text-box. and i want to get the maximum of all just by pressing one button, and display them in white color text-box under each column.

Amranullah

Monday, January 13, 2020 12:48 PM
• Well what I presented before, let's call that a base. Here each column TextBox name starts with ColumnX

Where X is incremented downwards e.g. Column1TextBox1, Column2TextBox2 etc. Then under each of the columns is a TextBox for Max values. The two last variables can be used in a MessageBox, Labels or TextBox controls.

Did I miss something that you can go with this code?

In a button click event.

```Private Sub ResultsButton_Click(sender As Object, e As EventArgs) Handles ResultsButton.Click

Dim column1Max = Controls.OfType(Of TextBox).
Where(Function(tb) tb.Name.StartsWith("Column1")).
Select(Function(tb)
Dim result As Decimal = 0
If Decimal.TryParse(tb.Text, result) Then
Return result
Else
Return 0
End If
End Function).Max()

Total1TextBox.Text = column1Max.ToString()

Dim column2Max = Controls.OfType(Of TextBox).
Where(Function(tb) tb.Name.StartsWith("Column2")).
Select(Function(tb)
Dim result As Decimal = 0
If Decimal.TryParse(tb.Text, result) Then
Return result
Else
Return 0
End If
End Function).Max()

Total2TextBox.Text = column2Max.ToString()

Dim Column3Max = Controls.OfType(Of TextBox).
Where(Function(tb) tb.Name.StartsWith("Column3")).
Select(Function(tb)
Dim result As Decimal = 0
If Decimal.TryParse(tb.Text, result) Then
Return result
Else
Return 0
End If
End Function).Max()

Total3TextBox.Text = column2Max.ToString()

Dim Column4Max = Controls.OfType(Of TextBox).
Where(Function(tb) tb.Name.StartsWith("Column4")).
Select(Function(tb)
Dim result As Decimal = 0
If Decimal.TryParse(tb.Text, result) Then
Return result
Else
Return 0
End If
End Function).Max()

Total4TextBox.Text = column2Max.ToString()

Dim column5Max = Controls.OfType(Of TextBox).
Where(Function(tb) tb.Name.StartsWith("Column5")).
Select(Function(tb)
Dim result As Decimal = 0
If Decimal.TryParse(tb.Text, result) Then
Return result
Else
Return 0
End If
End Function).Max()

Total5TextBox.Text = column2Max.ToString()

'
' Can be placed into TextBox controls
'
Dim sums = Controls.OfType(Of TextBox).
Where(Function(tb) tb.Name.StartsWith("Column")).
Select(Function(tb) CDec(tb.Text)).Sum().
ToString()

Dim max = Controls.OfType(Of TextBox).
Where(Function(tb) tb.Name.StartsWith("Column")).
Select(Function(tb) CDec(tb.Text)).
Max().
ToString()

End Sub
```

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

Monday, January 13, 2020 1:33 PM