Welcome to Dream.In.Code
Become a C# Expert!

Join 150,423 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,101 people online right now. Registration is fast and FREE... Join Now!




MessageBox to appear on top of main form

 
Reply to this topicStart new topic

MessageBox to appear on top of main form

killnine
19 Mar, 2008 - 05:23 AM
Post #1

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 114



Thanked: 5 times
My Contributions
Any thoughts on how to keep a messagebox on top of the main form?

I have a form designed but messages from my thrown exceptions appear below the main form, which is annoying becuase users dont see them. Right now I just throw an exception and in my "catch" clause I say:

CODE

MessageBox.Show("Blah blah","Error",MessageBoxButtons.OK, MessageBoxIcon.Error);


nothing special. But it always appears on bottom...

One idea I had was to use the IWin32WinOwner argument, but I dont really know how to reference my main window, so any help would be appreciated!
User is offlineProfile CardPM
+Quote Post

zakary
RE: MessageBox To Appear On Top Of Main Form
19 Mar, 2008 - 07:21 AM
Post #2

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 420



Thanked: 8 times
Dream Kudos: 175
My Contributions
not sure what you mean. are you looking to display the messagebox in a form or is it a Textbox displaying your message on the form.
User is offlineProfile CardPM
+Quote Post

killnine
RE: MessageBox To Appear On Top Of Main Form
19 Mar, 2008 - 08:03 AM
Post #3

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 114



Thanked: 5 times
My Contributions
QUOTE(zakary @ 19 Mar, 2008 - 08:21 AM) *

not sure what you mean. are you looking to display the messagebox in a form or is it a Textbox displaying your message on the form.



Lets assume I have a form where all my controls are and there is a textbox within that form. Lets also assume this textbox should only have integers put into it (no alpha characters, or numbers with a decimal).

If a user enters some invalid value, I throw an exception (myToolException, for instance).

When I catch the exception, I just show a messagebox that says something like "Invalid textbox input" or something like that.

However, right now, these messageboxes appear BELOW the main form.

Because its an exception, it pretty much halts all user input until the user selects "OK" on the messagebox. But because its below the main form, people dont often see it (and then they get frustrated).

But the thing is, it doesn't happen EVERY time, only sometimes. Sometimes the error pops up in front of the main form, but other times it doesn't.

Thoughts?
User is offlineProfile CardPM
+Quote Post

zakary
RE: MessageBox To Appear On Top Of Main Form
19 Mar, 2008 - 08:48 AM
Post #4

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 420



Thanked: 8 times
Dream Kudos: 175
My Contributions
and you are using System.Windows.Forms.MessageBox.Show(""); to display your Exception message
User is offlineProfile CardPM
+Quote Post

killnine
RE: MessageBox To Appear On Top Of Main Form
19 Mar, 2008 - 10:25 AM
Post #5

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 114



Thanked: 5 times
My Contributions
QUOTE(zakary @ 19 Mar, 2008 - 09:48 AM) *

and you are using System.Windows.Forms.MessageBox.Show(""); to display your Exception message


Yup.
User is offlineProfile CardPM
+Quote Post

zakary
RE: MessageBox To Appear On Top Of Main Form
19 Mar, 2008 - 11:22 AM
Post #6

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 420



Thanked: 8 times
Dream Kudos: 175
My Contributions
That is strange can you post your code so we can see what is going on.
User is offlineProfile CardPM
+Quote Post

killnine
RE: MessageBox To Appear On Top Of Main Form
20 Mar, 2008 - 05:20 AM
Post #7

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 114



Thanked: 5 times
My Contributions
QUOTE(zakary @ 19 Mar, 2008 - 12:22 PM) *

That is strange can you post your code so we can see what is going on.



I dont think my employer is too keen on me pasting any code right now.

I dont know if this fixed it, but I now use the following argument list

CODE

MessageBox.Show(this,"Some error text","Error Title",MessageBoxButtons.OK,MessageBoxIcon.Error);



User is offlineProfile CardPM
+Quote Post

zakary
RE: MessageBox To Appear On Top Of Main Form
20 Mar, 2008 - 06:25 AM
Post #8

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 420



Thanked: 8 times
Dream Kudos: 175
My Contributions
try this

try
{

}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "Error Title",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: MessageBox To Appear On Top Of Main Form
21 Mar, 2008 - 07:05 AM
Post #9

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 4,032



Thanked: 38 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Could you post a screenshot of the Messagebox appearing behind the form?

This could possibly happen if your form's TopMost Property is set to True.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:37PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month