Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I use the close (X) button to hide the form, and not close it. 1

Status
Not open for further replies.

RebeccaLynn

Programmer
Nov 17, 2003
250
US
I want to be able to hide the form without closing the whole program when I click the close (X) button.
 
in the closing event of the form put this code

e.cancel = true
me.hide


Christiaan Baes
Belgium
"What a wonderfull world" - Louis armstrong
 
Why would you do that?

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
How about minismising the form and setting its showontaskbar property to false. Then no one would know the form was there.

JP
 
chrissie .. Thank you.

Sunaj .. I have program I want to un in the back ground, and want it to be hidden with the X button, as many apps like this do (making it conform to what folks are use too. I already have a context menu to exit, so don't need the X to do it.

WJProctor .. I already do something like that, just want the X button to do it as well.

Thanks for all the replies.

Becca
 
RebeccaLynn, It just that when I press the X button I expect the program to close - and not continue to run in the background (Actullay I can't think of any MS programs that does not unload when you click the X).
You could show a icon in the system tray that indicates that the program still runs and at the same time let the user have a possiblility to show the application window agian.
It could look like:
----------------------------------------------------------
Private ForceExit As Boolean

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If Not ForceExit Then
NotifyIcon1.Visible = True
e.Cancel = True
Me.Hide()
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ForceExit = False
Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
NotifyIcon1.Icon = New Icon("S:\janus\icons\ego\end.ico")
NotifyIcon1.Text = "Form1 (NotifyIcon example)"
NotifyIcon1.Visible = False
AddHandler NotifyIcon1.DoubleClick, AddressOf NotifyIcon1_DoubleClick
End Sub

Private Sub NotifyIcon1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Show()
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
ForceExit = True
Me.Close()
End Sub
----------------------------------------------------------


Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Just a thought,

Why not set you formborderstyle to none, create any borders with panels and then put your own buttons on there.
 
Here is just a short list of programs that do not close with the X ... AIM, Yahoo Messanger, MSN Messanger, RX-Minder, Web-Shots, AVG-AntiVirus, Date-Manager. There are many more, but I can't think of them just now. Not all my programs will behave this way. But some need to be a bit harder to accidently turn of then others (I do however give the option to let the X turn the program off, if the user so chooses it to do so) ... I love options, :)
 
the reason why i use it.

when you have a main form from wich other forms are opened for a example subform in a database, or a resultset from a query or an image. then i initialize it once in my main form and keep it open in the background with the me.hide so it doesnt have to be recreated all the time just once the first time it is open because if a form is closed it needs to be renewed for example

in the main form

dim frm as new subfrm

private sub btn_onclick(...) handles btn.click
frm.show()
end sub

if the user clicks the first time on the btn then the form will load (trigering the load event), the second time it will just show. (not triggering the load event) now if you dont use the me.hide and just let it close. the program will produce an error because it is disposed. if you hide it it will be reopend like nothing happens.

hope this clears things up.




Christiaan Baes
Belgium
"What a wonderfull world" - Louis armstrong
 
Well it's not that I wanted to start a long discussion on that, but just a few comments:
RebeccaLynn I don't know all the programs that you list above, but I do believe that most of them work as I've shown above, with an icon in the system tray when no forms are visible. Those that do not are services (I don't know AVG-Virus, but all other virus programs that I know are services, not applications).
chrissie1 That is a totally different case - you always have a form open, so the user always sees a form and therefore know that the application is running.

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
The short answer to why I wanted to do it is, my beta testers requested it :)

as to long discusions ... good way to learn new things and share ideas.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top