Resize Bordless Form is Glitchy - vb.net

My code to resize my form from the right hand side works, but is really glitchy or has a lot of lag. All my images blur and my drawn rectangles flicker while I'm resizing. Everything is normal however once mouseup.
I put the formborderstyle from borderless to sizable and the form resizes normally.
Hoping someone might be able to point out what is wrong with my code.
Dim myresize As Boolean = False
Dim cursorx As Integer
Dim cursory As Integer
Private Sub TableLayoutPanel1_MouseDown(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.MouseDown
If e.Location.X > Me.Width - 7 And e.Location.Y > 11 And e.Location.Y < Me.Height - 10 Then
myresize = True
cursorx = Windows.Forms.Cursor.Position.X - Me.Width
cursory = Windows.Forms.Cursor.Position.Y - Me.Height
End If
End Sub
Private Sub TableLayoutPanel1_MouseMove(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.MouseMove
If myresize = True Then
Me.Width = Windows.Forms.Cursor.Position.X - cursorx
End If
End Sub
Private Sub TableLayoutPanel1_MouseUp(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.MouseUp
myresize = False
End Sub

For the flickering, the form does not pass the DoubleBuffered property to its child controls, such as your TableLayoutPanel. Instead try adding a new class to your project that will doubleBuffer the tableLayoutPanel.
Public Class DoubleBufferedTableLayoutPanel
Inherits TableLayoutPanel
Public Sub New()
Me.DoubleBuffered = True
End Sub
End Class
Build your project and the new version of the TableLayoutPanel will be in your toolbox as DoubleBufferedTableLayoutPanel. From there just use it as you would the TableLayoutPanel in your code.

Related

How to make a button click quickly switch between two images in a picturebox vb

Making a joke VB program that requires a button click to make a PictureBox very quickly switch between two pictures. I tried using the sleep command but nothing changes on screen. Here's what I've tried so far.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TransClass2.Image = My.Resources._21
System.Threading.Thread.Sleep(100)
TransClass2.Image = My.Resources._11
System.Threading.Thread.Sleep(100)
End Sub
TransClass2 is a class that inherits PictureBox. It's used to add transparent functionalities to PictureBoxes.
Public Class TransClass
Inherits PictureBox
Protected Overrides Sub OnPaintBackground(e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaintBackground(e)
If Parent IsNot Nothing Then
Dim index As Integer = Parent.Controls.GetChildIndex(Me)
For i As Integer = Parent.Controls.Count - 1 To index + 1 Step -1
Dim c As Control = Parent.Controls(i)
If c.Bounds.IntersectsWith(Bounds) AndAlso c.Visible = True Then
Dim bmp As New Bitmap(c.Width, c.Height, e.Graphics)
c.DrawToBitmap(bmp, c.ClientRectangle)
e.Graphics.TranslateTransform(c.Left - Left, c.Top - Top)
e.Graphics.DrawImageUnscaled(bmp, Point.Empty)
e.Graphics.TranslateTransform(Left - c.Left, Top - c.Top)
bmp.Dispose()
End If
Next
End If
End Sub
End Class
Mark the click handler as Async, then use Await Task.Delay():
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TransClass2.Image = My.Resources._21
Await Task.Delay(100)
TransClass2.Image = My.Resources._11
Await Task.Delay(100)
End Sub
I think 100 might be too fast!

Maximize form but do not cover the taskbar

When I click the button to maximize my form, it covers the entire screen including the taskbar. I managed to find a solution and it works, I used my code in form load event but I cannot return the form into normal state.
Private Sub frmDashboard_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Top = Screen.PrimaryScreen.WorkingArea.Top
Me.Left = Screen.PrimaryScreen.WorkingArea.Left
Me.Height = Screen.PrimaryScreen.WorkingArea.Height
Me.Width = Screen.PrimaryScreen.WorkingArea.Width
End Sub
Private Sub btnMaximizeMin_Click(sender As Object, e As EventArgs) Handles btnMaxMin.Click
If Me.WindowState = FormWindowState.Normal Then
'maximize but dont cover taskbar
Me.Top = Screen.PrimaryScreen.WorkingArea.Top
Me.Left = Screen.PrimaryScreen.WorkingArea.Left
Me.Height = Screen.PrimaryScreen.WorkingArea.Height
Me.Width = Screen.PrimaryScreen.WorkingArea.Width
Else
Me.WindowState = FormWindowState.Normal
End If
End Sub
The problem is that you aren't maximising the form. You specifically DON'T want to maximise the form because that covers the Windows Task Bar. You can't set the WindowState "back" to Normal because it's already in that state, because it never leaves that state. It's up to you to remember the state for yourself and also the previous bounds, e.g.
Private isMaximised As Boolean = False
Private normalBounds As Rectangle
Private Sub MaximiseOrRestore()
isMaximised = Not isMaximised
If isMaximised Then
normalBounds = Bounds
Bounds = Screen.PrimaryScreen.WorkingArea
Else
Bounds = normalBounds
End If
End Sub

If something gets out of the panel in the top side then make it go to (0, 0) position, if in the botton side then (0, Panel Height Size)

So basically I've been trying to program a custom ScrollBar for my WinForm written in VB.NET and I don't seem to make it have a limit of the movement.
Code without limit tries:
Public Class GoldSrcScrollBar
Dim drag As Boolean
Dim mousey As Integer
Private Sub GoldSrcScrollBtn1_MouseDown(sender As Object, e As MouseEventArgs) Handles GoldSrcScrollBtn1.MouseDown
drag = True
mousey = Cursor.Position.Y - GoldSrcScrollBtn1.Top
End Sub
Private Sub GoldSrcScrollBtn1_MouseUp(sender As Object, e As MouseEventArgs) Handles GoldSrcScrollBtn1.MouseUp
drag = False
End Sub
Private Sub GoldSrcScrollBtn1_MouseMove(sender As Object, e As MouseEventArgs) Handles GoldSrcScrollBtn1.MouseMove
If drag Then
GoldSrcScrollBtn1.Top = Cursor.Position.Y - mousey
End If
End Sub
I have no idea how to make it have a limit
Slider = GoldSrcScrollBtn1
ScrollBar Background = GoldSrcScrollBarBg1
Without limit it works like this:
As you can see, it gets out of the Panel (GoldSrcScrollBarBg1).
Could someone help me please? Thanks :)

border less form gets automatically resized during runtime in vb .net

I've created a small borderless form using vb .net. The form contains three square buttons. The size of the form is (93, 31). All good during the design of the form, but when I run the program the size of the form gets increased to somewhat like (98,34). I even switched between true and false for the autosize property of the form to check if it was the cause of the problem but that dint help.
How do I stop the form from resizing?
EDIT:
I made the form borderless by setting the form's FormBorderStyle property to None
Here's the code
Public Class OSD_Dialog
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
' The folllowing three subs are helpfull in making the form dragable
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
drag = True
mousex = Windows.Forms.Cursor.Position.X - Me.Left
mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
drag = False
End Sub
'The following sub is helpful in creating an outline on the border of the form
Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
MyBase.OnPaintBackground(e)
Dim rect As New Rectangle(0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)
e.Graphics.DrawRectangle(Pens.White, rect)
End Sub
Private Sub OSD_Dialog_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TopMost = True
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.BackColor = Color.Red
TransparencyKey = BackColor
End Sub
Add this to your Form_Load event:
Me.Size = New Size(93, 31)
Also ensure you have set AutoScaleMode to 'None' in design time

Picturebox / Panel tearing when drag vb.net

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.