Sign in to follow this  
master_ball

[.net] C# tool development

Recommended Posts

master_ball    132
Hi, I've been looking everywhere for a good tutorial on how to make a windows application with C# and winforms. I'm wanting to make a simple 2D map making tool. Most of the examples I find are how to make a web browser. Has anyone found any in-depth tutorials on setting up various kinds of window apps?

Share this post


Link to post
Share on other sites
Estese    236
If you have Visual C# .NET, you can simply use the Form Designer to design your layout visually.

If you don't have the luxury of having a fancy IDE, and if you are using DX9, check out the SDK examples under C# and DirectDraw to get you started. There's a basic one under my installation path: "C:\DX9SDK\Samples\C#\DirectDraw\Windowed".

Share this post


Link to post
Share on other sites
master_ball    132
I can lay things out with the Form Designer, I have set up a main menu, and see how to tie in the code. Is there an easy way to do a OpenFile Dialog? I know how to call this in Win32, is there a way in C# with the Form Designer? If not is there a C# call for that dialog? Thanks@!

Share this post


Link to post
Share on other sites
Pipo DeClown    804

private void FileOpen_Clicked( object sender, System.EventArgs e )
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
DialogResult res = ofd.ShowDialog( this ); // This will make the ofd steal your mainwindows focus.
// You want this.

/* This code gets executed after the ofd closed */
/* So let's wait.. */
/* Hmppff.. */
/* DONE!! */

if( res == DialogResult.Ok )
{
/* Yay! */
}
}

Share this post


Link to post
Share on other sites
CWIZO    118
Go to www.windowsforms.com for beginners tutorials on how to build Windows Forms applications.

You can also check www.codeproject.com

Share this post


Link to post
Share on other sites
Toolmaker    967
The code shown by Pipo TheClown is flawed, since the OpenFileDialog/SaveFileDialog are unmanaged resources. And you are required to release unmanaged resource yourself.

There are 2 ways of doing it. The first one is doing it yourself:

OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
MyObject.Save(dlg.FileName);
}
dlg.Dispose(); // Release


The other way is by taking advantage of the C# language keyword "using":

using (OpenFileDialog dlg = new OpenFileDialog())
{
if (dlg.ShowDialog() == DialogResult.OK)
{
MyObject.Save(dlg.FileName);
}
} // <- End of "using" scope, dlg.Dispose() will auto called


Toolmaker

Share this post


Link to post
Share on other sites
liquiddark    350
Quote:
Original post by Toolmaker
The code shown by Pipo TheClown is flawed, since the OpenFileDialog/SaveFileDialog are unmanaged resources. And you are required to release unmanaged resource yourself.

Em. Where did you get this idea? System.Windows.Form.OpenFileDialog is part of the core .NET framework. I have difficulty imagining how it's going to be unmanaged...

Checking the MSDN page, I don't see any remarks on the subject, either...

ld

Share this post


Link to post
Share on other sites
Arild Fines    968
Quote:
Original post by liquiddark
Em. Where did you get this idea? System.Windows.Form.OpenFileDialog is part of the core .NET framework.

The OFD ultimately derives from Component. The docs for Component state:
Quote:

It is recommended that a Component release resources explicitly by calls to its Dispose method, without waiting for automatic memory management through an implicit call to Finalize. When a Container is disposed, all components within the Container are also disposed.

If you add an OFD to the form itself, the designer will create code to ensure it's disposed with the form(by adding it to the Container components instance - Take a look at YourForm.Dispose).
Quote:

I have difficulty imagining how it's going to be unmanaged...

It's a wrapper around the Win32 file dialog.

Share this post


Link to post
Share on other sites
liquiddark    350
I'm not being intentionally thick, i promise. I've just realized that you're considering the fact that the internal item is unmanaged. This is the case for pretty much any system resource, but I'd be interested to know why components in particular recommend not waiting for garbage collention.

ld

Share this post


Link to post
Share on other sites
Xai    1848
All resources which used Handles in windows coding should use Dispose in .NET .. because there is a very limited number of them in the system shared by everyone in many cases ... such as device contexts (instances of Graphics / Forms classes).

Basically, you use .Dispose with almost any heavyweight object who's scope is easy to determine (so basically half the benifit of garbage collection is lost, but at least it will clean up for you EVENTUALLY if you forget).

Share this post


Link to post
Share on other sites
joelmartinez    338
Quote:
Original post by liquiddark
I'm not being intentionally thick, i promise. I've just realized that you're considering the fact that the internal item is unmanaged. This is the case for pretty much any system resource, but I'd be interested to know why components in particular recommend not waiting for garbage collention.

ld
Think about it this way ... if you manually call .dispose on any object that implements IDisposable, your system can go back to using those resources immediately instead of waiting for the GC to clean it up. It's a good idea to *always* call dispose if it's available to you, and you know you won't need the object again.

One simple way of making sure this happens is by using the using construct.

using (myObject)
{
//code that uses myObject
}

Once that code is done executing, it will automatically go out of scope and have its dispose method called as soon as control leaves that block.

Share this post


Link to post
Share on other sites
realJJ    163
Quote:
Original post by liquiddark
I'm not being intentionally thick, i promise. I've just realized that you're considering the fact that the internal item is unmanaged. This is the case for pretty much any system resource, but I'd be interested to know why components in particular recommend not waiting for garbage collention.

ld

In fact, not only components are best disposed than waiting for the finalizer to execute, any classes that implement the IDisposable interface are. There are a few reasons for this but the main one popping out of my head is that disposable objects collected by GC will be added to the finalization thread for execution at a later time (adding overheads and delaying resource collection further).

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