Creating own custom dialogs

  • Thread starter =?ISO-8859-1?Q?Mikko_Nyl=E9n?=
  • Start date
?

=?ISO-8859-1?Q?Mikko_Nyl=E9n?=

After playing a while with System.Windows.Forms, I decided to try
creating my own custom dialogs from scratch. After a bit of googling, I
found out that there's not so much examples of doing own custom dialogs.

So, I'm asking what is the preferred way for creating your own custom
dialogs from scratch? I think it would look a little absurd to inherit
Form for a dialog and the only alternative I was able to find was
inheriting CommonDialog.

The ColorDialog, FontDialog and others inherit from CommonDialog, so I
feel like it would be the preferred way. But the problem is that I can't
find any examples and the documentation of that class in MSDN isn't so
good. For example, when I override RunDialog() and Reset(), what they
should actually do? Is there any examples available?

Thanks in advance.

- Mikko Nylén
 
?

=?ISO-8859-1?Q?Mikko_Nyl=E9n?=

Bob said:
Creating objects based upon Form is the correct way to make custom dialogs.

Okay. Thanks for the reply.

But what for the CommonDialog is then used to? Is it used just for the
existing dialogs in .NET Framework (ColorDialog, FontDialog etc.) or
should it be used in special cases or something?

- Mikko Nylén
 
J

Jason Black [MSFT]

To be a little more specific:

In the Solution Explorer window, right-click on the project you want to add
the dialog to, and select Add->Windows Form from the context menu. You'll
be asked for a name for the new Form's class, and you'll get a blank form
just like when you create a WinForms app, and a new .cs file added to your
project. Add input controls and so forth just like you would for your main
application UI. Add an "OK" button, and make sure that the button's
DialogResult property is set to "OK". Similarly, add a "Cancel" button with
a DialogResult of (surprise surprise) "Cancel".

Now view the code for the dialog's .cs file. In the code editor, add some
public properties for accessing the values in the dialog box's input
controls. You need a "get" accessor on the properties in order to read the
values, but you can also have "set" accessors so that the caller can set
default values for the input controls if necessary. Set accessors are
usually optional, houwver, as in most cases you probably know what a good
default value is ahead of time, and can set that directly through the Forms
Designer.

For example, if your dialog was asking the user for width and height values
for creating a bitmap, you might add something like this:

class myDialog: System.Windows.Forms.Form {
public int Width {
get { return (int)WidthEntry.Value; }
set { WidthEntry.Value = value; }
}
public int Height {
get { return (int)HeightEntry.Value; }
set { HeightEntry.Value = value; }
}
// everything else is what the Forms Designer generated for you
}

In your application, you use it by creating a new "myDialog" object, and
call the ShowDialog() method to display it to the user. After that, treat
it just like you would treat any of the built-in dialogs such as
ColorDialog: check the return value from ShowDialog, get the relevant values
out of the dialog's public fields/properties, and then do whatever you need
to do with them. In this example, maybe something like this:

myDialog d = new myDialog();
if(d.ShowDialog() == DialogResult.OK) {
Bitmap b = new Bitmap(d.Width, d.Height);
}
 
S

Sergey Novogilov

Hi, just FYI:

Dialog Workshop .NET(www.componentage.com) allows to
extend almost all standard dialogs in many ways (add custom controls,
customize dialog size, position and icon, etc.) without writing a line of
code and
without using resources, etc. and without leaving Visual Studio.


Sergey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top