473,508 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Form, that work like MessageBox?

Bry
Is it possible to produce a Windows Dialog form that can be used like
MessageBox.ShowDialog(), but returns a custom object type

e.g.

myObject = MyForm.ShowDialog();

rather than

// This method doesn't return the object either and ShowDialog can't be
// overriden to make it behave like that
MyForm frmMyForm = new MyForm();
frmMyForm.ShowDialog();

Can anyone suggest any soloution to this?

Thanks.

Dec 14 '05 #1
4 9673
Bry,

MessageBox does not have a ShowDialog method. It has a Show method
which is not related to ShowDialog at all. You can create your own class
with a static method and have it return whatever you want.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Bry" <br*********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Is it possible to produce a Windows Dialog form that can be used like
MessageBox.ShowDialog(), but returns a custom object type

e.g.

myObject = MyForm.ShowDialog();

rather than

// This method doesn't return the object either and ShowDialog can't be
// overriden to make it behave like that
MyForm frmMyForm = new MyForm();
frmMyForm.ShowDialog();

Can anyone suggest any soloution to this?

Thanks.

Dec 14 '05 #2
The MessageBox class is not a windows form or a dialog. It's just a regular
class that happens to have a static Show method. This method instantiates a
new form object, displays it, returns the result.

So you can very easily write the same thing.

"Bry" <br*********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Is it possible to produce a Windows Dialog form that can be used like
MessageBox.ShowDialog(), but returns a custom object type

e.g.

myObject = MyForm.ShowDialog();

rather than

// This method doesn't return the object either and ShowDialog can't be
// overriden to make it behave like that
MyForm frmMyForm = new MyForm();
frmMyForm.ShowDialog();

Can anyone suggest any soloution to this?

Thanks.

Dec 14 '05 #3
If you want it to work exactly that way, where the static method you
call returns an object, the only way I can think of to do it is to use
a class inbetween the main form and the dialog box.

Code for main form:

object test = TestClass.Show();
MessageBox.Show(test.ToString());

-----------------------------------------------------------------
class TestClass
{
private object whatever;
private static TestClass instance;
protected TestClass() {}
public static object Show()
{
instance = new TestClass();
TestForm tf = new TestForm();
tf.FormClosing += new FormClosingEventHandler(tf_FormClosing);
tf.ShowDialog(); //must use show dialog so the return waits for
the form to close
return instance.whatever;
}

private static void tf_FormClosing(object sender,
FormClosingEventArgs e)
{
instance.whatever = ((TestForm)sender).whatever2;
}
}

-------------------------------
TestForm

The TestForm in this example would just have to have an instance
variable called whatever2. Hopefully this gives you a general idea of
how you could do it. You have to use .ShowDialog() on the test form so
the return line waits for the form to close to do the return.

Dec 14 '05 #4
Bry
Many thanks to all for the replies. I was under the impression the
MessageBox class was a windows form class.

Now that I've got that cleared up, I've managed to do what I was trying
to do.

Thanks.

Dec 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
284
by: .pd. | last post by:
Hello, If I have a static control and 2 forms, each of which references the static. When I return from the 2nd and go back to the first, the reference to static control appears to have been...
1
20940
by: Kevin Carter | last post by:
Hi there...hoping somebody can help me out because I am almost ready to pull out my hair. My problem is as follows: In a method in my main form (non MDI app), I instantiate and .Show another...
4
1774
by: Sebastian Bargmann | last post by:
Hi, I've run into a weird problem with the form designer. I have three classes: a messagebox class and two forms (Base and Derived which is derived from Base): (note: only relevant code...
8
4918
by: CJack | last post by:
hy, I have an mdi application, i create a child form and I want to know when a button is pressed while that child form is loaded. I have this code: private void frmTestBaby_KeyUp(object sender,...
15
5729
by: Sharon | last post by:
I’m trying to build a generic Publisher-Subscriber that will work over the net, so I’m using the Remoting. I wish that the subscriber user will be notify about the messages sent by the...
5
8889
by: Jon E. Scott | last post by:
I'm a little confused with "static" methods and how to access other unstatic methods. I'm a little new to C#. I'm testing a callback routine within a DLL and the callback function returns a...
6
3470
by: Goran Djuranovic | last post by:
Hi all, I have a VB.NET windows application that uses MDI form. When I try to delete a datagrid row from one of the MDI children forms, I use a MessageBox YesNo confirmation, which, after confirmed,...
1
2019
by: Gary Brown | last post by:
Hi, I have a dialog box that must behave as MessageBox does with regard to the TopMost property. It must be TopMost if and only if the calling form is TopMost. A static method creates the...
0
971
by: Cristo4 | last post by:
I have a class that is able to detect a global key press. However, I don't seem to be able to create a working event that notifies the Form1 of a global keypress. The function "HookCallback" is...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7391
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.