vb.net close winforms application from title bar X button - vb.net

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

Related

Form last location when minimized to system tray loop

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

vb.net detect keypress on a form

I am new to vb.net and am trying to detect a KeyPress on a Form
I have accomplished this in JavaFX by creating a listener that when the ESC Key is press the application close
I have not found any code examples that use a listener in vb.net
I have found code that Handles a KeyPress for a TextBox but the same code for a Form FAILS
For this function to close the application from any Form I am wondering if it needs to be declared in a Module? While that part of the question would be nice to know Call it a Bonus
My question is why is this code not detecting a keypress on frmOne ?
The code to detect a keypress in the txtBoxOne runs as expected
Public Class frmOne
Private Sub frmOne_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
'frmOne Property FixedToolWindow
'frmOne is the Start Up Form
If Asc(e.KeyChar) > 1 Then
MessageBox.Show("You Pressed " & e.KeyChar)
End If
'If Asc(e.KeyChar) > 1 Then txtBoxOne.Text = "You Pressed"
End Sub
Private Sub txtBoxOne_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtBoxOne.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
MsgBox("Error.")
Else
e.Handled = False
End If
End Sub
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
'txtBoxOne.Text = "Enter"
MessageBox.Show("Enter Data")
txtBoxOne.Select()
Return
End If
Dim OBJ As New frmTwo
OBJ.SPass = txtBoxOne.Text
OBJ.Show()
'MyTextBox_Enter()
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
Public Sub MyTextBox_Enter()
txtBoxOne.Clear()
End Sub
'Private Sub frmOne_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Use code below if pre populated text in txtBoxOne
'Me.ActiveControl = txtBoxOne
'txtBoxOne.Select(txtBoxOne.Text.Length, 0)
'txtBoxOne.Select()
'End Sub
End Class
The same code will work for a form but a form will not raise keyboard events by default if a child control has focus. You need to set the form's KeyPreview property to True, in which case the form will raise those keyboard events before the active child control does.

How to: Force users to focus a winform before selecting something from it

I am working on a project that uses WinForms, and I am running into concurrency problems when users click controls on a Form while it isn't the active window. My forms refresh whenever a user activates them to prevent them from showing old/dirty info, but if they click a button on an inactive Form the data doesn't get refreshed, which can cause a problem.
I know some programs in windows force you to select the window before you can preform any other actions, is there a way to do this in my VB.NET program?
This code run in my test, but some issues that must be handled to run as our need:
Dim mystts As Boolean = False
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
'here your process, unfortunately this will be performed too after messagebox close back to form2
'for example the process are:
For myCnt As Integer = 1 To 4000
Debug.Print("Test") 'This only for waiting
Next
mystts = False
End Sub
Private Sub Form2_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
'we must define the other criteria for make mystts=true, for example if
'we call messageBox.show, we can pass out from this for example we have msgFlag
'before call messageBox set msgFlag=true and after it set msgFlag=false
'and here you add: if msgFlag=true then exit sub
mystts = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'if we click this button and myStts=true, the next process will wait until process in form activate finish
MsgBox("this My Process, run after form2 activated and process finish")
End Sub

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

Click Button Event in VB that displays a message about user input

I have a really quick and simple question, I’m learning programming in C# and VB and I need to create a GUI application in Windows Form with Visual Studio. This GUI will prompt the user to enter in an integer. I believe I have this part alright but then I need to have the user click a button that will convert the user's entry to an integer and display a message indicating whether the user was successful. I think I even have the conversion done correctly but I am having a problem displaying that message if the user was successful. Basically I need to know how to function the click method in VB that will allow this message to appear. Any help with this would be greatly appreciated. The following code is what I have already written for this project:
Public Class Form1
Private Sub EvaluateInput()
Dim InputValue As String
InputValue = ValueTextBox.Text
If IsNumeric(InputValue) Then
MessageBox.Show(InputValue & " is a number.")
Else
MessageBox.Show(InputValue & " is not a number.")
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 'Continue Button
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Exit Button
Dim button As DialogResult
button = MessageBox.Show _
("Are you sure you want to exit this application?", _
"Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
If button = Windows.Forms.DialogResult.Yes Then
Me.Close()
Else
'Do Nothing
End If
End Sub
End Class
If I understand your question correctly, then you would need a simply change:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) _
Handles Button2.Click 'Continue Button
EvaluateInput()
End Sub
When you press Button2, it will call the EvaluateInput sub and display a message accordingly.