How to create only one procedure that change the backcolor of whatever label that clicked? - vb.net

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

Related

How can I use the VB.NET Key press

I created a mouse position program that can be used to save your mouse position {X, Y}
I realised that this is not going to be effective unless I implement a method where for example pressing "5" will save that position
The only way i can save the position is by pressing the button, although that does work, there is no way to save the position without clicking the btn.
Can anyone help? I would be very grateful
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub XYbtn_Click(sender As Object, e As EventArgs) Handles XYbtn.Click
Dim mousep As Point = MousePosition
MouseXY.Text = mousep.ToString()
TimeCo.Start()
End Sub
Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
LabelX.Text = "X"
LabelY.Text = "Y"
X2.Text = "X2"
Y2.Text = "Y2"
End Sub
Private Sub TimeCo_Tick(sender As Object, e As EventArgs) Handles TimeCo.Tick
Dim mousep As Point = MousePosition
MouseXY.Text = mousep.ToString()
End Sub
Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click
LabelX.Text = Cursor.Position.X
LabelY.Text = Cursor.Position.Y
End Sub
Private Sub save2_Click(sender As Object, e As EventArgs) Handles save2.Click
X2.Text = Cursor.Position.X
Y2.Text = Cursor.Position.Y
End Sub
Private Sub startBtn_Click(sender As Object, e As EventArgs) Handles startBtn.Click
End Sub
End Class
If your form will have focus, you can set the AcceptButton property of the FORM to saveBtn. This will make it so that when you press ENTER on the keyboard while your form has focus then that button will be pressed.
If you'd rather use the key approach then set the KeyPreview property of the Form to True and handle the KeyPress event:
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = "5" Then
Console.WriteLine("5")
End If
End Sub

hello! there is someone who can help me on my project? this is my code

Public Class frmColor
Dim red, green, yellow, blue, orange As New frmChanger
Private Sub BtnRed_Click(sender As Object, e As EventArgs) Handles BtnRed.Click
frmChanger.Show(red)
End Sub
Private Sub BtnGreen_Click(sender As Object, e As EventArgs) Handles BtnGreen.Click
frmChanger.Show(green)
End Sub
Private Sub BtnYellow_Click(sender As Object, e As EventArgs) Handles BtnYellow.Click
frmChanger.Show(yellow)
End Sub
Private Sub BtnBlue_Click(sender As Object, e As EventArgs) Handles BtnBlue.Click
frmChanger.Show(blue)
End Sub
Private Sub BtnOrange_Click(sender As Object, e As EventArgs) Handles BtnOrange.Click
frmChanger.Show(orange)
End Sub
End Class
that is my code on form1....
Public Class frmChanger
Private Sub PnlRed_Paint(sender As Object, e As PaintEventArgs) Handles PnlRed.Paint
PnlRed.BackColor = Color.Red
End Sub
Private Sub PnlGreen_Paint(sender As Object, e As PaintEventArgs) Handles PnlGreen.Paint
PnlGreen.BackColor = Color.Green
End Sub
Private Sub PnlYellow_Paint(sender As Object, e As PaintEventArgs) Handles PnlYellow.Paint
PnlYellow.BackColor = Color.Yellow
End Sub
Private Sub PnlBlue_Paint(sender As Object, e As PaintEventArgs) Handles PnlBlue.Paint
PnlBlue.BackColor = Color.Blue
End Sub
Private Sub PnlOrange_Paint(sender As Object, e As PaintEventArgs) Handles PnlOrange.Paint
PnlOrange.BackColor = Color.Orange
End Sub
End Class
and this is my code on form2....
what i need is, when i clicked the btnRed on form1, on form2 only the PnlRed show..
when the BtnGreen is clicked, Pnlgreen on form2..
my problem is both panel colors show when i click only one button.. what should i do? can someone help me please.
Add a Form level variable to frmChanger with its datatype as Color and its scope as Friend (so it can be seen from frmColor). This code is using the default instance of the form. You don't really need a separate form for each color. You only want to have a different color shown. How about a single panel that will have different colors instead of multiple panels.
In the Form1 code (your frmColor)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.panelColor = Color.Red
Form2.Show()
End Sub
Repeat for the other buttons with = Color.Green etc.
In Form2 (your frmChanger)
Friend panelColor As Color
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Panel1.BackColor = panelColor
End Sub

How do i change the button color in vb.net with an enter action?

I added a Button (name: btn_exit) with the action: If the cursor leaves the button it should turn red, just as a signal. In my application that doesn't work like i thought. The Console says there no errors. So what do I to adjust to change the color ?
My Code:
Private Sub btn_exit_Leave(sender As Object, e As EventArgs) Handles btn_exit.Leave
btn_exit.BackColor = Color.Red
End Sub
You can not do this with the 'Leave' event, you have to use the MouseEnter, MouseHover, MouseLeave events.
Example Code (just tested): The button is "normal" until you move the mouse first time over it, then turns to blue and when you leave the mouse then red Background.
Private Sub Button1_MouseEnter(sender As Object, e As EventArgs) Handles Button1.MouseEnter
Button1.BackColor = Color.Blue
End Sub
Private Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave
Button1.BackColor = Color.Red
End Sub
Try this:
Private Sub Button1_MouseEnter(sender As Object, e As EventArgs) Handles Button1.MouseEnter
Button1.BackColor = Color.Blue
End Sub
Private Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave
Button1.BackColor = Color.Red
End Sub

Different Panels' MouseEnter and MouseLeave conflict on Event Order

Here, I have two side-by-side panel the first_pnl and second_pnl, the second panel is not visible by default. Initial thoughts of what I need:
If my cursor is over the the first one (MouseEnter)
It's BackColor would change to Black
Second panel would be visible
If my cursor leaves first one (MouseLeave)
It's BackColor would change back to Gray
Second panel would not be visible
Which is simple as:
Private Sub PanelMouseEnter(sender As Object, e As EventArgs) _
Handles first_pnl.MouseEnter
first_pnl.BackColor = Color.Black
second_pnl.Visible = True
End Sub
Private Sub PanelMouseLeave(sender As Object, e As EventArgs) _
Handles first_pnl.MouseLeave
first_pnl.BackColor = Color.Gray
second_pnl.Visible = False
End Sub
But what I want to happen is:
When the cursor moves to the second panel (which by now is visible)
second_pnl would remain visible unless the cursor leave its area.
It sustains first_pnl property as if it where on a MouseEnter event
And here's the scenario to be clear:
And here's my logic of making that possible: (Giving their same events with same code)
Private Sub PanelMouseEnter(sender As Object, e As EventArgs) _
Handles first_pnl.MouseEnter, second_pnl.MouseEnter
first_pnl.BackColor = Color.Black
second_pnl.Visible = True
End Sub
Private Sub PanelMouseLeave(sender As Object, e As EventArgs) _
Handles first_pnl.MouseLeave, second_pnl.MouseLeave
first_pnl.BackColor = Color.Gray
second_pnl.Visible = False
End Sub
Looks reasonable, but I think the system consider first the MouseLeave of first_pnl before it even consider the MouseEnter of second_pnl.
Any way to do it?
jmcilhinney's comment solves this easily.
Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
first_pnl.BackColor = Color.Gray
second_pnl.Visible = False
End Sub
Private Sub first_pnl_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles first_pnl.MouseEnter
first_pnl.BackColor = Color.Black
second_pnl.Visible = True
End Sub

Changing the background color for Form2

i have two RadioButtons one for Light Blue and the other for Ghost White and a button to show the next Form (Form2)
i want to be able to check on a Radio Button and the backcolor of form2 changes to the checked Radio Button
this is what i have on my coding so far
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SecondForm.Show()
End Sub
Private Sub rbLightBlue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbLightBlue.CheckedChanged
If rbLightBlue.Checked Then
SecondForm.BackColor = (Color.LightBlue)
End If
End Sub
Private Sub rbGhostWhite_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbGhostWhite.CheckedChanged
If rbGhostWhite.Checked Then
SecondForm.BackColor = (Color.GhostWhite)
End If
End Sub
The problem am having is making the background color change on Form2.
Any answer for this question will be very helpful.
I am not sure what you are doing, it probably has to do with how you are creating your SecondForm, this code does work, see if it helps you narrow it down.
Public Class Form1
Dim SecondForm As Form2 = New Form2
Private Sub rbLightBlue_CheckedChanged(sender As Object, e As EventArgs) Handles rbLightBlue.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
SecondForm.BackColor = Color.LightBlue
End If
End Sub
Private Sub rbGhostWhite_CheckedChanged(sender As Object, e As EventArgs) Handles rbGhostWhite.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
SecondForm.BackColor = Color.GhostWhite
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SecondForm.Show()
End Sub
End Class