Sign in to follow this  
Nodger

[.net] How to write a "good" c# program

Recommended Posts

Here's the deal: Im just getting started with C# and I need a guide as to how to write a "proper" c# application. When I create a new C# project in visual studio, I notice it creates a file called "Program.C#" which runs the main form, form1. Does this "Program" file serve any purpose? I guess what Im looking for is a template. My plan is to write a file-sharing system that begins by starting a few threads, to handle networking etc, then when the user closes down form1, the app will not shut down but will be minimized to the system tray. Can anyone point me in the direction of some good books/guides/tutorials/examples that explain this clearly?

Share this post


Link to post
Share on other sites
Just handle the Closing event.


using System.Windows.Forms;

public class MainWindow : Form
{
public MainWindow()
{
// register to the Closing event
this.Closing += new CancelEventHandler(MainWindow_Closing);
}

[STAThread]
static void Main()
{
// run the application as long as MainWindow exists
Application.Run(new MainWindow());
}

// heandel the event
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// cansel the shutdown of the Form
e.Cancel = true;
// hide the Window;
this.Hide();
// or Minimize the window
this.WindowState = FormWindowState.Minimized;
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Rob Loach
Quote:
Original post by iwandi
Just handle the Closing event.
Application.Exit();
Might be better as it "cleanly" sends a close message to all application processes.



But he doesnt want to clsoe the application, he wants to minimize it rather than terminate, thus the handling of the event by canceling it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Cybrosys
He wants it in the tray bar, how does one do that? (The icons to the bottom right of the start menu)


Easily. Heres a brief, semi step by step way to do it:

Add a private variable,

System.Windows.Forms.NotifyIcon trayIcon;


Catch the onClose event of your main form (copied from Iwandi's post):

(in constructor)

this.Closing += new CancelEventHandler(MainWindow_Closing);

and as a method:

private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)


Now, in that method:

private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.cancel = true;
this.trayIcon = new NotifyIcon();
this.trayIcon.Icon = this.Icon;
this.trayIcon.Click += new EventHandler(trayIcon_Click);
this.trayIcon.Visible = true;
this.Hide();
}

then add the event handler trayIcon_click:

private void trayIcon_Click(object sender, EventArgs e)
{
this.Show();
((NotifyIcon)sender).Dispose();
}



And that should be it. I may have made a typo or two in there, but that should get you aimed in the right direction.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rob Loach
Might also want to check ALT-TAB to see if it shows up there. I had to fix that bug in ScreenCap a while ago.



Hmm, Doing it my way (calling Hide()), the form doesnt show up in the alt-tab list, atleast not for me on my computer. I think it might if you did something like set the form's windowState to minimize and then set it's visibleintaskbar option to false that it might show up when you alt tab. Interesting thing to look into though.



[Edited by - xanin on July 14, 2005 10:29:36 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this