I'm currently writing a program for laboratory weighing. On my form there is a grid of 42 positions (7 rows of 6 samples = 1 rack), where each position is a button, with name "Buttonxx" (where xx stands for position number, e.g. Button01, Button02, ... , Button42)
All the information comes from a Oracle Database, through an SQL.
I want to change the Backcolor of specific button based on a piece of information that is returned by the SQL, namely if that sample has to be weighed or not (ToDo = "Y" or "N") for a certain laboratory analysis (= "TitrType" in code)
E.g. In the code below, if for a certain rack only position 23 needs to be weighed (ToDo= "Y") for TAN (= analysis/TitrType), then only the backcolor of that button ("Button23") should be changed to LightSkyBlue
I get the correct information from the SQL and am able to change the name of a virtual button into the name of the position that is ToDo: DummyBtn.Name = "Button23"
But the Backcolor doesn't change for some reason.
p.s.: I'm only novice programmer, so feel free to ask for more code or information if you need it.
Public Sub PrFillSampleGrid(TitrType As String, ByRef RS As System.Data.DataSet)
Dim Pos As Integer = 0
Dim TODO As String = ""
For x = 0 To RS.Tables("test").Rows.Count - 1
Pos = RS.Tables("test").Rows(x).Item("SERIESPOS")
TODO = RS.Tables("test").Rows(x).Item("TODO")
For y = 1 To 42
Dim DummyBtn As New Button
Select Case TODO.ToUpper
Case TODO = "YES", "Y"
If y = Pos Then
DummyBtn.Name = "Button" & y
Select Case TitrType
Case Is = "AcIn"
DummyBtn.BackColor = Color.Orange
Case Is = "TAN"
DummyBtn.BackColor = Color.LightSkyBlue
Case Is = "TBN"
DummyBtn.BackColor = Color.Crimson
Case Is = "TBN2"
DummyBtn.BackColor = Color.Yellow
End Select
End If
Case TODO = "NO", "N"
DummyBtn.BackColor = SystemColors.Control
End Select
Next y
Next x
End Sub
Try doing this:
Public Sub PrFillSampleGrid(TitrType As String, ByRef RS As System.Data.DataSet)
For x = 0 To RS.Tables("test").Rows.Count - 1
Dim Pos = CInt(RS.Tables("test").Rows(x).Item("SERIESPOS"))
Dim TODO = CStr(RS.Tables("test").Rows(x).Item("TODO"))
For y = 1 To 42
For Each DummyBtn In Me.Controls.Find("Button" & y.ToString(), True).OfType(Of Button)()
Select Case TODO.ToUpper
Case "YES", "Y"
If y = Pos Then
Select Case TitrType
Case Is = "AcIn"
DummyBtn.BackColor = Color.Orange
Case Is = "TAN"
DummyBtn.BackColor = Color.LightSkyBlue
Case Is = "TBN"
DummyBtn.BackColor = Color.Crimson
Case Is = "TBN2"
DummyBtn.BackColor = Color.Yellow
End Select
End If
Case "NO", "N"
DummyBtn.BackColor = SystemColors.Control
End Select
Next
Next y
Next x
End Sub
The key change I made is the line For Each DummyBtn In Me.Controls.Find("Button" & y.ToString(), True).OfType(Of Button)(). This searches for the existing buttons with the name "Button" & y on the form and assigns the existing button to DummyBtn.
The other thing I did was put Option Strict On on the top of your code. This immediately highlighted a bunch of other errors in your code which I fixed.
It's advisable to always use Option Strict On as it puts VB into a strict typing mode and many many errors will be eliminated. Your line Case TODO = "YES", "Y" is an example of code which might seem correct, but isn't. It was evaluation to Case "True", "Y" or Case "False", "Y" with Option Strict Off.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With