Vb.Net - Class to Change Textbox BackColor Dynamically - vb.net

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.

Related

How can I change one label out of many by right clicking one of them

I have some simple code. It changes the BorderStyle property of a Label by right-clicking. Nothing fancy, but still. However, I have twenty labels. Is there a simpler way of doing this instead of "copy-paste" this code 20 times?
Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Label1.MouseDown
If e.Button = MouseButtons.Right Then
If Label1.BorderStyle = BorderStyle.None Then
Label1.BorderStyle = BorderStyle.FixedSingle
Else
Label1.BorderStyle = BorderStyle.None
End If
End If
End Sub
Private Sub Label2_MouseDown...
...
End Sub
You could either create a custom control which inherits from Label and has the behaviour you want, or you could write a handler which works out which control it is responding to from the sender parameter.
The latter, presented first here, is simpler for a one-off, but the former would be more re-usable, and you wouldn't have to maintain the list of Labels for the AddHandler.
Sub Label_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim lbl = DirectCast(sender, Label)
If e.Button = MouseButtons.Right Then
If lbl.BorderStyle = BorderStyle.None Then
lbl.BorderStyle = BorderStyle.FixedSingle
Else
lbl.BorderStyle = BorderStyle.None
End If
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each l In {Label1, Label2}
AddHandler l.MouseDown, AddressOf Label_MouseDown
Next
End Sub
The AddHandler line connects the MouseDown event of each of the Labels to the specified event handler. (You can add more than one event handler to an event, if needed.)
For a control (your very own custom one) derived from an existing control (a System.Windows.Forms.Label in this case), let's call it BorderedControl, you can follow the instructions at How to: Inherit from Existing Windows Forms Controls (it's too close to plagiarism to copy it to here), and then your code for the control might look like:
Public Class BorderedLabel
Inherits Label
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
If e.Button = MouseButtons.Right Then
If Me.BorderStyle = BorderStyle.None Then
Me.BorderStyle = BorderStyle.FixedSingle
Else
Me.BorderStyle = BorderStyle.None
End If
End If
MyBase.OnMouseDown(e)
End Sub
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
After you have built your project after adding that code, you will find a new control, named "BorderedLabel", in the ToolBox. You can drag that onto the form "design surface" and it will behave just like an ordinary Label except that it will have your BorderStyle-changing code incorporated automatically.

VB .net Change text of selected textbox on button click

I have 10 buttons and I want to click on a button so it changes text of the focused textbox and switch to next textbox.
i tried this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Focus()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Focus Then
TextBox1.Text = "1"
TextBox1.Enabled = False
TextBox2.Focus()
TextBox2.Enabled = True
End If
If TextBox2.Focus Then
TextBox2.Text = "1"
TextBox2.Enabled = False
TextBox3.Focus()
TextBox3.Enabled = True
End If
If TextBox3.Focus Then
TextBox3.Text = "1"
TextBox3.Enebled= False
TextBox4.Focus()
TextBox4.Enabled = True
End If
End Sub
But it writes the value in every textbox instead of just going to the next textboxt
This code does not do what you think it does:
If TextBox1.Focus Then
Focus is not a boolean property. In VB.Net, you can call methods without parentheses, and that's what you're doing here. The conditional block actually tries to set the focus. And since this is always gonna succeed unless you explicitly handle the event and block it, all of those If conditions result in True.
To find which control has focus, do this:
Public Shared Function FindFocusedControl(control As Control) As Control
Dim container = TryCast(control,IContainerControl)
While container IsNot Nothing
control = container.ActiveControl
container = TryCast(control, IContainerControl)
End While
Return control
End Function
In your Click event handler, you are calling the Focus method of each TextBox in turn and then populating them if it succeeds. It will succeed every time so you populate every TextBox.
I suspect that what you meant to do was test the Focused property rather than call the Focus method. That would make more sense because then it would only populate the TextBox that had focus. That is still flawed though, because the Button that you just clicked will have focus, so you won't actually populate any TextBox.
You have two main choices here. Firstly, you could use a custom Button control that will not take focus when it is clicked. That way, the TextBox that had focus when you clicked will still have focus. Alternatively, you could remember which control last had focus by assigning it to a field and using that. That's probably the way that I'd go.
Here's a quick (i.e. not rigorous) example of the second option:
Private lastActiveControl As Control
Private Sub TextBoxes_Leave(sender As Object, e As EventArgs) Handles TextBox4.Leave,
TextBox3.Leave,
TextBox2.Leave,
TextBox1.Leave
lastActiveControl = DirectCast(sender, Control)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim textBoxes = New Control() {TextBox1, TextBox2, TextBox3, TextBox4}
Dim lastTextBoxIndex = Array.IndexOf(textBoxes, lastActiveControl)
If lastTextBoxIndex <> -1 Then
Dim nextTextBoxIndex = (lastTextBoxIndex + 1) Mod textBoxes.Length
Dim nextTextBox = textBoxes(nextTextBoxIndex)
lastActiveControl.Text = "1"
lastActiveControl.Enabled = False
nextTextBox.Enabled = True
nextTextBox.Select()
End If
End Sub

Detect if a combobox is clicked without using SelectedIndexChange

I would like to ask if it is possible for VB.net applications to detect if a combobox has been changed without using SelectedIndexChange.
For Example my application has 4 comboboxes and this comboboxes was dynamically created from the user configuration. (the tool autocreates the comboboxes when the application was launched). Then once the application was launched, I want to run a SUB everytime those comboboxes was changed. Badly needed help. TIA
If you are creating controls dynamically, you should add the event handlers dynamically as well. There is nothing wrong with using the SelectedIndexChanged event.
You can test this by making a new project and pasting this code inside Public Class Form1.
Private myComboBox1 As ComboBox
Private myComboBox2 As ComboBox
Private Shared selectedIndexChanged As EventHandler =
Sub(sender As Object, e As EventArgs)
Dim myComboBox = DirectCast(sender, ComboBox)
' alert the user as to what was selected
MessageBox.Show(String.Format("{0} value: {1}, index: {2}",
myComboBox.Name, myComboBox.Text, myComboBox.SelectedIndex))
' you can do something different on each one by name in a case statement
Select Case myComboBox.Name
Case "myComboBox1"
' do something for 1
Case "myComboBox2"
' do something for 2
End Select
End Sub
Private Sub addHandlers()
AddHandler myComboBox1.SelectedIndexChanged, selectedIndexChanged
AddHandler myComboBox2.SelectedIndexChanged, selectedIndexChanged
End Sub
Private Sub removeHandlers()
RemoveHandler myComboBox1.SelectedIndexChanged, selectedIndexChanged
RemoveHandler myComboBox2.SelectedIndexChanged, selectedIndexChanged
End Sub
Form eventhandlers
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' dynamically generate combo boxes
myComboBox1 = New ComboBox() With {.Name = "myComboBox1",
.Left = 30,
.Top = 30}
myComboBox2 = New ComboBox() With {.Name = "myComboBox2",
.Left = 30,
.Top = 60}
' add some items
myComboBox1.Items.AddRange({1, 2, 3})
myComboBox2.Items.AddRange({"four", "five", "six"})
' add the combo boxes to the form
Me.Controls.Add(myComboBox1)
Me.Controls.Add(myComboBox2)
' add event handlers
addHandlers()
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
removeHandlers()
End Sub

Change focus color when button clicked

I have few buttons on my system and I tried to change the color focus when button is clicked. So far my coding is only able to change the button color when clicked but I want my system be able to reset the button color back to it's normal color as well when other button is clicked.
I tried to find solution on website but I don't really understand how because their sample is too complicated for me.
Here is my simple coding to change the button color focus.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Button1.BackColor = Color.Gainsboro
Me.Button1.ForeColor = Color.Black
End Sub
Kindly to help me. Thank you.
Since the user can focus on buttons without click, it's better to handle GotFocus and LostFocus events for buttons and put your logic there.
In below code, I assigned a handler to those events for all buttons in form and stored original ForeColor and BackColor in a data structure in Tag property. Then in GotFocus I set the ForeColor and BackColor to desired focusedForeColor and focusedBackColor. Also in LostFocus I restore original forecolor and backcolor that I stored previously in Tag.
It's enough to paste this code in your form code and it will work for all buttons:
'Change these to your desired color
Private focusedForeColor As Color = Color.Black
Private focusedBackColor As Color = Color.Gainsboro
Private Function GetAllControls(control As Control) As IEnumerable(Of Control)
Dim controls = control.Controls.Cast(Of Control)()
Return controls.SelectMany(Function(ctrl) GetAllControls(ctrl)).Concat(controls)
End Function
Public Sub New()
InitializeComponent()
Me.GetAllControls(Me).OfType(Of Button)().ToList() _
.ForEach(Sub(b)
b.Tag = Tuple.Create(b.ForeColor, b.BackColor)
AddHandler b.GotFocus, AddressOf b_GotFocus
AddHandler b.LostFocus, AddressOf b_LostFocus
End Sub)
End Sub
Private Sub b_LostFocus(sender As Object, e As EventArgs)
Dim b = DirectCast(sender, Button)
Dim colors = DirectCast(b.Tag, Tuple(Of Color, Color))
b.ForeColor = colors.Item1
b.BackColor = colors.Item2
End Sub
Private Sub b_GotFocus(sender As Object, e As EventArgs)
Dim b = DirectCast(sender, Button)
b.ForeColor = focusedForeColor
b.BackColor = focusedBackColor
End Sub
In the declarations section create 2 Color variables, one for the background property and another for the forecolor property. You have to assign the Background color and Foreground color properties of Button1 to these variables in the event Load of the form. When you click Button1 it changes with the code you did and when you click the other button it restored the Button1 colors through the use of the color variables. I hope this explanation help you. Below is the full code for further clarification.
Public Class Form1
Dim bgColor, foColor As Color
Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click
Button1.BackColor = Color.Yellow
Button1.ForeColor = Color.Blue
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) _
Handles Button2.Click
Button1.BackColor = bgColor
Button1.ForeColor = foColor
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
bgColor = Button1.BackColor
foColor = Button1.ForeColor
End Sub
End Class

Using handlers across multiple forms?

I have code that highlights the current textbox in focus in order to provide a visual cue to the user. My question is, if I had 10 forms with textboxes and I wanted to provide this same code to them all. Would I have to duplicate it or can I use a global method? If so, an example would be very helpful. Thanks.
The code is as follows.
Private Sub FocusChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim txt As TextBox = sender
If txt.Focused Then
txt.Tag = txt.BackColor
txt.BackColor = Color.AliceBlue
Else
txt.BackColor = txt.Tag
End If
End Sub
Private Sub CreateAccount_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each ctrl As TextBox In Me.Controls.OfType(Of TextBox)()
AddHandler ctrl.GotFocus, AddressOf FocusChanged
AddHandler ctrl.LostFocus, AddressOf FocusChanged
ctrl.Tag = ctrl.BackColor
Next
End Sub
If you want to add this behavior to all TextBox controls, you're better off deriving your own class from the TextBox class, and override the OnGotFocus and OnLostFocus methods to set the properties accordingly.
Here's how:
Public Class MyTextBox
Inherits TextBox
Protected Overrides Sub OnGotFocus(e As System.EventArgs)
MyBase.OnGotFocus(e)
Me.Tag = Me.BackColor
Me.BackColor = Color.Aqua
End Sub
Protected Overrides Sub OnLostFocus(e As System.EventArgs)
MyBase.OnLostFocus(e)
Me.BackColor = Me.Tag
End Sub
End Class
EDIT: forgot to mention that after adding that class to your project, rebuild the solution, and if it compiles without errors, then your new TextBox class show show up in the VS ToolBox. You can then simply drag & drop onto your form just as any control.
Cheers