How to change the button`s name on Mouse Hover - vb.net

Q- When a 'Mouse Hover action' is used for a button (N.B: the button has a name-Text it shows under properties- we wish to change that name when the mouse hovers over it as well as change the font color of the textfield (textbox) from blue to red). Please can you`ll help me to change the name of the button component and color issue.
Code Attempt: (Visual Basic 10 Express)
Public Class Form1
Private Sub Button1_MouseHover(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.MouseHover
Button1.Name("About to go Red")
End Sub
End Class

When you click buttton in Properties, there is an icon looking like a lightning bolt which is for events.
Click that icon, and search for the event MouseHover
As I don't know if you actually mean the design name, or the text it shows.
For the text:
Private Sub Button1_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover
Button1.text = R.Next(0, 10)
End Sub
For the name:
Private Sub Button1_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover
Button1.Name = R.Next(0, 10)
End Sub
Ofcourse you can edit R.Next to any thing you want.

Related

viewing image in picturebox using listview in vb.net 2008

I am working in vb.net 2008 with listview control. I added some images in listview.items using imagelist. now I want to show that image in picture box when I clicked on button. I had tried some time for this but getting error everytime.
Here is the code :
Public Class Form1
Private Sub ListView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
PictureBox1.Image = Bitmap.FromFile(ListView1.Items.Item(1).ToString)
End Sub
End Class
If you want the picture to show when you click a button then I think you want to use Button_Click instead of ListView_Click
Private Sub MyButton_Click(sender As Object, e As EventArgs) Handles MyButton.Click
'Also, might be easier just to do this:
PictureBox1.Image = ImageList1.Images(0)
End Sub
Then you can set different buttons for your different pictures

Get text of self object in vb.net

Let's say I have a button with the text 'A'.
When I click this button, I want a messagebox to appear with the text of what name of the button that was clicked.
I tried setting it to MessageBox.Show(Me.Text) but that just gives me the form name.
How can I refer to the text of button I just clicked?
If you have to handle multiple button click try following method
Private Sub btn_a_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btn_a.Click _
,btn_b.Click 'you can add other buttons click event here(ex. btn_c,btn_d etc)
Dim objButton As Button = DirectCast(sender, Button)
MessageBox.Show(objButton.Text)
End Sub
DirectCast() vs. CType()
The reason why MessageBox.Show(Me.Text) didn't work is that Me refers the class containing the code, in this case, the Form).
If your button's event handler only handles one button, you can just hard code the buttons name like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(Button1.Text)
End Sub
If the event handler can handle more than one button, you can use the sender argument to reference the button being clicked:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Dim but as Button = CType(sender, Button)
MessageBox.Show(but.Text)
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

button release after mouse hovering out

I have this six button
After i press one button, for example i press settings it will open an new windows after i close that windows the button remain pressed.
Here are the settings button code:
Private Sub btn_SETTINGS_MouseEnter(sender As System.Object, e As System.EventArgs) Handles btn_SETTINGS.MouseEnter
btn_SETTINGS.ForeColor = Color.White
End Sub
Private Sub btn_SETTINGS_MouseLeave(sender As System.Object, e As System.EventArgs) Handles btn_SETTINGS.MouseLeave
btn_SETTINGS.ForeColor = SystemColors.HotTrack
End Sub
Private Sub btn_SETTINGS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_SETTINGS.Click
OpenSettings()
End Sub
Any suggestion what can i do to fix this problem.
Okay, this problem seems related to Focus that got set to the button when you pressed it. Very easy and quick fix of this problem is, change the focus of control to some other control. On the same side, add one label control which is of Hidden type say lblHidden. So when you're doing following code then change focus.
Private Sub btn_SETTINGS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_SETTINGS.Click
OpenSettings()
Me.ActiveControl = lblHidden
End Sub
This will change the focus to hidden control. However if you are doing any formatting change of button on click event then revert it back to original in above even.t

Mimic MouseIn/Out

I am trying to hide my cursor when a user enters a button, and show it when they leave the button.
So far I am using the MouseEnter and MouseLeave Events
Private Sub btnbeis_MouseEnter(sender As System.Object, e As System.EventArgs) Handles btnBeis.MouseEnter
Dim btn As Button = DirectCast(sender, Button)
btn.FlatStyle = FlatStyle.Flat
Cursor.Hide()
End Sub
Private Sub btnbeis_mouseLeave(sender As System.Object, e as system.EventArgs) Handles btnbeis.MouseLeave
Dim btn As Button = DirectCast(sender, Button)
btn.FlatStyle = FlatStyle.Standard
cursor.Show()
End Sub
However because MouseEnter is hit every time the user moves the mouse, it keeps on hiding the mouse
I found the MouseOut event but it does not work for vb, is there anything that mimics mouseout?
Meaning I only want it to hit when the mouse leave the button, and enters for the first time
EDIT
I tried this on a new form and it works perfectly
But on this form it keeps on hitting the mouseEnter and MouseLeave, when I debug it keeps on going from mouseEnter to MouseLeave and back to MouseEnter
I Do Not have any code under mouseMove, so that is not the problem
Thank You!
Sub Button1MouseLeave(sender As Object, e As EventArgs)
Cursor.Show()
End Sub
Sub Button1MouseEnter(sender As Object, e As EventArgs)
Cursor.Hide()
End Sub