Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good morning i have a problem can anyone help me.I have created a form in vs2013 in Vb.net. The purpose of the form is a staff rota for a month, on a 3 shift system of 8 Hours.Person creating rota uses the textbox back color of each employee to display the shift the staff member is on. IE .. Red back color is 6am-2pm, green is 2pm-10pm etc. The color is change via the click event each click on day shift is changed to the next color. The code below works well for changing the text boxes to the required output. My question is ..... Is there a way of instead of copy and paste the code below to each text box click event and editing to the text name to the required text box is it possible to write code onece in a class or procedure that will cover all text boxes thus saving time writing code for all 50 plus text boxes????

Thank you for anyone input/advice in advance

What I have tried:

Dim Count As Integer = 5

**Code Below is of one text box click event

Private Sub txtM16_Click(sender As Object, e As EventArgs) Handles txtM11.Click
Count = Count - 1
If Count = 4 Then
txtM16.BackColor = Color.Red
ElseIf Count = 3 Then
txtM16.BackColor = Color.Green
ElseIf Count = 2 Then
txtM16.BackColor = Color.MediumPurple
ElseIf Count = 1 Then
txtM16.BackColor = Color.Blue
ElseIf Count = 0 Then
txtM16.BackColor = Color.White
Count = 5
End If
End Sub
Posted
Updated 14-Mar-17 0:55am

1 solution

Yes: the sender parameter contains the source control that raised the event.
So all you need to do is set all the Textboxes to use the same handler and use the parameter to control which box:

VB
Private Sub AllTextBoxes_Click(sender As Object, e As EventArgs) Handles txtM11.Click
...
    Count = Count - 1
    Dim tb As TextBox = TryCast(sender, TextBox)
    If tb IsNot Nothing Then
        If Count = 4 Then
            tb.BackColor = Color.Red
        ElseIf Count = 3 Then
            tb.BackColor = Color.Green
        ElseIf Count = 2 Then
            tb.BackColor = Color.MediumPurple
        ElseIf Count = 1 Then
            tb.BackColor = Color.Blue
        ElseIf Count = 0 Then
            tb.BackColor = Color.White
            Count = 5
        End If
    End If
 
Share this answer
 
Comments
Member 13010705 14-Mar-17 8:11am    
Thank you for your help exactly what i was looking for ... thank you again
OriginalGriff 14-Mar-17 8:19am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900