I am new to this VB. I'm building an application where I need to fill some 48 text boxes with 4 sets of similar names like
L1onHHxx
L1onMMxx
L1offHHxx
L1offMMxx
Where xx is a number from 1 to 24 for each label.
What I'm trying to do is calculate some values based on the user input and fill the text boxes with results.
Private Async Sub Line1Calc_Click(sender As Object, e As EventArgs) Handles Line1Calc.Click
Dim Cycle_Time As Integer
Dim Cycle_Count As Integer
Dim srtHH As Integer
Dim srtMM As Integer
Dim stpHH As Integer
Dim stpMM As Integer
Dim inDelay As Integer
Dim i As Integer
Dim lineONHH As New Label
Dim lineONMM As New Label
Dim lineOFFHH As New Label
Dim lineOFFMM As New Label
'Dim controls = GetAllTextBoxes(Me)
'Line1Tab
Cycle_Time = CInt(L1Time.Text)
Cycle_Count = CInt(L1Count.Text)
srtHH = CInt(L1ONHH.Text)
srtMM = CInt(L1ONMM.Text)
stpHH = CInt(L1OFFHH.Text)
stpMM = CInt(L1OFFMM.Text)
inDelay = (24 / CInt(Cycle_Count))
L1onhh1.Text = srtHH
L1onhh1.Text = srtHH
L1offhh1.Text = stpHH
L1offmm1.Text = stpMM
For i = 2 To (Cycle_Count)
srtHH = inDelay
stpHH = inDelay
lineONHH.Name = "L1onhh" & i.ToString()
lineONMM.Name = "L1onmm" & i.ToString()
lineOFFHH.Name = "L1offhh" & i.ToString()
lineOFFMM.Name = "L1offmm" & i.ToString()
lineONHH.Text = srtHH
lineONMM.Text = srtMM
lineOFFHH.Text = stpHH
lineOFFMM.Text = stpMM
' To Check if the labels name are correct
Box1.Text = lineONHH.Name
Box2.Text = lineONMM.Name
Box3.Text = lineOFFHH.Name
Box4.Text = lineOFFMM.Name
Await Task.Delay(1000)
Next
End Sub
Here when I pass the value of the New label to Box 1,2,3 & 4 for test purposes they are correct but the values don't appear on the respective text boxes.
Please can anyone point me in the right direction of what I'm doing wrong?
CodePudding user response:
This should get you close, with a few assumptions made. It assumes you have constant textbox naming pattern. Assumes you want hour and minute in string. Switch that structure to a DateTime if you want it in that format.
'class to hold all your time value entries
Private Class TimeValue
Public Property HourValue As String
Public Property MinValue As String
End Class
'class for each control time slow and its associated timevalues
Private Class ControlTime
Public Property TimeSlot As String
Public Property OnValue As TimeValue
Public Property OffValue As TimeValue
End Class
'method to set control values
Private Function SetControlValus(controlTimeItem As ControlTime) As Boolean
'find the control
Dim OnHourTextBox As TextBox = DirectCast(Me.Controls.Find(String.Concat("L1onHH", controlTimeItem.TimeSlot), False)(0), TextBox)
OnHourTextBox.Text = controlTimeItem.OnValue.HourValue
Dim OnMinTextBox As TextBox = DirectCast(Me.Controls.Find(String.Concat("L1onMM", controlTimeItem.TimeSlot), False)(0), TextBox)
OnMinTextBox.Text = controlTimeItem.OnValue.MinValue
Dim OffHourTextBox As TextBox = DirectCast(Me.Controls.Find(String.Concat("L1offHH", controlTimeItem.TimeSlot), False)(0), TextBox)
OffHourTextBox.Text = controlTimeItem.OffValue.HourValue
Dim OffMinTextBox As TextBox = DirectCast(Me.Controls.Find(String.Concat("L1offMM", controlTimeItem.TimeSlot), False)(0), TextBox)
OffMinTextBox.Text = controlTimeItem.OffValue.MinValue
Return True
End Function
Private Sub Test()
'set up some results
Dim results As List(Of ControlTime) = New List(Of ControlTime)
results.Add(New ControlTime() With {.TimeSlot = "01",
.OnValue = New TimeValue() With {.HourValue = "08", .MinValue = "56"},
.OffValue = New TimeValue() With {.HourValue = "08", .MinValue = "58"}})
results.Add(New ControlTime() With {.TimeSlot = "02",
.OnValue = New TimeValue() With {.HourValue = "09", .MinValue = "14"},
.OffValue = New TimeValue() With {.HourValue = "09", .MinValue = "29"}})
For Each controlTimeItem In results
SetControlValus(controlTimeItem)
Next
End Sub
Private Sub TestControlTimeButton_Click(sender As Object, e As EventArgs) Handles TestControlTimeButton.Click
Try
Test()
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
End Try
End Sub