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
Related
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
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
*I have a program that copies files from one directory to the other. I want it to run when minimised and an icon should show in the system tray so I created these codes. The minimise and system tray icon shows but AS SOON AS IT IS MINIMISED, THE COPYING SEIZES. I want the process to continue when the form is minimised. Please Help.
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
Notif.Visible = True
Notif.BalloonTipText = "Copying"
Notif.BalloonTipTitle = "Serious Copying"
Notif.ShowBalloonTip(5000)
Notif.Text = "Serious Copying"
ShowInTaskbar = False
Me.Visible = False
End If
End Sub
Private Sub notifycn1_DoubleClick(sender As Object, e As EventArgs) Handles notifycn1.DoubleClick
Me.Visible = True
Me.show()
ShowInTaskbar = True
Notif.Visible = False
Me.WindowState = FormWindowState.Normal
End Sub
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
I would like to create a program to run from the bottom right system tray of Windows.
But I don't know where to start from?
Can someone tell \ show me where to look and examples or what commands to use \ research ?
Add a NotifyIcon to the main windows form.
Use the Resize event in Form to control when to show the NotifyIcon and hide the form:
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
Me.Hide()
NotifyIcon1.BalloonTipText = "Hi from right system tray"
NotifyIcon1.ShowBalloonTip(500)
End If
End Sub
Use the events in NotifyIcon to show the form again:
Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
Me.Show()
Me.WindowState = FormWindowState.Normal
NotifyIcon1.Visible = False
End Sub
You can download a full example in AutoDNIE google code project
I review the answers I note that miss the icon.
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 = SystemIcons.Application
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipTitle = "Verificador corriendo"
NotifyIcon1.BalloonTipText = "Verificador corriendo"
NotifyIcon1.ShowBalloonTip(50000)
'Me.Hide()
ShowInTaskbar = False
End If
End Sub
Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
'Me.Show()
ShowInTaskbar = True
Me.WindowState = FormWindowState.Normal
NotifyIcon1.Visible = False
End Sub
You can also do:
Sub ToggleHide()
If Me.WindowState = FormWindowState.Normal Then
Me.ShowInTaskbar = False
Me.WindowState = FormWindowState.Minimized
Else
Me.ShowInTaskbar = True
Me.WindowState = FormWindowState.Normal
End If
End Sub