Change focus color when button clicked - vb.net

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

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.

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 to create this control/button in VB.NET?

can someone help me, how to create button/control like that in VB.NET .. that i mean, how to create/use style like that when i pointing the cursor or click the control, the style like transparent. that style different from button style.. please help me.. i have search everywhere but nothing. thank's before
Here is the image:
If you want to accomplish what you are looking for take a look at this example it might help you work out any issues you have:
Sub Main()
Dim myButton As Button = New Button()
myButton.Text = "Hello World"
myButton.BackColor = Color.Blue
myButton.FlatAppearance.BorderColor = Color.LightBlue
myButton.FlatAppearance.BorderSize = 2
AddHandler myButton.Click, AddressOf Me.OnMyButtonClick
AddHandler myButton.MouseHover, AddressOf Me.OnMyButtonEnter
AddHandler myButton.MouseLeave, AddressOf Me.OnMyButtonLeave
Panel1.Controls.Add(myButton)
End Sub
As you can see I have created a new button, and can modify all of the attributes of the button that you were looking for: Back color, Border color, and Border width, even the content like text or the image. Now you will want to create the events that will perform the actions that you are looking to accomplish:
Private Sub OnMyButtonClick(sender As Object, e As EventArgs)
Dim currentButton As Button = CType(sender, Button)
currentButton.Text = "I have been clicked!"
currentButton.BackColor = Color.LightBlue
currentButton.FlatAppearance.BorderColor = Color.Blue
currentButton.FlatAppearance.BorderSize = 1
End Sub
Private Sub OnMyButtonEnter(sender As Object, e As EventArgs)
Dim currentButton As Button = CType(sender, Button)
currentButton.BackColor = Color.LightGreen
End Sub
Private Sub OnMyButtonLeave(sender As Object, e As EventArgs)
Dim currentButton As Button = CType(sender, Button)
currentButton.BackColor = Color.Blue
End Sub
As you can see we have defined the 3 events you were looking to capture Mouse Click, Mouse Hover, and Mouse Leave, and within each one we can modify the button as we please. From here you can make a global parameter that monitors the color of the button, or you can monitor the state, from there you can know within each event what you want to do. Take for instance the Mouse Leave event.
You showed that you want to create a border, but have a white background as you can see, all you would have to do is just:
myButton.BackColor = Color.White
myButton.FlatAppearance.BorderColor = Color.LightBlue
That's easy enough, but there is one very important part that you need to know. If you want your button to actually perform events, ones that you created, you have to attach them to the button. Lets that a look at this:
AddHandler myButton.Click, AddressOf Me.OnMyButtonClick
AddHandler myButton.MouseHover, AddressOf Me.OnMyButtonEnter
AddHandler myButton.MouseLeave, AddressOf Me.OnMyButtonLeave
As you can see from the above code you instruct the program what to do when a certain event occurs such as Mouse Click. An important part to do with event handlers is to attach them after creation so that they will perform the actions right away. That is why I placed the attachment of the event handler in the Main() before I added the button to Panel1.
Let's try with this:
Import your image to resource.
You will need 3 different image: Normal, Hover and Clicked. Change this to your image name: CustomButtonN, CustomButtonH and CustomButtonC.
Add a Class with any name. Then add the code below:
Inherits Windows.Forms.Button 'To make this class as button command
Public Sub New()
Me.FlatStyle = Windows.Forms.FlatStyle.Flat 'Make it flat
Me.FlatAppearance.BorderSize = 0 'With borderless
Me.FlatAppearance.MouseDownBackColor = Color.Transparent 'You know this
Me.FlatAppearance.MouseOverBackColor = Color.Transparent 'And this
Me.BackColor = Color.Transparent 'Transparent key
Me.ForeColor = Color.Black 'Text color
Me.Width = 32 'Button width
Me.Height = 32 'Button height
Me.BackgroundImage = My.Resources.CustomButtonN 'Normal
Me.BackgroundImageLayout = ImageLayout.Stretch 'To stretch the image
End Sub
Private Sub CustomButton_MouseDown(ByVal sender As Object, _
ByVal e As MouseEventArgs) Handles Me.MouseDown
Me.BackgroundImage = My.Resources.CustomButtonC 'Click event
End Sub
Private Sub CustomButton_MouseHover(ByVal sender As Object, _
ByVal e As EventArgs) Handles Me.MouseHover
Me.BackgroundImage = My.Resources.MiniButtonH 'Hover event
End Sub
Private Sub CustomButton_MouseLeave(ByVal sender As Object, _
ByVal e As EventArgs) Handles Me.MouseLeave
Me.BackgroundImage = My.Resources.CustomButtonN 'Leave even
End Sub
Private Sub CustomButton_MouseUp(ByVal sender As Object, _
ByVal e As MouseEventArgs) Handles Me.MouseUp
Me.BackgroundImage = My.Resources.CustomButtonH 'Click release
End Sub
Note: You must compile the project after adding a class or editing:
Press F5 to start it and then wait for 3 sec, then stop it.
Finally see your Toolbox panel click your button name then put to your form.

Graphics.DrawRectangle not working in control events

First of all thank you for taking the time out of your busy schedule to assist me.
I am developing a project (Win Application) with a Form and 3 textboxes (TextBox1, TextBox2 and TextBox3).
I need draw a rectangle around the textbox when focused this.
The code is:
Private Sub TextBox123_Enter(sender As Object, e As System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter, TextBox3.Enter
Using g As Graphics = Me.CreateGraphics
Dim r As Rectangle = sender.Bounds
r.Inflate(4, 4)
g.DrawRectangle(Pens.Blue, r)
End Using
End Sub
The problem is the following:
The first time the textbox1 gains focus rectangle is not drawn.
The first time the textbox2 gains focus rectangle is not drawn.
Why not the rectangle is drawn when the first two events enter are fired?
Drawing with CreateGraphics is almost always not the correct approach. If you notice also, when you move from one box to another, the old rectangle is not being erased. You need to use the Form_Paint event to get it to work right. Or...perhaps simpler would be to create a UserControls which is 1-2 pixels larger than a child TextBox and set the backcolor of the UserControl canvas, draw your rectangle when the control gets the focus.
For form paint:
Public Class Form1
Private HotControl As Control
If you are only going to do TextBoxes, you can declare it As TextBox. This way it allows you to do the same for other control types. Set/clear the tracker:
Private Sub TextBox3_Enter(sender As Object, e As EventArgs) Handles TextBox3.Enter,
TextBox2.Enter, TextBox1.Enter
HotControl = CType(sender, TextBox)
Me.Invalidate()
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave,
TextBox2.Leave, TextBox3.Leave
HotControl = Nothing
Me.Invalidate()
End Sub
The Me.Invalidate tells the form to redraw itself, which happens in Paint:
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
If HotControl IsNot Nothing Then
Dim r As Rectangle = HotControl.Bounds
r.Inflate(4, 4)
e.Graphics.DrawRectangle(Pens.Blue, r)
End If
End Sub
You should also turn on Option Strict.
Try this in the click event handler
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
Using g As Graphics = Me.CreateGraphics()
Dim rectangle As New Rectangle(TextBox1.Location.X - 1, _
TextBox1.Location.Y - 1, _
TextBox1.Size.Width + 1, _
TextBox1.Size.Height + 1)
g.DrawRectangle(Pens.Blue, rectangle)
End Using
End Sub

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.