locked
change close button behaviour RRS feed

  • Question

  • Hey,

    I am trying to change the behaviour of my close button. I don't want the application to terminate when the close button is placed. I simply want the form to be hidden when the close button is pressed.

    Anyone have any ideas of doing this?

    I also wouldn't mind removing the close button, But I still want to keep the border. I don't want to set the WindowStyle to None and loose the border.

    Thanks!


    Oshara Narangoda
    Sunday, June 12, 2011 5:14 PM

Answers

  • I don't write in VB.NET but why you cannot use some converter c# to VB.NET ?

     

    Public Partial Class MainWindow
    	Inherits Window
    	Public Sub New()
    		InitializeComponent()
    		AddHandler Me.Closing, AddressOf MainWindow_Closing
    	End Sub
    
    	Private Sub MainWindow_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs)
    		e.Cancel = True
    		Me.Hide()
    
    		' later, call this.Show() to reinstate the window
    	End Sub
    End Class

    • Marked as answer by Oshara Friday, June 17, 2011 8:00 PM
    Tuesday, June 14, 2011 8:45 AM

All replies

  • This MSDN page explains what you need to do:

    http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(SYSTEM.WINDOWS.WINDOW.CLOSING);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22);k(DevLang-CSHARP)&rd=true

     

    public partial class MainWindow : Window
    	{
    		public MainWindow()
    		{
    			InitializeComponent();
    			this.Closing += MainWindow_Closing;
    		}
    
    		void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    		{
    			e.Cancel = true;
    			this.Hide();
    
    			// later, call this.Show() to reinstate the window
    		}
    	}
    


    Monday, June 13, 2011 2:02 PM
    Moderator
  • Yes this is what I am looking for. But I am coding in VB, how can I do this in VB?

    Thanks


    Oshara Narangoda
    Tuesday, June 14, 2011 1:10 AM
  • I don't write in VB.NET but why you cannot use some converter c# to VB.NET ?

     

    Public Partial Class MainWindow
    	Inherits Window
    	Public Sub New()
    		InitializeComponent()
    		AddHandler Me.Closing, AddressOf MainWindow_Closing
    	End Sub
    
    	Private Sub MainWindow_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs)
    		e.Cancel = True
    		Me.Hide()
    
    		' later, call this.Show() to reinstate the window
    	End Sub
    End Class

    • Marked as answer by Oshara Friday, June 17, 2011 8:00 PM
    Tuesday, June 14, 2011 8:45 AM