473,394 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 9663
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.