VB.Net program minimized not working - vb.net

I have a code like below
Private Sub InterfaceProg_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
'program minimized
Try
If Me.WindowState = FormWindowState.Minimized Then
Me.Visible = True
NotifyIcon1.Visible = True
NotifyIcon1.ShowBalloonTip(1, "Browser Bandwidth Optimizer", "Program Minimized", ToolTipIcon.Info)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
The code is working fine but there is one problem.
When i press the minimize button on top right, the program go to system tray
When i press close button, the program also go to system also go to system tray.
i want to make the program go to system tray if user press close button only and minimized the program to taskbar if user press minimized. how to do it?

Use the FormClosing event instead of the Resize event:
Private CloseAllowed As Boolean
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not CloseAllowed And e.CloseReason <> CloseReason.WindowsShutDown Then
Me.Hide()
e.Cancel = True
NotifyIcon1.Visible = True
'' etc..
End If
End Sub
You still need to give the user a way to exit the program. The context menu for the NotifyIcon is the usual approach. Add an Exit item:
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
CloseAllowed = True
Me.Close()
End Sub

When ever you press the minimize or close buttons a WM_SYSCOMMAND message is sent. The WPARAM specifies which button is pressed:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = &H112 Then 'WM_SYSCOMMAND
If CInt(m.WParam) = &HF060 Then 'SC_CLOSE, the close button is pressed
Me.Visible = False
Me.ShowInTaskbar = False
Return 'cancel the message
End If
If CInt(m.WParam) = &HF020 Then 'SC_MINIMIZE, the minimize button is pressed
'do your staff
End If
End If
MyBase.WndProc(m)
End Sub
WM_SYSCOMMAND message

Then you need to call your Code of "InterfaceProg_Resize" in the form closing even.
So make a new sub "Private Sub ProgToTray()"
Also you don't need a try catch for it.
You also need to hide the Taskbar Icon "Me.ShowInTaskbar = False"
And minimize the form"Me.WindowState = FormWindowState.Minimized"
Private Sub ProgToTray()
Me.ShowInTaskbar = False
Me.WindowState = FormWindowState.Minimized
NotifyIcon1.Visible = True
NotifyIcon1.ShowBalloonTip(1, "Browser Bandwidth Optimizer", "Program Minimized", ToolTipIcon.Info)
End Sub
If the user clicks x on your form ,Then you need to Cancel the closing of the form in the FormClosing event."e.Cancel = True"
Then you call "ProgToTray()"
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
e.Cancel = True
ProgToTray()
End Sub
Then if you close the form with x it will go to the notify area.
Don't forget to make a click event to open the form again from the notify icon and an option so the user can exit the form.
To make an exit button you need to add a ContextMenuStrip1 to your form .
To open the ContextMenuStrip1 when you right click the NotifyIcon1 you need add the code "ContextMenuStrip1.Show(Cursor.Position)" to you NotifyIcon1 click event
You also need to check which button is clicked.
"If e.Button = Windows.Forms.MouseButtons.Right Then" for the right button.
and
"e.Button = Windows.Forms.MouseButtons.Left Then" for the left button.
So if the user clicks left the form will open again and when he clicks right the ContextMenuStrip1 will show.
If the user clicks left mouse button you set the formwindowstate back to normal.
Also show the taskbar icon again and hide the tray icon.
Private Sub NotifyIcon1_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
ContextMenuStrip1.Show(Cursor.Position)
ElseIf e.Button = Windows.Forms.MouseButtons.Left Then
Me.WindowState = FormWindowState.Normal
Me.ShowInTaskbar = True
NotifyIcon1.Visible = False
End If
End Sub
Then you make an click event for the exit button and remove the handler for the form closing event "RemoveHandler MyBase.FormClosing, AddressOf Form1_FormClosing".
The canceling of closing the form will not take place.
Then call me.close()
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
RemoveHandler MyBase.FormClosing, AddressOf Form1_FormClosing
Me.Close()
End Sub

Related

Application.Exit() not working when using form closing methods to minimise to tray

I am using this code to minimise the form to the tray when the X button is pressed
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
NotifyIcon1.Visible = True
NotifyIcon1.Icon = SystemIcons.Application
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipTitle = "Running In the Background"
NotifyIcon1.BalloonTipText = "Application is running in the Background. Double click it to maximize the form"
NotifyIcon1.ShowBalloonTip(50000)
Me.Hide()
ShowInTaskbar = True
e.Cancel = True
End Sub
I have a Quit button too which will actually exit the application, but I think it's using the code above to minimise the form instead.
Private Sub btn_Quit_Click(sender As Object, e As EventArgs) Handles btn_Quit.Click
Dim confirm As DialogResult = MessageBox.Show("Are you sure you wish to Exit the App?", "Exit Application?", MessageBoxButtons.YesNo)
If confirm = DialogResult.Yes Then
Application.Exit()
End If
End Sub
How can I override the FormClosing sub when using the Quit button?
I tried using End but that didn't work either
You should use the FormClosingEventArgs passed to Form_Closing handler. It will tell you whether someone tried to close the Form, or if the application is exiting. There are other reasons too which you can check out
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Select Case e.CloseReason
Case CloseReason.ApplicationExitCall
e.Cancel = False
Case CloseReason.FormOwnerClosing
NotifyIcon1.Visible = True
NotifyIcon1.Icon = SystemIcons.Application
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipTitle = "Running In the Background"
NotifyIcon1.BalloonTipText = "Application is running in the Background. Double click it to maximize the form"
NotifyIcon1.ShowBalloonTip(50000)
Me.Hide()
ShowInTaskbar = True
e.Cancel = True
End Select
End Sub

How to handle a form close event from another form (VB.Net)

I have a Form1, when we close that Form1 and Form2 displays and I have added 2 Labels, "Are you sure?" and "Do you really want to exit?" and there are 3 buttons called "Yes", "No" and "I don't care". I know how to handle a closing form event using a message box it's:
MessageBox("Do you really want to exit?", "Are you sure?", MessageBoxButtons.YesNo) = DialogResult.Yes Then
e.Cancel = True
Else
e.Cancel = False
But I don't know how to stop closing Form1 from Form2. Thanks in advance!
here is how I would go about solving this problem, use this code in Form1:
Public Class Form1
'handle the Closing event of Form1 to be able to cancel the closing
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
'if Form2 returns a dialog result of Cancel, cancel the closing operation, otherwise let it continue
If Form2.ShowDialog = DialogResult.Cancel Then
e.Cancel = True
End If
End Sub
End Class
and this code in Form2:
Public Class Form2
'if the Ok button is clicked, return a dialog result of OK
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
DialogResult = DialogResult.OK
End Sub
'if the cancel button is clicked, return a dialog result of Cancel
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
DialogResult = DialogResult.Cancel
End Sub
End Class
Depending on which button the user clicks in Form2, Form1 will either close or remain open.
If you have any questions, don't hesitate to post a comment on my answer and I'll be glad to direct you further.

vb.net close winforms application from title bar X button

Using VB.Net on a winforms application I have two forms
Both forms are set to "When Last Form Closes"
Both forms have "Key Preview" set to TRUE
frmOne the start up form has a "Form Border Style" set to FixedToolWindow
Here are the sequence of events preformed with the two forms
Load frmOne and click the X button on the title bar form closes and the start button shows up in the IDEThis is the desired behavior
Second sequence of events load app frmOne has a button to navigate to frmTwo and frmTwo has a button to navigate back to frmOne When we arrive back at frmOne we click the X button on the title bar and frmOne close but the app is still running NO Start button in the IDE
The Question is how to close the application after navigating to other forms?
The Code Below are the various ways I have tried to solve this issue
I am including the KeyPress Code which does not exhibit the desired results
Private Sub frmOne_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
'frmOne Property KeyPreview needs to be set to True
If Asc(e.KeyChar) = 27 Then
'MessageBox.Show("You Pressed " & e.KeyChar)
Me.Close()
End If
End Sub
'Public Sub frmOne_QueryClose(Cancel As Integer, CloseMode As Integer)
'Prevent user from closing with the Close box in the title bar.
'If CloseMode = 1 Then Me.Close()
'If CloseMode = 1 Then Application.Exit()
'End Sub
'Private Sub frmOne_FormClosing(sender As Object, e As FormClosingEventArgs) 'Handles Me.FormClosing
'Application.Exit()
'Me.Close()
'End Sub
Private Sub frmOne_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
'Application.Exit()
End Sub
Code to frmTwo
Private Sub btnToFormTwo_Click(sender As Object, e As EventArgs) Handles btnToFormTwo.Click
Dim i As Integer
i = txtBoxOne.Text.Length
If i = 0 Then
MsgBox("Enter Data")
txtBoxOne.Select()
Return
End If
Dim OBJ As New frmTwo
OBJ.SPass = txtBoxOne.Text
OBJ.Show()
lblFormOne.Text = " "
txtBoxOne.Clear()
Me.Hide()
'Me.Close()'R Click project PassVar Set Start Up Form
'Best Solution is to have Splash Form as Start Up Form
End Sub
Code Back to frmOne
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
frmOne.txtBoxOne.Text = "Back from 2"
Me.Hide()
frmOne.Show()
End Sub
You need to close Forms to get the application to end. I use "When last form closes" in a project with maybe a dozen forms and it works fine.
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
frmOne.txtBoxOne.Text = "Back from 2"
'Me.Hide() 'frmTwo is still running, To close a form use .Close()
Close()
frmOne.Show()
End Sub
You had your vb project set to quit "when last form closes" - opening a second form from your startup form meant that when you closed the startup form, there was still another form open so the app did not quit. Changing the setting to "when startup form closes" made the app behave as you expected

How to prevent a form from closing in btnClose_Click

I am trying to prevent the user from closing the form when the DialogResult.No is True in vb.net. I also tried e.cancel=true but it does not work in btnClose_click. I mentioned that is not FormClosingEventArgs. I want put it in btnClose_Click.
Code copied from OP comment;
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
If CustomMessageBox.Show("Are You Sure?", Buttons.YesNo, Icons.Question,
AnimateStyle.ZoomIn) = Windows.Forms.DialogResult.Yes Then
If Windows.Forms.DialogResult.Yes Then
' ---Close The Form
End If
ElseIf Windows.Forms.DialogResult.No Or Windows.Forms.DialogResult.None Then
' ---Not Closing the Form
End If
End Sub
You need to use the FormClosing Event (or Closing befoce NET 2.0). This Event occurs before the close event and can be canceled.
Private Sub Form1_Closing(sender As Object, e As FormClosingEventArgs ) Handles Me.FormClosing
If CustomMessageBox.Show("Are You Sure?", Buttons.YesNo, Icons.Question,
AnimateStyle.ZoomIn) = Windows.Forms.DialogResult.Yes Then
e.Cancel = true ' <<<< close event will not occur, form stays open
End If
End Sub
FormClosing Event

How to detect if button1 was pressed in a If statement. VB.NET

I'm trying to make it so when the button is pressed, it disables the button and allows you to select the mouse coordinates. (With right click) then enable back the button. How do you detect if button1 was pressed for the if statement while keeping the timer?
If Button1.clicked Then
This is where I need the If statement to detect button1 being pressed.
Private Sub Timer2_Tick(sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If Button1.clicked Then
Button1.Enabled = False
If GetAsyncKeyState(2) Then
TextBox1.Text = Cursor.Position.X
TextBox2.Text = Cursor.Position.Y
Button1.Enabled = True
End If
End If
End Sub
Thanks with the help of #the_lotus I found a way around the timer/button issue.
Instead of looking for the button press within the timer statement, you control if the timer is on or off.
Lotus also asked for the reasoning behind the timer. The timer is so the form always looks for a input, not just when the button is pressed.
Private Sub Button1_Click(sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
Timer2.Interval = 5
Timer2.Enabled = True
Me.Button1.Text = "Set Posistion 1"
End Sub
Sub Timer2_Tick() Handles Timer2.Tick
If GetAsyncKeyState(2) Then
TextBox1.Text = Cursor.Position.X
TextBox2.Text = Cursor.Position.Y
Button1.Enabled = True
Timer2.Enabled = False
Me.Button1.Text = "Reset Position"
End If
End Sub