Question Changing colors in code

Pianoman

Member
Joined
Jul 22, 2013
Messages
7
Programming Experience
Beginner
Can anyone help? I'm writing an application that keeps track of pupils in my small music school. Everything's working apart from one small thing that I can't seem to solve.
Pupils pay for four lessons at a time so it was easy to take the start date and add 28 days to get the renewal date. Also, the renewal date has a backcolor green as long as the renewal date is ahead of the current system date. What I'm trying to do is get the backcolor to change to red if the renewal date is equal to, or greater than the system date, to highlight that the renewal is due.
The textboxes are created as controls in code as follows and I've been trying to change the backcolor of the textbox named "RenewalDate". Clearly I'm doing something wrong as everything I've tried either makes no change, or makes the control disappear altogether.

Dim TBS(200)
Dim j As Integer
Dim MyReadWrite As New ReadWrite
Dim btnRenew(200) As Button
Dim r As Integer = 100




Private Sub StudentList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyReadWrite.read()
TextBox1.Text = Format(Today, "dd/MM/yyyy")
TextBox2.Text = ReadWrite.Renewal(ReadWrite.a - 1)
TextBox3.Text = TextBox1.Text
TextBox4.Text = TextBox2.Text


For n = 1 To ReadWrite.SintCount
j = n
TBS(n) = New TextBox
TBS(n).Name = "RegNum" & n.ToString
TBS(n).Text = ReadWrite.RegNo(n)
TBS(n).Size = New Size(55, 20)
TBS(n).Location = New Point(34, r)
TBS(n).BorderStyle = BorderStyle.Fixed3D
Me.Controls.Add(TBS(n))


TBS(n) = New TextBox
TBS(n).Name = "Name" & n.ToString
TBS(n).Text = ReadWrite.ForeName(n) & " " & ReadWrite.SurName(n)
TBS(n).Size = New Size(155, 20)
TBS(n).Location = New Point(95, r)
TBS(n).BorderStyle = BorderStyle.Fixed3D
Me.Controls.Add(TBS(n))


TBS(n) = New TextBox
TBS(n).Name = "Age" & n.ToString
TBS(n).Text = ReadWrite.Age(n)
TBS(n).Size = New Size(45, 20)
TBS(n).Location = New Point(257, r)
TBS(n).BorderStyle = BorderStyle.Fixed3D
Me.Controls.Add(TBS(n))


TBS(n) = New TextBox
TBS(n).Name = "Subject" & n.ToString
TBS(n).Text = ReadWrite.Subject(n)
TBS(n).Size = New Size(95, 20)
TBS(n).Location = New Point(308, r)
TBS(n).BorderStyle = BorderStyle.Fixed3D
Me.Controls.Add(TBS(n))


TBS(n) = New TextBox
TBS(n).Name = "Room" & n.ToString
TBS(n).Text = ReadWrite.Room(n)
TBS(n).Size = New Size(45, 20)
TBS(n).Location = New Point(409, r)
TBS(n).BorderStyle = BorderStyle.Fixed3D
Me.Controls.Add(TBS(n))


TBS(n) = New TextBox
TBS(n).Name = "Day" & n.ToString
TBS(n).Text = ReadWrite.Day(n)
TBS(n).Size = New Size(100, 20)
TBS(n).Location = New Point(460, r)
TBS(n).BorderStyle = BorderStyle.Fixed3D
Me.Controls.Add(TBS(n))


TBS(n) = New TextBox
TBS(n).Name = "Time" & n.ToString
TBS(n).Text = ReadWrite.Time(n)
TBS(n).Size = New Size(60, 20)
TBS(n).Location = New Point(566, r)
TBS(n).BorderStyle = BorderStyle.Fixed3D
Me.Controls.Add(TBS(n))


TBS(n) = New TextBox
TBS(n).Name = "Paid" & n.ToString
TBS(n).Text = ReadWrite.Paid(n).ToString("C")
TBS(n).Size = New Size(80, 20)
TBS(n).Location = New Point(632, r)
TBS(n).BorderStyle = BorderStyle.Fixed3D
Me.Controls.Add(TBS(n))


TBS(n) = New TextBox
TBS(n).Name = "RenewalDate" & n.ToString
TBS(n).Text = ReadWrite.Renewal(n).ToShortDateString
TBS(n).Size = New Size(80, 20)
TBS(n).Location = New Point(720, r)
TBS(n).BorderStyle = BorderStyle.Fixed3D
TBS(n).BackColor = Color.Green
TBS(n).ForeColor = Color.White
If TextBox3.Text = TextBox4.Text Then
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox And
ctrl.Name = "RenewalDate" & j Then
TBS(j).BackColor = New Color = Color.Red
End If
Next
End If
Me.Controls.Add(TBS(n))

btnRenew(n) = New Button
btnRenew(n).Name = "Renewed" & n.ToString("C")
btnRenew(n).Text = "Renew"
btnRenew(n).Size = New Size(60, 20)
btnRenew(n).Location = New Point(823, r)
btnRenew(n).Parent = Me
AddHandler btnRenew(n).Click, AddressOf btn_Click
Me.Controls.Add(btnRenew(n))




r += 25

Next

r = 100


End Sub
 
Hi,

From what I can see you error is here:-

VB.NET:
If TextBox3.Text = TextBox4.Text Then
  For Each ctrl As Control In Me.Controls
    If TypeOf ctrl Is TextBox And ctrl.Name = "RenewalDate" & j Then
      TBS(j).BackColor = New Color = Color.Red
   End If
  Next
End If

Firstly, and again based on what I can see, TextBox3 and TextBox4 do not exist or do not contain values for testing the Renewal date. Should this not be:-

VB.NET:
If ReadWrite.Renewal(n) <=Now Then ....

Secondly, once you are happy that you have your If statement correct you are trying to set the BackColor to a BOOLEAN value based on:-

VB.NET:
TBS(j).BackColor = New Color = Color.Red

This should be:-

VB.NET:
TBS(j).BackColor = Color.Red

There are many other things here that I could mention, but for the moment, I will just stick with one additional comment which would be "Have you considered using a DataGridView for this?" It might help for the future, but then this depends on how your ReadWrite class actually works.

Hope that helps.

Cheers,

Ian

BTW, When posting code, please use the Advanced Button to post your question where you will find a button for encasing your code in Code tags. It helps to keep the formatting of the Code which makes things a lot easier to read.
 
Back
Top