Form last location when minimized to system tray loop - vb.net

I am using the application setting Location of the form to store the application last location so that when the app is opened again, it opens exactly where I left off.
I also use the NotifyIcon control to send the application to the system tray when minimized. An odd behavior is if the app is in the system tray when it is killed (ie through task manager) then the next time the app is opened, it opens to the system tray. Which is fine (although I prefer it to open normally on the screen), but when you double click on the icon in the system tray the app moves to the task bar, and if I click the icon on the taskbar it moves back to the system tray and then gets stuck in an infinite loop.
I am using the form_resize method as such:
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
NotifyIcon1.Visible = True
NotifyIcon1.Icon = Me.Icon
NotifyIcon1.ShowBalloonTip(50000)
ShowInTaskbar = False
End If
End Sub
And then setting the doubleclick method of NotifyIcon as:
Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
ShowInTaskbar = True
Me.WindowState = FormWindowState.Normal
NotifyIcon1.Visible = False
End Sub
Everything works fine if I don't use the last location setting but that means the application opens up in random places. As soon as I add the last location setting it exhibits the behavior above.
Any ides what I'm doing wrong?

Try this code
Dim IsLoaded As Boolean
Private Sub Form1_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
If IsLoaded Then
SaveSetting("My App", "App settings", "LocationX", Location.X.ToString)
SaveSetting("My App", "App settings", "LocationY", Location.Y.ToString)
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Location = New Point(CInt(GetSetting("My App", "App settings", "LocationX", "0")), _
CInt(GetSetting("My App", "App settings", "LocationY", "0")))
IsLoaded = True
End Sub

Related

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 can I pop-up window when minimize the application?

Visual Basic Window Form Application problem
Hi, I am using Visual Studio 2010, i am facing a problem that
I need to make a timer function, when the timer end, the application will pop-up out the screen even the application is minimized.
I tried to google and use MsgBox but still the application do not pop-up the screen!
Have a go at this
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.TopMost = True
Timer1.Enabled = False
MsgBox("Time to take a break! Yay!!", MsgBoxStyle.Information, "Breaktime")
MsgBox("Click OK when you've had a break!", MsgBoxStyle.Question, "Waiting!")
Timer1.Enabled = True
Me.TopMost = False
End Sub
I know this is an old topic but look into:
Me.WindowState = FormWindowState.Minimized
Me.WindowState = FormWindowState.Normal
Me.WindowState = FormWindowState.Maximize

In the RichTextBox control DragEnter event fires, but DragDrop event does not fire

I have a test Windows Forms application in VB.NET, which consists of a Form, a RichTextBox, a Label, and a Timer. I need to perform drag-and-drop operations from a Mozilla Firefox browser window into that RichTextBox, namely I need to drag-and-drop text from a web page and web page URLs from the address bar.
Here is the test code of the application:
Public Class Form1
Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.Text) Then
Label1.Text = "Status: DragEnter"
Timer1.Enabled = True
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop
Label1.Text = "Status: DragDrop"
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = "Status:"
Timer1.Enabled = False
End Sub
End Class
When I run this application I can drag-and-drop text from a web page, both events fire. But when I try to drag-and-drop web page URLs from the address bar, only the DragEnter event fires, but the DragDrop event does not fire, so the dropping never occurs, despite the DataObject is created and it does contain "DataFormats.Text".
How can I fix this?
Thank you.

How do I display an Icon on the Taskbar but not on the form itself?

If I use ShowIcon in the form properties, I get the icon in the top left of my form (I don't want that), but I do get my icon in the taskbar.
If I turn ShowIcon off, then I get no icon on the form but I get the default winform icon on the taskbar.
I've tried changing border style, but FixedToolWindow creates an undesirable look that doesn't match the style of the other forms.
I have found a workaround. If you do Me.ShowIcon = False after the form is loaded, then it will display in the taskbar, but not on the program.
One way to do this is to have a timer enabled/begin as soon as form load ends, and then on tick, do Me.ShowIcon = False
As Below:
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Me.ShowIcon = False
Timer1.Enabled = False
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Timer1.enabled = True
End Sub
Timer1 has an interval of 100ms (which works). If you just put the ShowIcon as True in the Form1_Load, a weird Icon shows (not the program's original icon). This is why we use the Timer.

VB.Net program minimized not working

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