how does MessageBox work?

  • Thread starter Thread starter Saso Zagoranski
  • Start date Start date
S

Saso Zagoranski

Hi!

I'm trying to make my own MessageBox... What I would like to know is, how
the MessageBox class is implemented?
I could have something like:
new MyMessageBox().ShowDialog();

but I would like the solution used by MessageBox, where you call the static
Show() method, which
returns a DialogResult value...

a few tips on how to create that would be really appreciated!

Thanks,
saso
 
Public class MyMessageBox
{
public static DialogResult Show(....)
{
//code here
}
}
 
Hi Michael!

I know that part... the question is what goes into the Show method?
do I create an instance of MyMessageBox in there?
 
No, the idea is to not create and instance of MyMessageBox. You could just call MessageBox.Show if you like, or you could use the
MessageBox API call, or you could show a form that looks like a messagebox.
 
do you mean something like this:

class MBForm : Form
{
...
}

class MyMessageBox
{
public static DialogResult Show(...)
{
MBForm form = new MBForm;
form.Show();
}
}

If I do it like this I still wouldn't get any input from MBForm... and it
wouldn't make any difference if I didn't even use the MyMessageBox...
In a way, I would like a form, which I could call with a static show()
method... and would return a selected value,
when I press something on that form...

I just looked at MSDN Library and read that MessageBox derives from
System.Object... it could be that the MessageBox class isn't as simple as it
looks... :)

saso


Michael Culley said:
No, the idea is to not create and instance of MyMessageBox. You could just
call MessageBox.Show if you like, or you could use the
 
Everything derives from System.Object, if something derives directly from system.object then it is possibly simple. Have a look at
how many levels there are between Form and object. In the case of the messagebox class, it would simply call the windows api
functions, so would be fairly simple.

If you want to show a form as a messagebox then you should use this code
MBForm form = new MBForm;
return form.ShowDialog();

The ShowDialog functon returns a DialogResult enum. In your form you can give the OK button the DialogResult property a value of
DialogResult.OK and the Cancel button a value of DialogResult.Cancel.
 
Try
http://msdn.microsoft.com/library/d...lrfSystemWindowsFormsMessageBoxClassTopic.asp

protected void button1_Click(object sender, System.EventArgs e) {
if(textBox1.Text == "") {
MessageBox.Show("You must enter a name.", "Name Entry Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else {
// Code to act on the data entered would go here.
}
}

You may need to change to MessageBoxButtons.OKCancel or YesNoCancel

Alternatively:
http://msdn.microsoft.com/library/d...ystemwindowsformsmessageboxclassshowtopic.asp


private void validateUserEntry2()
{

// Checks the value of the text.

if(serverName.Text.Length == 0)
{

// Initializes the variables to pass to the MessageBox.Show
method.

string message = "You did not enter a server name. Cancel this
operation?";
string caption = "No Server Name Specified";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;

// Displays the MessageBox.

result = MessageBox.Show(this, message, caption, buttons,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);

if(result == DialogResult.Yes)
{

// Closes the parent form.

this.Close();

}

}

}
 
Thanks Michael!

I get it now...

One other question... what if the form doesn't just have simple buttons and
some text... and I would like to
return a string (or something else) instead of DialogResult?

Michael Culley said:
Everything derives from System.Object, if something derives directly from
system.object then it is possibly simple. Have a look at
how many levels there are between Form and object. In the case of the
messagebox class, it would simply call the windows api
functions, so would be fairly simple.

If you want to show a form as a messagebox then you should use this code


The ShowDialog functon returns a DialogResult enum. In your form you can
give the OK button the DialogResult property a value of
 
Store the string in a module level variable on the form and return it via a property. Then access that property in your Show
function:
Console.WriteLine(form.MyReturnedString);
 
Back
Top