Hi, I'm trying to develop a program that will, if a condition is not met, display an error message without having to close the background window.

If I use Application::Start It returns an error saying that the only one window can be open a once blah blah blah.

I'm looking for a way to make the program display an error whenever you click a button with a certan condition.

Thanks, TailsTheFox

Like a MessageBox() with an exclamation icon, or?

Are you using solely C++. What platform? Does it have to be a portable solution? Can you use anything else?

If you are using Windows, i.e. Win32 API, then you can use the following function (as suggested before):

MessageBox(NULL,"An error has occurred because you didn't use this program correctly!", "Error!", MB_OK);

If you need something more fancy (like different buttons or display), then you can make your own form with whatever GUI tool you are using. Any GUI tool, like MFC, Qt or wxWidgets, has a way to create the form and usually, you have a function like "show" or "execute" that will show the window, and usually, that function has a parameter which is the handle of the form from which you are creating this sub-form (usually called the "Parent"). This will generate a new form on top of the current form, you can also disable the current form until the sub-form (error message) has been clicked away or dealt with. You should be able to find this in your particular GUI tool (in the help files).

I like your idea of

MessageBox(NULL,"An error has occurred because you didn't use this program correctly!", "Error!", MB_OK);

But I just wanted to ask one question;
Does it require any header to run, and if so, what header?

Upon closer examination of my program , I learned that I could also use Form::ShowDialog If you wanted to make a message appear, But I don't know how to use it! The program says Form:ShowDialog(Form2()); id a "Violation" of something. How should I use this?

First, the header for the MessageBox is just the "windows.h" header. That is for most straight Win32 API calls, like MessageBox and many others of the like.

For the second solution you just posted. Normally, when you design a form (i.e. place buttons and menus and stuff on the form), it will generate a class with the name you gave to the form that you created. Say I design a form that I call "MainForm", then the GUI tool (MFC or Qt or whatever) will generate a header and a cpp file that declares and implements this MainForm class. Say I add a button to it and define an event that is triggered from clicking the button. Then, the header might look something like this (the actual class and function names might differ in your GUI tool):

//... some includes and stuff before ...

class MainForm : public TForm {
  // ... some stuff
  public:
    TButton Button1;

    void OnButton1Click();

    //... some more stuff ...
};

Now, what you can do is design another form for your dialog with some message label on it and some caption or whatever else you want. Let's call it MyDialogForm. Again, as before, when you design this form, the GUI tool will generate a header and cpp for this form to work. If you include this header from the MainForm.h and add the cpp to the build of your main application, you should be able to, for example, make a data member of MainForm of type MyDialogForm (you might have to go and add this data member directly by editing MainForm.h, unless your GUI tool has a feature to add it through its form editor). Then, MainForm.h might look like this:

//... some includes and stuff before ...
#include "MyDialogForm.h" //include the header for your dialog form.

class MainForm : public TForm {
  // ... some stuff
  public:
    TButton Button1;
    MyDialogForm ErrorDialog; //add a data member of type MyDialogForm.

    void OnButton1Click();

    //... some more stuff ...
};

Now, all you have to do is use this "ShowDialog" function within the proper event handlers or whatever. For sake of example, you can trigger the dialog when clicking on the button, which would make the definition of "OnButton1Click" in MainForm.cpp look something like this:

void MainForm::OnButton1Click() {
  this->ShowDialog(ErrorDialog);
};

This is the essence of it, it might differ slightly in your particular GUI tool. Please specify which one you are using if you want more precise instructions.

If you are wondering how you would show an error message from somewhere outside a Form class (which doesn't have the ShowDialog function), then it can be a bit more tricky (if you want to not use MessageBox). However, in general, functions that are called by the GUI (i.e. Forms) of your applications should not pop-up error dialogs, but they should throw exceptions which would in turn be caught by the GUI Form (event handler or otherwise) that originally called those low-level functions and then pop-up the appropriate error dialog. It is generally good to do this to separate the "communicating with the user" task from the "crushing the numbers" task (more portable, more flexible, more modular, etc.).

I'm okay with the MessageBox stuff, but I really need to figure out the Form::Show

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.