How do I show dialog boxes in Csharp?

C

COHENMARVIN

I was told on this forum that a good way to display dialog boxes is
with the following code:
using (MyDialog myDialog = new MyDialog())
{
if (myDialog.ShowDialog(this) == DialogResult.OK)
{
someValue = myDialog.SomeValue;
}
}

Now I notice that when I create a Visual C# project, it comes with a
Form called "Form1", and a
program class that has a routine such as:

static void Main()
{
Application.Run(new Form1());
}

Is this "Form1" a modal dialog or a modeless form?

Also, if it is a dialog, then where can I fit in the code sample with
the "using" statement. The dialog is called within "Application Run",
and so there seems to be no room for a "using" statement.

Finally, if the dialog has a textbox called "TextBox1", I notice that
the Textbox is declared as being "private". Do I have to declare it
as "public" to be able to use code such as was shown above
(someValue= MyDialog.TextBox1.Text;)?
(what I mean is that I am getting a value from the dialog after the
dialog has been closed by the user, but before the 'using' statement
goes out of scope.)
Thanks
-- Marvin
 
G

Greyhound

Form1 is inherited from System.Windows.Forms.Form and is a non-modal form.
It is the default for all Windows Forms applications. When are you desiring
to show a dialog box?
 
I

Ignacio Machin ( .NET/ C# MVP )

I was told on this forum that a good way to display dialog boxes is
with the following code:
 using (MyDialog myDialog = new MyDialog())
 {
         if (myDialog.ShowDialog(this) == DialogResult.OK)
         {
                 someValue = myDialog.SomeValue;
         }
 }

Now I notice that when I create a Visual C# project, it comes with a
Form called "Form1", and a
program class that has a routine such as:

static void Main()
{
    Application.Run(new Form1());

}

Is this "Form1" a modal dialog or a modeless form?

Form1 is the starting form, without it there is no "windows program",
when it's closed the program ends (most of the times at least) , so
you could say it's modal
But modal/no modal is refered when a windows (like the main window
mentioned above) open a new window, IF this new windows does not allow
the access to the parent, it's modal.
Also, if it is a dialog, then where can I fit in the code sample with
the "using" statement.  The dialog is called within "Application Run",
and so there seems to be no room for a "using" statement.

You do not need to use the using construction, the modalness of the
form is based in what I explained above.
Finally, if the dialog has a textbox called "TextBox1", I notice that
the Textbox is declared as being "private".  Do I have to declare it
as "public" to be able to use code such as was shown above
(someValue= MyDialog.TextBox1.Text;)?

Yes, but you could also declare a public property that return
textbox1.Text;
 
M

Michael A. Covington

There are to things you can do with a form, .Show() to make it become
visible and start handling events, and .ShowDialog(), which makes it become
visible and modal.

Typically:

- Your application creates a form called Form1 and performs .Show() on it.

- If you want to put up another form as a dialog box then:
- Design it and give it a name (I'll call it Form2);
- When you're ready to show it, do Form2.ShowDialog();
- Form2 must handle events for the buttons on it, etc., and its OK button
must do a "this.Close()" to make Form2 go away.

At least that's what I recall without actually looking up the docs.
 
B

Bob Powell [MVP]

Hi.
The program class and the main function are the bootstrapper for the
application. The main function usually instantiates and runs the main form
using Application.Run.

A second optional form of an application is the dialog app in which the
modal form representing the main application window is run using the
Form.ShowDialog method. Dialog based applications or modal forms take all of
the application focus and exit only when the ShowDialog method, which runs
it's own message pump, is stopped by setting the DialogResult property of
the form.

To show a dialog from an ordinary application you will use the code that you
cite in your original post, possibly in response to an event such as a
button click.

Its a very bad idea to change the declaration of controls such as TextBox to
public. The side effect of this is to enable external objects full acccess
to the control which means that the textbox may be deleted or changed in
really wierd ways. The accepted method is to create a property on the form
that then modifies the internal Text property of the text box.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
B

Bob Powell [MVP]

Further to this discussion see the code after my signature.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

using System;

using System.Collections.Generic;

using System.Windows.Forms;

using System.Drawing;

namespace WindowsApplication9

{

static class Program

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

//this shows a dialog...

Dialog d = new Dialog(); // this is a dialog app

d.ShowDialog();



Application.Run(new Form1()); // this is an ordinary app

}

class Dialog : Form

{

public Dialog()

{

Button b=new Button();

b.Location=new Point(10,10);

b.Size=new Size(150,25);

b.Text="Close";

b.Click+=new EventHandler(b_Click);

this.Controls.Add(b);

}

void b_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.OK;

}

}

class Form1 : Form

{

public Form1()

{

Button b=new Button();

b.Location=new Point(10,10);

b.Size=new Size(150,25);

b.Text="Close";

b.Click+=new EventHandler(b_Click);

this.Controls.Add(b);

}

void b_Click(object sender, EventArgs e)

{

this.Close();

}

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)

{

if (MessageBox.Show("Close?", "?", MessageBoxButtons.YesNo) ==
DialogResult.No)

e.Cancel = true;

}

protected override void OnClosed(EventArgs e)

{

//maybe do something?

base.OnClosed(e);

}

}

}

}
 

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