Mouse hovering event - vb.net

I want to do an mouse hovering event, when the mouse is over an button I want to change button text color and font size, I have try this code but doesn't work:
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.ForeColor.MediumBlue()
Command1.FontSize = 10
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.ForeColor.White()
Command1.FontSize = 8
End Sub
Can anyone give me a suggestion i have search on Google and try different ways with mouse event handler but didn't work.

First, instead of tracking every mouse move, you can rely on MouseEnter and MouseLeave events of the button.
Second, do not forget to add Handles <Control>.<Event> clause at the declaration of your event-handling procedures.
Result:
Private Sub Command1_MouseEnter(sender As Object, e As EventArgs) _
Handles Command1.MouseEnter
Command1.FontSize = 10
End Sub
Private Sub Command1_MouseLeave(sender As Object, e As EventArgs) _
Handles Command1.MouseLeave
Command1.FontSize = 8
End Sub
Also please do not forget that some users are preferring keyboard control.
This means that
You might want to equip the button with an accelerator.
Command1.Text = "&Launch" (now Alt+L activates the button)
Note: accelerator character for winforms is &, for wpf is _.
You might want to make your entry/leave effect also when the button receives/looses keyboard focus (focus is moved using Tab and Shift+Tab key).

You can try making your changes into MouseEnter and MouseLeave
Private Sub RightButton_MouseEnter(sender As System.Object, e As System.EventArgs) Handles RightButton.MouseEnter
RightButton.ForeColor = Color.AliceBlue
RightButton.Font = New Font(RightButton.Font, 12)
End Sub
Private Sub RightButton_MouseLeave(sender As System.Object, e As System.EventArgs) Handles RightButton.MouseLeave
RightButton.ForeColor = Color.White
RightButton.Font = New Font(RightButton.Font, 10)
End Sub

Related

Button collision event vb.net

I'm trying to make some buttons move around in a rapid manner. Each click of a specific button should "reward" the player with a certain quantity of points (both positive or negative). This is a similar idea to some of the aspects in some of the "idiot test games" you see online.
How can I perform collision checks with the buttons?
I know it's possible with picture boxes to perform event collisions, with the following code picObject1.bounds.intersectsWith(picObject2.bounds).
However, when I tried using that function for buttons, they didn't register as a collision. I do not know if that is because buttons don't have bounds (though that doesn't sound right) or due to some other hidden detail that I have missed.
Any pointers in the right direction would be extremely useful!
Handle the Click event for the buttons. The code might look something like this:
Public Class Form1
Private PlayerScore As Integer = 0
Private Buttons() As Button
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Buttons = Enumerable.Range(0,10).
Select(Function(i)
i = New Button()
AddHandler i.Click, AddressOf ScoreClick
Me.Controls.Add(i)
Return i
End Function)
End Sub
Dim rnd As New Random()
Private Sub ScoreClick(ByVal sender As Object, ByVal e As EventArgs)
Dim button As Button = DirectCast(sender, Button)
PlayerScore += rnd.Next(-10, 10)
End Sub
End Class
Now just add a timer to to the form to move the buttons around.

How to clear a textbox if i drag the text out

I have code to drag files into a textbox, but I would also like to have code to drag the file out of the textbox, thus clearing it.
Anybody have any ideas?
You can use the DoDragDrop method to do a drag & drop operation. It returns a DragDropEffects value that specifies if the data actually has been dropped somewhere in which case you can clear the text box.
Since a drag & drop operation shouldn't start before the mouse has been moved a bit while pressing the mouse button you need to check for that in the MouseDown and MouseMove events. SystemInformation.DragSize tells you how far the mouse should be moved before a drag & drop operation starts.
In the MouseDown event check if you actually want to start a drag (i.e left button is pressed and text box actually contains text). Then create a rectangle using the mouse location and the size given by SystemInformation.DragSize. In the MouseMove event check if the mouse is dragged outside the rectangle and call DoDragDrop:
Private _dragStart As Boolean
Private _dragBox As Rectangle
Private Sub srcTextBox_MouseDown(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseDown
' a drag starts if the left mouse button is pressed and the text box actually contains any text
_dragStart = e.Button = MouseButtons.Left And Not String.IsNullOrEmpty(srcTextBox.Text)
If _dragStart Then
Dim dragSize As Size = SystemInformation.DragSize
_dragBox = New Rectangle(New Point(e.X - (dragSize.Width \ 2),
e.Y - (dragSize.Height \ 2)), dragSize)
End If
End Sub
Private Sub srcTextBox_MouseUp(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseUp
_dragStart = False
End Sub
Private Sub srcTextBox_MouseMove(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseMove
If Not _dragStart Or
(e.Button And MouseButtons.Left) <> MouseButtons.Left Or
_dragBox.Contains(e.X, e.Y) Then Return
Dim data As New DataObject()
data.SetData(srcTextBox.Text)
' you can optionally add more formats required by valid drag destinations:
' data.SetData(DataFormats.UnicodeText, Encoding.Unicode.GetBytes(srcTextBox.Text))
' data.SetData("UTF-8", Encoding.UTF8.GetBytes(srcTextBox.Text))
' data.SetData("UTF-32", Encoding.UTF32.GetBytes(srcTextBox.Text))
Dim dropEffect As DragDropEffects = srcTextBox.DoDragDrop(data, DragDropEffects.Move)
If (dropEffect = DragDropEffects.Move) Then
srcTextBox.Text = ""
End If
_dragStart = False
_dragBox = Rectangle.Empty
End Sub
Private Sub destTextBox_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles destTextBox.DragOver
If e.Data.GetDataPresent(GetType(String)) Then
e.Effect = e.AllowedEffect And DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub destTextBox_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles destTextBox.DragDrop
If e.Data.GetDataPresent(GetType(String)) Then
destTextBox.Text = e.Data.GetData(GetType(String)).ToString()
End If
End Sub
Dragging the mouse in a TextBox usually starts text selection. The above code changes this behavior. Users can't use the mouse any more to select text. This is obviously not a good idea since users wouldn't expect that. To allow both text selection with the mouse and dragging you need to control the selection mechanism. This means you need to create your own text box class.
I would suggest a different approach: Use a label that shows the value as the dragging source and/or destination. To allow editing you can create a hidden text box. If the user double clicks on the label you hide the label and show the text box. After the user finishes editing (by hitting enter or cancel) or if the text box looses focus you hide the text box and show the label again.
Check out the DragLeave Event on the TextBox
Easiest way will probably be to store it as a variable or in clip board if leaving app.
Private Sub TheTextBox_DragLeave(sender As System.Object, e As System.EventArgs) Handles TheTextBox.DragLeave
Clipboard.SetText(TheTextBox.Text)
TheTextBox.Clear() 'Optional
End Sub
Then you'll need to code what happens when you mouse up of course, or drag into or whatever. You could clear the textbox in one of these steps. Depends on your implementation.

.NET TrackBar MouseDown follow cursor?

I currently use this code to fix the bug where if you click somewhere on the Horizontal TrackBar it jumps to the middle then to the end of the TrackBar. So this code fixes that bug, which now jumps to the location you click.
But still a problem remains when I keep my mouse down and move it around the TrackBar the slider should follow but it just resets to beginning position, how do I make it follow right on top of cursor? would I need a timer control for that?
Private Sub tbTest_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbTest.MouseDown
Dim dblValue As Double
'Jump to the clicked location, bug FIX.
dblValue = (Convert.ToDouble(e.X) / Convert.ToDouble(tbTest.Width)) * (tbTest.Maximum - tbTest.Minimum)
tbTest.Value = Convert.ToInt32(dblValue)
End Sub
Make the method handle both the MouseDown() and MouseMove() events like this:
Private Sub tbTest_MovePointer(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbTest.MouseDown, tbTest.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim dblValue As Double
'Jump to the clicked location, bug FIX.
dblValue = (Convert.ToDouble(e.X) / Convert.ToDouble(tbTest.Width)) * (tbTest.Maximum - tbTest.Minimum)
tbTest.Value = Convert.ToInt32(dblValue)
End If
End Sub
*Note the multiple events listed after the Handles keyword at the end of the first line. I also added a check to ensure the left mouse button is down.

Change Cursor as Feedback in DragDrop VB.NET 2010

Working on a DragDrop application. I have a class object (built as an object) which has a GroupBox that is enabled for DragDrop.
In the class I have set it up so that the cursor will change when it enters the groupbox, leaves the groupbox, mouse is down within the groupbox and drag/drop is working. I also set up a label in the class object and changed the text in that label as well. Basically, the text changes in the label as expected including 'FEEDBACK' appearing when I am dragging the object BUT the cursor stays stubbornly as a pointer. This is tghe state of affairs either when I debug the class or when, after I build the class, it is run as part of the main program.
Here are the subs set up in the class.
Private Sub GroupBoxSourceMouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBoxSource.MouseDown
GroupBoxSource.DoDragDrop(GroupBoxSource.Text, DragDropEffects.Copy)
lbl1.Text = "DOWN"
End Sub
Private Sub GroupBoxSourceMouseMove(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles GroupBoxSource.MouseMove
If bolDragDropMouseDown Then
' Initiate dragging.
'GroupBoxSource.DoDragDrop(GroupBoxSource.Text, DragDropEffects.Copy)
End If
bolDragDropMouseDown = False
lbl1.Text = "MOVE"
End Sub
Private Sub GroupBoxSourceMouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBoxSource.MouseEnter
Windows.Forms.Cursor.Current = Cursors.Hand
lbl1.Text = "ENTER"
End Sub
Private Sub GroupBoxSourceMouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBoxSource.MouseLeave
Windows.Forms.Cursor.Current = Cursors.Arrow
lbl1.Text = "LEAVE"
End Sub
Private Sub GroupBoxSourceDragDropFeedback(ByVal sender As Object, ByVal e As GiveFeedbackEventArgs) Handles GroupBoxSource.GiveFeedback
Windows.Forms.Cursor.Current = Cursors.SizeAll
lbl1.Text = "FEEDBACK"
End Sub
Now for the unusual stage. I decided to change the cursor during FormLoad of the main program (which has the class object defined on the form) to a hand and nothing changed. In desperation I set up a 1mS timer and coded that to change the cursor to a hand and sure enough the cursor is now a hand but momentarity flicks back to a pointer when I move the cursor so it looks as if something else is causing the cursor to revert to its default value.
You have to set e.UseDefaultCursor = False in your GiveFeedback event handler to make your cursor change visible. Without it, drag and drop always uses the default cursors.
The cursor you get when the mouse hovers a control is set by the Control.Cursor property. Changing the Cursor.Current property has no effect, the property causes the cursor to change back instantly when you move the mouse.
To easily change the cursor, click on the form and bring up the properties. Then go to Cursor and change it to whatever you want.

Text is selected in the text box

When I load the form where some text has been given to text box. All the text in that textbox is highlighted. I want vb not to load it this way.
How to fix it.
Thanks
Furqna
You could set the tab index on your textbox to something else so that it's not the lowest index.
You could set the TextBox1.SelectionLength = 0 in the form.activated event.
I don't like this as much because if the user had the text hilited and minized the application then they will lose the hilite, but is fairly easy to do. I guess you could use a flag to make sure it only did it on the first activate.
You could set a timer event in the load to clear it immediately after the load event, but that seems like overkill. I have worked at places where they had a standard function that happened on every form 100 ms after load because of problems such as this.
You could try this(it looks like a workaround):
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.SelectionStart = TextBox1.Text.Length
End Sub
It depends on the TabIndex of your TextBox, if it has the lowest TabIndex it gets focus and therefore it's Text is selected.
' VS.net 2013. Use the "Shown" event.
' GotFocus isn't soon enough.
Private Sub Form_Shown(sender As Object, e As EventArgs) Handles Me.Shown
TB.SelectionLength = 0
End Sub
Type 1 Method
Dim speech = CreateObject("sapi.spvoice")
speech.speak(TextBox1.Text)
Type 2 Method
Dim oVoice As New SpeechLib.SpVoice
Dim cpFileStream As New SpeechLib.SpFileStream
'Set the voice type male or female and etc
oVoice.Voice = oVoice.GetVoices.Item(0)
'Set the voice volume
oVoice.Volume = 100
'Set the text that will be read by computer
oVoice.Speak(TextBox1.Text, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault)
oVoice = Nothing
Type 3 Method
Imports System.Speech.Synthesis
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim spk As New SpeechSynthesizer
For Each voice As InstalledVoice In spk.GetInstalledVoices
ListBox1.Items.Add(voice.VoiceInfo.Name)
Next
ListBox1.SelectedIndex = 0
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim spk As New SpeechSynthesizer
spk.SelectVoice(ListBox1.SelectedItem.ToString)
spk.Speak(TextBox1.Text)
End Sub
End Class
This will also happen sometimes if The TextChanged or other similar Event is fired twice for the control.
When creating each form. Each object is indexed you can set the tab Index higher then the indexed object. Example: On the third form you put a text box in.
private void textBox1_TextChanged(object sender, EventArgs e)
This was the 12th object in the project, it would be indexed at 12. if you put the tab index higher then the indexed objects throughout the project. Tab index 1000 (problem solved.)
Have a great day.
Scooter