VB.Net - Toolstrip Button Mousehover Event - vb.net

Is there a way to change the size of the toolstrip button on mousehover event?
I tried this but didn't work.
Private Sub tsDriver_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsDriver.MouseHover
Dim pt As Point
pt.X = 60
pt.Y = 70
tsDriver.Size = pt
End Sub
I'd like to have the effect like, when mouse is hovered on the button, it will grow big and when the mouse leaves it will go back to its original size.

You should instantiate a the size, which is a separate object.Try this, it should work;
Private Sub tsDriver_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsDriver.MouseHover
Dim pt As New System.Drawing.Point
pt.X = 60
pt.Y = 70
tsDriver.Size = New System.Drawing.Size(pt)
End Sub
Note that the MouseHover event only triggers when the mouse cursor enters the control location.
So, for the button to shrink to original size, the MouseLeave event should be coded;
Private Sub tsDriver_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsDriver.MouseLeave
Dim pt As New System.Drawing.Point
pt.X = 40 ' Original size
pt.Y = 50
tsDriver.Size = New System.Drawing.Size(pt)
End Sub

Should only the button resize? or would it be okay for the rest of the buttons resize as well??
If so, you can Manipulate the ImageScalingSize property of the tool tip window
Dim pt2 As Point
pt2.X = 100
pt2.Y = 100
ToolStrip1.ImageScalingSize = pt2
This assumes it's okay for the rest of the buttons to grow as well.

Related

How to autoscroll panel or picturebox in MouseMove event in VB:NET

I'm trying to autoscroll panel with an image using mousemove event simulating a dynamic zoom.
I found this example Pan/scroll an image in VB.NET and this Scroll panel based on mouse position in VB.NET but I realized that the user have to click on the image to drag it, so I tried to modify the code but doesn't work
This is what I tried:
Private m_PanStartPoint As New Point
Private Sub PictureBox2_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseMove
Dim DeltaX As Integer = (m_PanStartPoint.X - e.X)
Dim DeltaY As Integer = (m_PanStartPoint.Y - e.Y)
Panel1.AutoScrollPosition = New Point((DeltaX - Panel1.AutoScrollPosition.X), (DeltaY - Panel1.AutoScrollPosition.Y))
End Sub
Private Sub PictureBox2_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox2.MouseEnter
PictureBox2.SizeMode = PictureBoxSizeMode.AutoSize
m_PanStartPoint = New Point(MousePosition)
End Sub
Private Sub PictureBox2_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox2.MouseLeave
PictureBox2.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
I also tried adding the event MouseHover:
Private Sub PictureBox2_MouseHover(sender As Object, e As EventArgs) Handles PictureBox2.MouseHover
m_PanStartPoint = New Point(MousePosition)
End Sub
if there is a way to do it without a panel, it would be better.
'The VB/XAML code below implements left mouse button click+hold scrolling and also works with touch screens. When using this code, put your frame/grid/image within a scrollviewer as shown below:
'XAML
<ScrollViewer x:Name="ScrollViewerObject" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Hidden" ScrollViewer.CanContentScroll="False" PreviewMouseDown="Part_ScrollViewer_PreviewMouseDown" PreviewMouseMove="Part_ScrollViewer_PreviewMouseMove">
<Frame x:Name="PartViewer" NavigationUIVisibility="Hidden"/>
</ScrollViewer>
'VB
Dim LastScrollPos As Double = 0
Dim MouseStartPos AS Double = 0
Private Sub ScrollViewerObject_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs)
Dim position As Point = Mouse.GetPosition(ScrollViewerObject)
ScrollViewerObject.UpdateLayout()
LastScrollPos = ScrollViewerObject.ContentVerticalOffset
MouseStartPos = position.Y
End Sub
Private Sub ScrollViewerObject_PreviewMouseMove(sender As Object, e As MouseEventArgs)
If Mouse.LeftButton = MouseButtonState.Pressed Then
Dim position As Point = Mouse.GetPosition(ScrollViewerObject)
ScrollViewerObject.ScrollToVerticalOffset(LastScrollPos + (position.Y - MouseStartPos))
End If
End Sub

I want the picture box to move every second to the right by one place

Here is where I have tried to implement the code to change the location of the picture box but it doesn't seem to be working, I want the picture to move to the right:
Public Class Form1
Dim mypicturebox As New PictureBox
Dim randval As Integer
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Label1.Text = "hello " & TextBox1.Text
Timer1.Enabled = True
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Timer1.Interval = 2000
Timer1.Enabled = True
mypicturebox.Location = New Point(mypicturebox.Location.X + 5, mypicturebox.Location.Y + 5)
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Label1.Font = New Font(Label1.Font.FontFamily, Label1.Font.Size +
mypicturebox.Location = New Point(mypicturebox.Location.X + 5, mypicturebox.Location.Y)
End Sub
End Class
Your current Timer handler - if the Timer is actually firing - is just going to drift your PictureBox off to the right by 5px each Timer period.
If you want a random position you need a way to get a random number. Random is your best bet.
You also need to ensure that the generated random position is "valid" for both the size of your screen and the size of the PictureBox otherwise the PictureBox might be either entirely invisible (off screen) or partially visible.
Here's some basic code to get two random numbers between -100 and 100 to get you started:
Dim myRand As Random = New Random(Now.Second)
Dim newX As Integer = myRand.Next(-100, 100)
Dim newY As Integer = myRand.Next(-100, 100)
(Now.Second seeds the generation of myRandom so that it should generate different starting numbers each time.)
Then you can use these as your PictureBox location points:
mypicturebox.Location = New Point(newX, newY)
Play around with limiting your locations and you should get what you want.
UPDATE
Ok, scrap all that as you've changed your requirement description ...
Just add whatever offset you need to your X position and you're done, provided your Timer is firing. You still need to watch your screen limits.

Keep Mouse Inside a WinForm Bounds

I have a Mouse Move method that moves a Button up and down along the Y axis on a WinForms project
When the Cursor is outside the bounds of the form the Button will not move
I would like to limit the Cursor from exiting or losing focus of the form
I would also like to set a limit on the top and bottom Y positions of the Button btnPad
Other Controls on the form are three buttons that form a TOP, BOTTOM and RIGHT WALL
The Y INSIDE dimensions of the TOP and BOTTOM walls are 50 and 690
FWIW the Form dimensions are Width 1000 and Height 890
I have some code that prevents the cursor from entering the form called MoveCursor
I will post some code I have tried like frmStart MouseLeave and MouseEnter as well as the actual MouseMove code
The Question is how to prevent the Cursor from exiting the Form?
Private Sub MoveCursor()
' Set the Current cursor, move the cursor's Position,
' and set its clipping rectangle to the form.
Me.Cursor = New Cursor(Cursor.Current.Handle)
Cursor.Position = New Point((Cursor.Position.X + 1000), (Cursor.Position.Y + 890))
Cursor.Clip = Me.Bounds
Cursor.Clip = New Rectangle(Me.Location, Me.Size)
End Sub
Private Sub frmStart_MouseLeave(ByVal sender As Object, ByVal e As EventArgs)
Cursor.Position = New Point(X, Y)
Cursor.Clip = Me.Bounds
End Sub
Private Sub frmStart_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
MoveCursor()
btnPad.Top = e.Y
End Sub
The code below was as close as I could come to keeping the Cursor inside the play area which is the form the mouse still wanders out on the right side
Private Sub frmStart_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Cursor.Clip = New Rectangle(Me.Location, Me.Size)
btnPad.Top = e.Y
End Sub

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 AddHandler MouseHover

My Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim top As Integer = 0
For i = 0 To 10
Dim inLine As Integer = 8
Dim left As Integer = 0
For x = 0 To inLine
Dim s As New Panel
s.BackColor = Color.Black
s.Width = 10
s.Height = 10
s.Left = left
s.Top = top
left = left + 20
AddHandler s.MouseHover, AddressOf Panel1_MouseHover
Me.Controls.Add(s)
Next
top = top + 20
Next
End Sub
Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
/////---- Some code!! :/
End Sub
My code adding panels into my from , i want that when Mouse Hover on panel
the panel change the background color.
if someone not understand:
when my mouse hover my panel that i add to my form, how i change the panel back color?
Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
/////---- Some code to change the panel color !! :/
End Sub
I think the part you're missing is the fact that the sender parameter to the event handler method will always be whichever control it is that is raising the event. Before using it though, I would cast it to the correct type so you get the full advantages of intellisense and compiler type checking.
Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
Dim panel As Panel = CType(sender, Panel)
panel.BackColor = Color.White
End Sub