Best practice question for focusing on textboxes in vb.net - vb.net

I have a form to add a new client to a database. Built in vb.net 2019 Windows Forms. I want to accomplish 2 things:
When a user enters a textbox, change textbox backcolor to cyan and forecolor to navy to highlight that the user is editing that field.
When a user leaves a textbox, revert colors back to original but also update the existing text to uppercase.
I have this accomplished already, but, I'm wondering if my method is making unnecessary steps or whether or not it's best practice to add this to the enter and leave event for every single textbox.
Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
TextBox1.BackColor = Color.Cyan
TextBox1.ForeColor = Color.Navy
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
TextBox1.BackColor = Color.White
TextBox1.ForeColor = Color.Black
TextBox1.Text = TextBox1.Text.ToUpper
End Sub
Private Sub TextBox2_Enter(sender As Object, e As EventArgs) Handles TextBox2.Enter
TextBox2.BackColor = Color.Cyan
TextBox2.ForeColor = Color.Navy
End Sub
Private Sub TextBox2_Leave(sender As Object, e As EventArgs) Handles TextBox2.Leave
TextBox2.BackColor = Color.White
TextBox2.ForeColor = Color.Black
TextBox2.Text = TextBox2.Text.ToUpper
End Sub
I'm not asking for you to do the work for me- just point me in the right direction, so please don't close this question unless necessary. Legitimately curious if this method is best practice or if there's a more efficient way of handling this.

Related

How to make a label blink

I have a Stopwatch in my form with Interval = 1000 displayed in the hh:mm:ss format.
When it reaches the 5th second it should start to blink the label background as green but so far I can only make the background color turn to green without any flash.
This is how I turn the background color to green:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
If Label1.Text = "00:00:05" Then
Label1.BackColor = Color.Green
End If
End Sub
How do I make the label blink?
You could use a simple Async method to do this.
The following code will give Label1 the effect of flashing. Since we have used While True this will continue indefinitely once you hit "00:00:05".
Private Async Sub Flash()
While True
Await Task.Delay(100)
Label1.Visible = Not Label1.Visible
End While
End Sub
You would call this inside your Timer1_Tick method:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
If Label1.Text = "00:00:05" Then
Label1.BackColor = Color.Green
Flash()
End If
End Sub
If you only want to flash a couple of times we can make a simple change to Flash():
Private Async Sub Flash()
For i = 0 To 10
Await Task.Delay(100)
Label1.Visible = Not Label1.Visible
Next
'set .Visible to True just to be sure
Label1.Visible = True
End Sub
By changing the number 10 to a number of your choice you can shorten or lengthen the time taken to flash. I have added in Label1.Visible = True after the For loop just to be sure that we see the Label once the flashing has finished.
You will have to import System.Threading.Tasks to make use of Task.Delay.
You need a label, two textboxes, and a button.
The screen allows you to 'set' a couple of colours - this could be taken further, by adding an Error colour, a Warning colour (where you haven't filled a field in...?) and more.
This colour selection would, in a real application, be done by an admin person, from a separate screen, and stored in the DB.
The timer frequency would also be set in the Admin screen/function.
This particular screen needs the textboxes to be double-clicked, and a colour selected for each one.
The back colour for each box changes. Then press the Start button.
If you press the Start button again, it toggles the timer (on/off)
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' not quite correct for what I want, but close...
' https://bytes.com/topic/visual-basic-net/answers/368433-blinking-text
Me.Label1.Text = "A blinking text box"
Me.Label1.BackColor = TextBox2.BackColor
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Me.Label1.BackColor = TextBox2.BackColor Then
Me.Label1.BackColor = TextBox1.BackColor
Else
Me.Label1.BackColor = TextBox2.BackColor
End If
End Sub
Private Sub TextBox1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseDoubleClick
Dim dlg As New ColorDialog()
If dlg.ShowDialog() = DialogResult.OK Then
TextBox1.BackColor = dlg.Color
End If
End Sub
Private Sub TextBox2_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TextBox2.MouseDoubleClick
Dim dlg As New ColorDialog()
If dlg.ShowDialog() = DialogResult.OK Then
TextBox2.BackColor = dlg.Color
End If
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Timer1.Enabled = Not Timer1.Enabled
End Sub
End Class
Try to put something like this in Timer1_Tick event handler -
Label1.Visible = Not Label1.Visible
Set the timer to enabled and it will do the job.
If you specify the color when the Text is 00:00:05 then you should also specify what the Backcolor should be when the text is something else i.e 00:00:06
Try this and see if it works:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
If Label1.Text = "00:00:05" Then
Label1.BackColor = Color.Green
else
Label1.Backcolor = Color.Yellow '(Change color as needed)
End If
End Sub

How to create only one procedure that change the backcolor of whatever label that clicked?

Guys, say I have 3 labels named "lblTest1", "lblTest2", and "lblTest3". All labels have same backcolor, which is black.
lblTest1.BackColor = Color.Black
lblTest2.BackColor = Color.Black
lblTest3.BackColor = Color.Black
Now I want to change the backcolor of the labels to red when clicked so I need 3 different procedures for each event.
Private Sub lblTest1_Click(sender As Object, e As EventArgs) Handles lblTest1.Click
lblTest1.BackColor = Color.Red
End Sub
Private Sub lblTest2_Click(sender As Object, e As EventArgs) Handles lblTest2.Click
lblTest2.BackColor = Color.Red
End Sub
Private Sub lblTest3_Click(sender As Object, e As EventArgs) Handles lblTest3.Click
lblTest3.BackColor = Color.Red
End Sub
What I'm trying to do is to create only one subfunction that change the backcolor of whatever label that clicked to colors.red. Is it possible to do this? Can you explain it to me, please? Because currently I'm working with at least 300 labels right now, and not only change its color but other actions as well, so the solutions for this will be so much helpful. :)
You can handle multiple events in the same sub, like this:
Private Sub label_Click(sender As Object, e As EventArgs) Handles
lblTest1.Click, lblTest2.Click, lblTest3.Click
To get the label that was clicked, use the sender passed to the sub:
Private Sub label_Click(sender As Object, e As EventArgs) Handles
lblTest1.Click, lblTest2.Click, lblTest3.Click
cType(sender, Label).BackColor = Color.Red
End Sub
Same sub can handle events for multiple controls, (Labels) in this case.
So proceed like below:
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Label1.Click, Label2.Click, Label3.Click ...
Dim myLabel = DirectCast(sender, Label)
myLabel.ForeColor = Color.Red
End Sub

Toggle button color by calling function in vb.net

I'm new to vb.net. I have 20 buttons in one form. When I click any of one button, it color should be changed.
I can code for all button like following. But I need a function, when i call that function, the color should be changed. Please help me and give me full coding
Private Sub btnR1X1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnR1X1.Click
If (btnR1X1.BackColor = Color.White) Then
btnR1X1.BackColor = Color.Gray
ElseIf (btnR1X1.BackColor = Color.Gray) Then
btnR1X1.BackColor = Color.White
End If
End Sub
I have assumed that you are using VB.Net. Assuming that is the case, you should edit your question to remove the vb6 tag.
You can write a function that will toggle the BackColor of any control.
Private Sub ToggleColor(ctrl As Control)
If ctrl.BackColor = Color.White Then ctrl.BackColor = Color.Gray Else ctrl.BackColor = Color.White
End Sub
You can call that function from a Button's click handler like this
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ToggleColor(CType(sender, Control))
End Sub
However, if all you want to do when any of the buttons is clicked is to toggle the BackColor, you can use a single event handler for the click event of every button.
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click 'etc
Dim ctrl as Control = CType(sender, Control)
If ctrl.BackColor = Color.White Then ctrl.BackColor = Color.Gray Else ctrl.BackColor = Color.White
End Sub

How do I get a "show password" effect in vb.net?

I am making an email login program in visual basic 2015 with various options, one of them being the option to show password or not with probably a check box. It is just a simple, standard windows forms application, I am relatively new to it.
Edit: Answered by jmcilhinney. Thanks! :D
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
'Display plain text if and only if check box is checked.
TextBox1.UseSystemPasswordChar = Not CheckBox1.Checked
End Sub
Set the UseSystemPasswordChar property of your TextBox to True to mask the password.
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
'Display plain text if and only if check box is checked.
TextBox1.UseSystemPasswordChar = Not CheckBox1.Checked
End Sub
Private Sub txtPassword_MouseHover(sender As Object, e As EventArgs) Handles txtPassword.MouseHover
txtPassword.PasswordChar = ""
End Sub
Private Sub txtPassword_MouseLeave(sender As Object, e As EventArgs) Handles txtPassword.MouseLeave
txtPassword.PasswordChar = "*"
End Sub
You have to do couple of changes. Change your password textbox UseSystemPasswordChar to false and the PasswordChar to Char(0). Something like this:
If chkViewPassword.Checked Then txbPwd.PasswordChar = Convert.ToChar(0) Else txbPwd.PasswordChar = Convert.ToChar("*")
txbSMTPPwd.UseSystemPasswordChar = Not chkViewPassword.Checked

Vb.Net - Class to Change Textbox BackColor Dynamically

I'd like to know how to create a Class to change each textbox BackColor inside a Form.
To be more Specific:
When the textbox Is Empty, the textbox BackColor equals White.
When the textbox Get focus, the textbox BackColor change.
When the textbox have any text, the textbox BackColor change.
When the textbox Lost focus, the textbox BackColor change.
At the moment, I'm doing it this way.
Private Sub tb_Login_Enter(sender As Object, e As EventArgs) Handles tb_Login.Enter
tb_Login.BackColor = Color.LightCyan
End Sub
Private Sub tb_Login_Leave(sender As Object, e As EventArgs) Handles tb_Login.Leave
If tb_Login.Text <> "" Then
tb_Login.BackColor = Color.LightGreen
Else
tb_Login.BackColor = Color.White
End If
But, I have many TextBox in my from, so, how can I create a Class for it?
Thanks
All you need to do is inherit from the TextBox control.
Public Class TextBoxEx
Inherits TextBox
Private Sub TextBoxEx_Enter(sender As Object, e As EventArgs) Handles Me.Enter
Me.BackColor = Color.LightCyan
End Sub
Private Sub TextBoxEx_Leave(sender As Object, e As EventArgs) Handles Me.Leave
If Me.Text <> "" Then
Me.BackColor = Color.LightGreen
Else
Me.BackColor = Color.White
End If
End Sub
End Class
Build your project and then replace your TextBox controls with the new TextBoxEx control.
You can create a class that has a collection of textbox controls. You can get this collection going through the Controls property of your Form or user control and verifying the type of the control.
Internally the class must subscribe to the events you've listed, of the textbox controls collection.
Finally, on the methods that handle the events you must write the logic that change the color accordingly.
Remember that the handle events methods have the control that triggered the event on the first parameter.
I can go into more detail if you have more doubts.
Like in the movie...... ten years later......
Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each tb As TextBox In Controls.OfType(Of TextBox)()
AddHandler tb.Enter, Sub() tb.BackColor = Color.Red
AddHandler tb.Leave, Sub() tb.BackColor = Color.White
Next
End Sub
With this one there's no problem even with MaskedTextBox.