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

This topic is 4757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 on other sites
Quote:
 Original post by iwandiJust handle the Closing event.
Application.Exit();
Might be better as it "cleanly" sends a close message to all application processes.

Share on other sites
Quote:
Original post by Rob Loach
Quote:
 Original post by iwandiJust 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 on other sites
He wants it in the tray bar, how does one do that? (The icons to the bottom right of the start menu)

Share on other sites
Quote:
 Original post by CybrosysHe 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:

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 on other sites
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.

Share on other sites
Thanks! I'll check out those

Share on other sites
Quote:
 Original post by Rob LoachMight 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]

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• Forum Statistics

• Total Topics
631781
• Total Posts
3002319
×