What is a good way to code a messagebox in C#, like the AfxMessageBox in used in Visal C++?

Also, is there a good way to write information out to a log file?

Recommended Answers

All 13 Replies

i can help with the message box question,
the snippet below is used to create a dialog box :

string message = "blah";
string caption = "blah";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;

result = MessageBox.Show(message, caption, buttons, icon);

then you just write event handlers for the yes/no buttons.
e.g. for yes:

if (result == System.Windows.Forms.DialogResult.Yes)
{
this.Close();
}

you may have guessed this dialog box can be used for a "are you sure you want to quit" scenario. hope this helps

For logging have a look at http://www.nlog-project.org.
You'll find a nice Tutorial there also. I use it in all my Projects and it is really easy to use.

Hope this is useful for you!

i can help with the message box question,
the snippet below is used to create a dialog box :

string message = "blah";
string caption = "blah";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;

result = MessageBox.Show(message, caption, buttons, icon);

There must be some sort of "using ..."
declaration missing.

I got a compile error:
error CS0246: The type or namespace name 'MessageBoxButtons' could not be found (are you missing a using directive or an assembly reference?)

>The type or namespace name 'MessageBoxButtons' could not be found
Are you getting the same error for MessageBox? They're in the same namespace: System.Windows.Forms.

ok which namespaces do you have in your code?
make sure you have
using System.Windows.Forms;

lol @ narue

using System.Windows.Forms;
fixed the problem.

I got this compile message:
error CS0103: The name 'MessageBox' does not exist in the current context

But not now.
Muchas Gracias

no probs

im having a problem with the MessageBox's yes and no option

either button i clicked, it does not close right away, but will only do so on the second click. so if i had put any code within the yes or no click, it gets executed twice because of this

by the way, my messagebox appears when i click a cell inside the datagridview object because i placed a delete icon there in 1 column

I'd debug the DialogResult to see what or how many you're getting back. It could also be that the messagebox is being displayed twice when the cell click event is fired. If that is the case, either debug the click event or add a bool to only allow the messagebox to be displayed once.

i got it. the event handler was added twice. still ... it should have overwritten it if i added it twice... not have 2 event handlers

Replace

this.Close();

With

Application.Exit();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.