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
Related
I am trying to create a simple winform application in VB. I have a form, a picturebox with a background image and a button. When I press the button, I want the picturebox to move left across the form until it disappears off the left side. Then I want it to reappear on the right side and move to the left again.
Initial condition:
The problem seems to be that the areas of the picturebox that exceed the boundaries of the form get erased. So the first time around the images slides nicely off the left side of the form and never comes back to the right side. Or rather, it does come back, I just can see it.
Second scroll around, parts of image in picturebox that exceeded the boundaries of the form have been erased (left) or distorted (right):
In the code below I have deliberately changed the boundaries for the picturebox so that I can see that it is actually looping back around. But as you can see in this image, the parts of the picture box that exceeded the boundaries of the form are either erased or distorted.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Do
PictureBox1.Left -= 1
Threading.Thread.Sleep(2)
If PictureBox1.Left <= -20 Then 'If PictureBox1.Left <= PictureBox1.Width Then
PictureBox1.Left = Me.Width - 40 'PictureBox1.Left = Me.Width
End If
Loop
End Sub
End Class
I have tried this with both VS2019 and VS2017 on two different computers and the same thing happens. I have also tried replacing the picturebox with a textbox and much the same thing happens.
I changed my code to this, and now it works perfectly.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 25
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Left -= 1
If PictureBox1.Left <= -PictureBox1.Width Then
PictureBox1.Left = Me.Width
End If
End Sub
End Class
I have a form with a picturebox on it, and another form with a button on it. How would I picturebox1_paint on the first form once I press the button on the other form?
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawRectangle(Pens.Blue, x.text, y.text, width.text, height.text)
End Sub
this is what I have, what would I need to alter to get it to work?
At Form1, add
Sub PaintImage(rectangle As Rectangle)
' create image in size of PictureBox1
Dim image As New Bitmap(PictureBox1.Width, PictureBox1.Height)
' paint on created image
Using gr As Graphics = Graphics.FromImage(image)
gr.DrawRectangle(New Pen(Color.Red), rectangle)
End Using
' display finished image in PictureBox1
PictureBox1.Image = image
End Sub
and call it from the form with button:
Sub Button1_Click(sender as Object, e as EventArgs) Handles Button1.Click
Form1.PaintImage(New Rectangle(x, y, width, height)) ' assume we already have x, y, w, h
End Sub
You can delete your Paint() event handler shown in the question. Its purpose is to repaint PictureBox1.Image if its size or visibility is changed and in most cases you can leave it as it is.
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
I'm trying to make VB.net program and UI is like normal desktop. I have images in picture
boxes as desktop shortcuts. So user can drag them around in form. Btw im not skilled just started
to do things like this.
I can move picture boxes with mouse events its fine But pictures in pictureboxes are tearing when dragging. i have a big background picture, i guess this is the problem but i need help to get rid of tearing. heres code thx.
i also tryed as a panel and get same result.
Edit : pictures about dragging is png , 50x50 , transparent BG.
Public Class testform
Dim drag As Boolean
Dim mousex, mousey As Integer
Private Sub testform_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackgroundImage = My.Resources.worldmap 'approx 1.03mb Picture size
End Sub
Private Sub dragbox_MouseDown(sender As Object, e As MouseEventArgs) Handles dragbox.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
drag = True
mousex = Windows.Forms.Cursor.Position.X - dragbox.Left
mousey = Windows.Forms.Cursor.Position.Y - dragbox.Top
Else
End If
End Sub
Private Sub dragbox_MouseMove(sender As Object, e As MouseEventArgs) Handles dragbox.MouseMove
If drag = True Then
dragbox.Left = Windows.Forms.Cursor.Position.X - mousex
dragbox.Top = Windows.Forms.Cursor.Position.Y - mousey
End If
End Sub
Private Sub dragbox_MouseLeave(sender As Object, e As EventArgs) Handles dragbox.MouseLeave
drag = False
End Sub
End Class
You need to make the picture form double buffered. That means that it will no longer tear as it will first completely make sure it's all in place before showing the next frame.
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.