Un-maximize size of the form - vb.net

I'm having a little something in my program that's bothering me and I would like some help please.
My form size is: 1366; 768 (that's the max resolution of the laptop I use). The form loads and it's a little bit smaller than my screen size, so I changed the load event of the form so that it starts maximized.
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
End Sub
Now, when I press the maximize button while the form is maximized (un-maximize), the form gets a little bit smaller. Can I choose what size to un-maximize? I want the form to be 800 x 600 when I press that button.
Thanks in advance.

Well, if you need the un-maximized event to be different than the designer size, then just set it in your OnLoad override:
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
Me.WindowState = FormWindowState.Maximized
Me.Size = New Size(800, 600)
End Sub

Related

Custom minimize button loses original minimize button animation

I would like to know if there is a way of keeping the Original Animation to keep my VB application as close to original as possible.
Private Sub Minimize_Click(sender As Object, e As EventArgs) Handles Minimize.Click
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
End Sub
I expect the minimize action to play the animation that ocurs in most if not all of the windows applications.
[Edit]:
All the apps I have installed have the default minimize animation because they all use the same windows bar, but my program doesn't because I find it not very attractive, so I created my own Print By programming the button myself I lose the animation.
You could use the opacity to create an animation of sorts.
Private Sub Minimize_Click(sender As Object, e As EventArgs) Handles Minimize.Click
For disapper As Single = 1.0! To 0 Step -0.2!
Me.Opacity = disapper
'Me.Refresh()
System.Threading.Thread.Sleep(50)
Next
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
Me.Opacity = 1.0!
End Sub
So I found that the problem is that my formborderstyle propety is set to none and the animation isn't shown when it is set to none I solved it by doing this, but even tho it works it worses things even more... For now I will stick with having no animation at all unless someone finds a solution. Thanks.
Private Sub Minimize_Click(sender As Object, e As EventArgs) Handles Minimize.Click
Me.FormBorderStyle = FormBorderStyle.FixedSingle
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
Me.FormBorderStyle = FormBorderStyle.None
End Sub
Any app minimization animation is in windows settings, and can be turned off anyway. I Don't think you can force it if you have it disabled in your userprofile System Properties settings.
This can be found in sysdm.cpl in Advanced -> Performance

Improve UI responsiveness on windows form application

I am currently working on a project and decided to create a user interface for it using visual studio with a windows forms application(Visual Basic).
The problem I'm facing is that the user interface doesn't respond as quickly and smoothly as I'd like it to.
Mainly, I am using pictures as buttons to make the user form look more modern.
However, when I hover my mouse over a "button" it takes a while until the "highlighted button" appears.
P1 is the picture of the "normal button" and P2 is the picture of the "highlighted button".
Here is the short code I have for now:
Public Class Main
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub PictureBox1_MouseHover(sender As Object, e As EventArgs) Handles P1.MouseHover
P1.Visible = False
P2.Visible = True
End Sub
Private Sub P2_MouseClick(sender As Object, e As MouseEventArgs) Handles P2.MouseClick
'Call cmdInit()
'Call cmdConnectRobot()
'Call cmdUnlock()
End Sub
Private Sub Main_MouseHover(sender As Object, e As EventArgs) Handles Me.MouseHover
If P2.Visible = True Then
P2.Visible = False
P1.Visible = True
End If
End Sub
Private Sub P4_Click(sender As Object, e As EventArgs) Handles P4.Click
End Sub
End Class
Another problem I'm facing is that when I call other subs, the user form becomes unresponsive while the sub is running.
I researched and found that I could implement multi threading or async tasks but I'm a bit lost and would be extremely grateful if someone could guide me or point me in the right direction.
Thanks in advance!!
In this case your UI is responsive, however the MouseHover event is only raised once the mouse cursor has hovered over the control for a certain amount of time (default is 400 ms), which is what is causing the delay.
What you are looking for is the MouseEnter event, which is raised as soon as the cursor enters ("touches") the control:
Private Sub P1_MouseEnter(sender As Object, e As EventArgs) Handles P1.MouseEnter
P1.Visible = False
P2.Visible = True
End Sub
You can then use that together with the MouseLeave event on the second picture box to switch back to the non-highlighted image:
Private Sub P2_MouseLeave(sender As Object, e As EventArgs) Handles P2.MouseLeave
P1.Visible = True
P2.Visible = False
End Sub
However switching picture boxes like this is not optimal. I recommend you to look into how you can use Application Resources, then modify your code to only switch the image that one picture box displays.
Here are the basic steps:
Right-click your project in the Solution Explorer and press Properties.
Select the Resources tab.
To add an image either:
a. Drag and drop the image onto the resource pane.
b. Click the arrow next to the Add Resource... button and press Add Existing File....
Now, in your code add this right below Public Class Form1:
Dim ButtonNormal As Image = My.Resources.<first image name>
Dim ButtonHighlighted As Image = My.Resources.<second image name>
Replace <first image name> and <second image name> with the names of your button images.
Now you only need one picture box for the button:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
P1.Image = ButtonNormal
End Sub
Private Sub P1_MouseEnter(sender As System.Object, e As System.EventArgs) Handles P1.MouseEnter
P1.Image = ButtonHighlighted
End Sub
Private Sub P1_MouseLeave(sender As System.Object, e As System.EventArgs) Handles P1.MouseLeave
P1.Image = ButtonNormal
End Sub
I'll start by saying i'm not a programmer by trade, and i'm sure someone will point out better ways of doing these things, but in regards to the threading question it's fairly simple to implement.
Imports System.Threading
Public Class Form1
Dim WorkerThread As New Thread(AddressOf DoWork)
'WorkerThread' can be any name you like, and 'DoWork' is the name of the sub you want to run in the new thread, and is launched by calling:
WorkerThread.start()
However there is a catch, the new thread is not able to interact directly with the GUI, so you cannot change textbox text etc... I find the easiest way to get changes made to the GUI is to drag a timer onto your form, and have the new thread change variables (pre-defined just below Public Class Form1), then use the Timer1 Tick event to monitor the variables and update the GUI if there are any changes.

Scroll through a user control while timer is running

Whenever I click the area of the user control, it highlights the combobox, making it so that if I attempt to scroll to the bottom of the screen, it only scrolls through the combobox.
I tried putting somelabel.focus() in the click event for the user control. This kind of works. However, this does not allow me to scroll to a part of the control where somelabel is not visible, which seems a little strange to me. I obviously need to be able to scroll all the way to the bottom of the usercontrol.
Additional info: This is a winforms project, and the user control is running on a timer, where the only controls are the combobox and some labels that have their .text property updated every time the timer loops.
UPDATE: I tried disabling the timer, and it appears that after changing the focus to one of the labels, I am now able to scroll all the way to the bottom of the page. So it seems its an issue with the timer.
Here is some code that replicates the problem. If you click on the panel, it gains focus, but it will not let you scroll using the scrollwheel. It will bring the top of the panel back to the top of the screen. If you don't turn on the timer, however, you can freely scroll.
Public i As Integer
Public iString As String
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
iString = i.ToString()
Label1.Text = iString
Label2.Text = iString
Label3.Text = iString
Label4.Text = iString
Label5.Text = iString
i += 1
End Sub
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Panel1_Click1(sender As Object, e As EventArgs) Handles Panel1.Click
Panel1.Focus()
End Sub
You could add a Panel with Dock=Fill and AutoScroll=true in which you insert your controls. Then add the click event for the panel:
private void panel1_Click(object sender, EventArgs e)
{
this.ActiveControl = panel1;
}
This way if you click inside the panel (which fills the whole UserControl), the panel will be active and you can scroll. This won't affect controls inside the panel so you can work with them in the usual way.

Animate Picturebox in VB

I am new to VB and just can't figure it out how to animate a button using the MouseHover Event..
I want to create a single loop for all the buttons(picturebox) in my project that will increase the button's size when the user rests the mouse on it.
Maybe something like:
For Each Form As Form In Application.OpenForms
For Each Control As Control In Form.Controls
Tks. Any help is appreciated.
Use Inherits to create a new button (or PictureBox) class for you purpose. Here is the code.
Public Class cuteButton
Inherits System.Windows.Forms.Button
Protected Overrides Sub OnMouseHover(e As EventArgs)
'
'Wite code here to change button size or whatever.
'
MyBase.OnMouseHover(e)
End Sub
End Class
A very simple way is to use a common MouseHover event to grow your buttons (which I guess is really a Picturebox with an image in it):
Private Sub CustomButton_Grow(sender As Object, e As System.EventArgs) Handles Picturebox1.MouseHover, Picturebox2.MouseHover
'Set a maximum height to grow the buttons to.
'This can also be set for width depending on your needs!
Dim maxHeight as single = 50
If sender.Height < maxHeight then
sender.Size = New Size(sender.Width+1,sender.Height+1)
End if
End Sub
Then you can reset all buttons in a snap using the MouseLeave event. If you want that part animated as well then you can to use a global shrink routine that constantly shrink all buttons but the one in MouseHover. Good luck!
This Will Work even if you have 10,000 button or picture box ....
I am Assuming that you only have 1 form and many Buttons ,,, you have to be specific on your question
This Code will work fine with Buttons, Picturebox ,text box
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each cntrl As Control In Controls ' Looping for each Button as Control
If TypeOf cntrl Is Button Then
AddHandler cntrl.MouseEnter, AddressOf cntrl_MouseEnter ' Adding the event and handler
AddHandler cntrl.MouseLeave, AddressOf cntrl_MouseLeave ' Adding the event and handler
End If
Next
End Sub
'' Assuming you wanna eNlarge everytime the mouse Hover or Enter
Private Sub cntrl_MouseEnter(sender As Object, e As EventArgs)
CType(sender, Button).Size = New Point(CType(sender, Button).Size.Width + 50, CType(sender, Button).Size.Height + 50)
End Sub
'' Here it goes back normal size
Private Sub cntrl_MouseLeave(sender As Object, e As EventArgs)
CType(sender, Button).Size = New Point(CType(sender, Button).Size.Width - 50, CType(sender, Button).Size.Height - 50)
End Sub

VB.net Borderless Form Maximize over Taskbar

When I maximize my borderless form, the form covers the entire screen including the taskbar, which I don't want it to do. I'm finding it very difficult to find a solution to my problem on the net, all I've come up with is the below code, which displays the taskbar for a short time, but then disappears and the form still takes up the entire screen.
Private Sub TableLayoutPanel1_DoubleClick(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.DoubleClick
If e.Location.Y < 30 Then
Me.WindowState = FormWindowState.Maximized
Me.ControlBox = True
End If
End Sub
I'm starting to think the only way to solve my problem is to find the screen size height minus the taskbar height to get the form height, but I'm hoping there might be a simpler answer.
Use the form's Load event to set its maximum size, like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.MaximumSize = Screen.FromRectangle(Me.Bounds).WorkingArea.Size
End Sub
Maximizing it now restricts the size to the monitor's working area, which is the monitor size minus the taskbars.
Use the "working area" of the screen:
Me.Bounds = Screen.GetWorkingArea(Me)
You should better do this:
Me.MaximumSize = Screen.FromControl(Me).WorkingArea.Size
Because some users use multiple monitors. So if you use Hans Passant's way, when user maximize application to secondary monitor, application will get primary's monitor working area size and will look awful!!!
I think this is a better way to solve your problem
Private Sub main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Normal
Me.StartPosition = FormStartPosition.Manual
With Screen.PrimaryScreen.WorkingArea
Me.SetBounds(.Left, .Top, .Width, .Height)
End With
End Sub