Change button Backcolor based on button name
I am currently writing a program for laboratory weighing. My form has a grid of 42 positions (7 rows of 6 samples = 1 rack) where each position is a button named "Buttonxx" (where xx stands for a position number, eg Button01, Button02, ..., Button42)
All information comes from the Oracle database via SQL.
I want to change the Backcolor of a specific button based on some of the information returned by SQL, namely if this sample should be weighted or not (ToDo = "Y" or "N") for a specific lab analysis (= "TitrType" in the code)
eg. In the code below, if for a certain rack only position 23 (ToDo = "Y") needs to be weighed for TAN (= Analysis / TitrType), then only the backlight of this button ("Button23") should be changed to LightSkyBlue
I am getting correct information from SQL and can change the name of the virtual button to the name of the ToDo position: DummyBtn.Name = "Button23"
But for some reason the background color doesn't change.
ps: I'm just a beginner 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
source to share
Try 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 was the line For Each DummyBtn In Me.Controls.Find("Button" & y.ToString(), True).OfType(Of Button)()
. This looks for existing named buttons on the "Button" & y
form and assigns the existing button DummyBtn
.
Another thing I did was put Option Strict On
at the top of your code. This immediately highlighted a bunch of other bugs in your code, which I fixed.
It is always advisable to use Option Strict On
it as it puts VB in strict printing mode and many bugs will be fixed. Your line Case TODO = "YES", "Y"
is example code that may seem correct, but is not. It was evaluation Case "True", "Y"
or Case "False", "Y"
with help Option Strict Off
.
source to share
You have a problem with the Select
/ operator Case
. You don't need Is =
it as that means you have objects and you are checking for equality. The value you are testing is String
(which is Value Type
), which is a direct comparison.
Just use this: -
Select Case TitrType
Case "AcIn"
DummyBtn.BackColor = Color.Orange
Case "TAN"
DummyBtn.BackColor = Color.LightSkyBlue
Case = "TBN"
DummyBtn.BackColor = Color.Crimson
Case = "TBN2"
DummyBtn.BackColor = Color.Yellow
End Select
source to share
If you use class-level declarations for buttons, you DummyBtn
never "connect" to them.
You need to do something like this before assigning BackColor
:
'Get a handle to the actual button
DummyBtn = CType(Me.ContainerName.Controls("Button" & y), Button)
Move Dim DummyBtn As New Button
so it is above the loop For y
and replace it with the following: -
Dim DummyBtn As Button
source to share