954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MessageBox in C#

Hiii Alll

I need to display a message to the user after clicking the button control. How to do this in c#.net? I could not find Msgbox() option as we hav in vb.net.

can any one help me

sonakrish
Light Poster
34 posts since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

Hi
You use

MessageBox.Show("This is a message");
frank@furnes.nu
Newbie Poster
12 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

Hi.
If I do so Am gettin this error
Error The name 'MessageBox' does not exist in the current context
Do we hav to create a separate function or to use any namespace for this.

sonakrish
Light Poster
34 posts since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

I guess the Call is done within a Form?
The MessageBox is included in the namespace System.Windows.Forms
Do you have this in your using at the top?
Or have you tried to write

System.Windows.Forms.MessageBox.Show("This is a message");
frank@furnes.nu
Newbie Poster
12 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

MessageBox is only available in Window Applicaitons. In ASP.NET, you cannot use the MessageBox class or MsgBox function like you can in a Window Applicaitons, but you can emulate this functionality by streaming out a javascript alert instruction like this:

Response.Write(@"<script language='javascript'>alert('This is my Message')</script>");
Aneesh_Argent
Junior Poster
104 posts since Dec 2008
Reputation Points: 16
Solved Threads: 18
 

Correct:)
I assumed this was a windows application:)

frank@furnes.nu
Newbie Poster
12 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

In ASP.NET with code Behind VB I have used msgbox().
It worked well.
But in C# :( am gettin into trouble

sonakrish
Light Poster
34 posts since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

Can you send a sample of the code

frank@furnes.nu
Newbie Poster
12 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

In web application we can use MsgBox() (with code behind VB).

sonakrish
Light Poster
34 posts since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

Okai..
This is my sample code for Palindrome checking.
Got a string in textbox 1.

TextBox2.Text = StrReverse(TextBox1.Text)
            If TextBox1.Text = TextBox2.Text Then
                MsgBox("String is palindrome", MsgBoxStyle.OkOnly)
            Else
                MsgBox("Not a palindrome", MsgBoxStyle.OkOnly)
            End If


This code is done in Asp.Net webapplication with VB code behind.
Can u say how to use message box with C# code behind:?:

sonakrish
Light Poster
34 posts since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

So this is a Web application in c#.net
Well then you cannot use the Messagebox. This function / method
is used within an windows application
As mention earlier , you can solve this by using Javascript or ms.Ajax (Popup /modalpopup)

frank@furnes.nu
Newbie Poster
12 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

Hmmm Fine....
In C# we have to use script for messageBox.Okay let me do.
Tnx...

sonakrish
Light Poster
34 posts since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

Select the function name "MsgBox", right click and from the menu select Go to Defenition and post the fuction defenition here. I suppose its a user defined function

Aneesh_Argent
Junior Poster
104 posts since Dec 2008
Reputation Points: 16
Solved Threads: 18
 

Hi again

I yust wonder who this could be done..
Do as i told over.. in button click method

System.Windows.Forms.MessageBox.Show("This is a message");


then select: project -> add reference
Then scroll down to System.Windows.Forms and select this.
Press ok

frank@furnes.nu
Newbie Poster
12 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

Hi,

I dont think what Frank is saying will work when deployed (But i will gladly be proven wrong as i would like to be able to use the messagebox functionality in an asp.net app). The messagebox will throw an exception because you can not show a modal form on a clients machine when it is created server side. The only real option avaliable is to Use javascript or Ajax as someone has mentioned earlier in this thread.

Jamie Shedley
Newbie Poster
5 posts since Feb 2009
Reputation Points: 10
Solved Threads: 2
 

I must say i have never tried this before:)
So Yes, the best right way is by javascript..

frank@furnes.nu
Newbie Poster
12 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "SCRIPT", string.Format("alert('Hello World')"), true);

sanchu666
Newbie Poster
1 post since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You