form increases size when resizing using button - vb.net

so I removed the borders of my program to have my own custom ui into it, so of course I have to make a way to resize it. i do this by using a button, but when it is on the bottom-right sides of the screen the program increases its size by a lot but it can still be resized, and when it's on the top left side it still does it but only increases the size by a little.
also I know there are other ways, but for me this one's the easiest, it's just that the button increases it's own size for some reason.
here's the code:
Private Sub tim_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tim.Tick
Me.Size = MousePosition
End Sub
Private Sub res_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles res.MouseDown
tim.Enabled = True
End Sub
Private Sub res_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles res.MouseUp
tim.Enabled = False
End Sub

If I understand your intent correctly, what you are missing is the Control.PointToClient method. It converts MousePosition from coordinates on your screen to coordinates on your form.
Here's a modified version of your code.
Private Sub tim_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tim.Tick
Dim curpos As Point = PointToClient(MousePosition)
Size = New Size(curpos.X + (Size.Width - ClientSize.Width), curpos.Y + (Size.Height - ClientSize.Height))
'if your form has no border you can skip the above and just use:
' Size = PointToClient(MousePosition)
End Sub
Private Sub res_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles res.MouseDown
Cursor.Position = PointToScreen(ClientRectangle.Size)
Cursor = Cursors.SizeNWSE
tim.Enabled = True
End Sub
Private Sub res_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles res.MouseUp
Cursor = Cursors.Default
tim.Enabled = False
End Sub

Related

VB.NET optimizing drag & drop code

I'm currently struggling with drag & drop code. I have 3 images that are at the top, and I want to add them in a random order in a flow-layout-panel.
I have this code for adding the square image into the flow-layout-panel, but I have got the feeling that this is not 100% correct. Is it possible to add these with 1 sub instead of 3?
And how do you write a sub that detects what object in being dragged? Now my sub just adds a square with each dragdrop event. But I need it to drop a Square only when a Square is being dragged and drop a Trapezium or round when it's being dragged.
Public Class Form2
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
' Initiate dragging.
PictureBox1.DoDragDrop(PictureBox1, DragDropEffects.Copy)
End Sub
Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
' Initiate dragging.
PictureBox2.DoDragDrop(PictureBox2, DragDropEffects.Copy)
End Sub
Private Sub PictureBox3_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox3.MouseMove
' Initiate dragging.
PictureBox3.DoDragDrop(PictureBox2, DragDropEffects.Copy)
End Sub
Private Sub FlowLayoutPanel1_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(GetType(PictureBox))) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop
Dim oPB As New PictureBox()
oPB.Image = Image.FromFile("C:\Users\Jef\Desktop\square.jpg")
oPB.Visible = True
oPB.Width = 100
oPB.Height = 100
oPB.SizeMode = PictureBoxSizeMode.CenterImage
FlowLayoutPanel1.Controls.Add(oPB)
End Sub
You already got the answer in your previous question:
Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop
Dim oPB As New PictureBox()
Dim pb = CType(e.Data.GetData(GetType(PictureBox)))
oPB.Image = pb.Image
pb.Image = Nothing '' Optional
'' etc...
End Sub
You do have a bug, the probable reason why you did not use the answer:
Private Sub PictureBox3_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox3.MouseMove
' Initiate dragging.
PictureBox3.DoDragDrop(PictureBox2, DragDropEffects.Copy)
End Sub
Note how it drags the wrong control, PictureBox2 instead of PictureBox3. You avoid bugs like this by writing DRY code, Do not Repeat Yourself. The sender argument of a MouseMove event already gives you a reference to the control. So you just need one event handler for all three controls:
Private Sub PictureBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove, PictureBox2.MouseMove, PictureBox3.MouseMove
' Initiate dragging.
Me.DoDragDrop(sender, DragDropEffects.Copy)
End Sub
With the detail that we now let the Form support DoDragDrop(). Which only matters if you implement the GiveFeedback or QueryContinueDrag events.

Mouse Over picture on VB and make it stay when clicked

So I make a form like this
Form1
The picture box will show neutral.png when form is loaded, so
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = Image.FromFile("Images\neutral.png")
End Sub
I make the image on picturebox1 change to x.png while mouse over and disappear when mouse leave, so
Private Sub PictureBox1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
PictureBox1.Image = Image.FromFile("Images\x.png")
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
PictureBox1.Image = Image.FromFile("Images\neutral.png")
End Sub
My question is how I make the x.png image stay on the picturebox1 when I click the picturebox1. Doing this
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
PictureBox1.Image = Image.FromFile("Images\x.png")
End Sub
Doesn't seem work since the mouseleave event still have an effect.
There is example, from my comment
Public Class Form1
Private isClicked As Boolean = False
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = Image.FromFile("images\neutral.jpg")
End Sub
Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click
If isClicked = False Then isClicked = True Else isClicked = False 'if You click again, everything back
'isClicked = True 'in this case, when You click "x.jpg" will stay always
PictureBox1.Image = Image.FromFile("images\x.jpg")
End Sub
Private Sub PictureBox1_MouseEnter(sender As Object, e As System.EventArgs) Handles PictureBox1.MouseEnter
If isClicked = False Then 'picturebox isn't clicked, so show x.jpg ... otherwise x.jpg will be showed always
PictureBox1.Image = Image.FromFile("images\x.jpg")
End If
End Sub
Private Sub PictureBox1_MouseLeave(sender As Object, e As System.EventArgs) Handles PictureBox1.MouseLeave
If isClicked = False Then 'picturebox isn't clicked, so show neutral image ... otherwise x.jpg will be showed always
PictureBox1.Image = Image.FromFile("images\neutral.jpg")
End If
End Sub
End Class
Use some variable, in this example is isClicked.
Always make checking under MouseEnter and MouseLeave is picturebox clicked or not.
By this example, You can click again on picturebox to, let's say, reset that isClicked, so You can have again neutral and x images.

visual basic game player wont move

For a school project I am making a simple game were zombie's walk straight down and try to attack the players base (it is kinda like space invaders and planets versus zombies). however my player has stopped moving when I press D or A, when I try to play the game. I will post my code. can somebody help make my player move side to side?.
Public Class Form2
Dim Player_Health As Integer = 10
Dim Player_Max_Health As Integer = 100
Dim Player_X_Speed_Moving As Integer = 0
Dim Player_Potential_X_Speed As Integer = 10
Private Sub Form2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.A Then
Player_X_Speed_Moving = -Player_Potential_X_Speed
End If
If e.KeyCode = Keys.D Then
Player_X_Speed_Moving = Player_Potential_X_Speed
End If
End Sub
Private Sub Form2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.A Then
Player_X_Speed_Moving = 0
End If
If e.KeyCode = Keys.D Then
Player_X_Speed_Moving = 0
End If
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bmp As Bitmap = Bitmap.FromFile("C:\Users\Kevin Welch\Documents\Visual Studio 2010\Projects\Zom-b\Zom-b\My Project\Res\Spr\Player1.bmp")
bmp.MakeTransparent(Color.White) ' magenta in bitmap will be transparent
PictureBox1.Image = bmp
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Call MovePlayer()
End Sub
Private Sub MovePlayer()
PictureBox1.Left += Player_X_Speed_Moving
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form1.Show()
Me.Hide()
End Sub
End Class
My guess would be that the form's message queue is getting backed up with key down events and your Tick event is having trouble keeping up. Most window UI systems use a single thread that responds to messages that are pumped to them such as key down/up, mouse movements, and clicks. If the UI thread is busy, your Tick event will have to wait since it runs on the same thread. Many games have a game loop that processes window events along with processing game logic and graphics rendering. I would recommend look into using the XNA Framework since it handles most of that stuff for you.

How to make a picturebox hide when dragged over another picturebox?

So I nailed the ability to drag a picturebox around the Windows form. I need it to hide itself when it's dragged and dropped over another picture. I've tried a few methods but none seem to work and I am now back at square one, the only code I have is so that I can move the picturebox around the form.
I Solved Your Problem
Here Is How The Form Should Look Like
And Here Is The Code I Made:
Private Sub CheckTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckTimer.Tick
CheckTimer.Stop()
CheckTimer.Interval = 1
If PictureBox2.Location = New System.Drawing.Point(TextBox1.Text, TextBox2.Text) Then
PictureBox2.Visible = False
End If
CheckTimer.Start()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = PictureBox1.Location.X
TextBox2.Text = PictureBox1.Location.Y
CheckTimer.Start()
End Sub
Hope This Code Was Helpful To You.
I made an better code than the old one, try it.
Here Is How The Form Should Look Like:
and here is the code :
Public Class Form1
Dim Point As New Point
Dim X, Y As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
Timer1.Interval = 1
Point = Cursor.Position
PictureBox2.Location = New System.Drawing.Point(Point.X - X, Point.Y - Y)
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown
X = Cursor.Position.X - PictureBox2.Location.X
Y = Cursor.Position.Y - PictureBox2.Location.Y
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp
Timer1.Stop()
If PictureBox2.Location.X < PictureBox1.Size.Width Then
PictureBox2.Visible = False
End If
End Sub
End Class
I hope this code was useful to you.

Fade in picturebox in VB

How do I make a picturebox fade in after a timer
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
picturebox1.opacity = false
End Sub
Opacity is not a member of System.Windows.Forms.PictureBox. You can't have the same effect like forms. But you can do this:
PictureBox1.Visible = False