65.9K
CodeProject is changing. Read more.
Home

CInputBox 1.0

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.82/5 (45 votes)

Dec 1, 2001

CPOL

2 min read

viewsIcon

132433

downloadIcon

1388

A CFrameWnd derived class that provides functionality similar to the VB InputBox function. You don't need a dialog resource to use the class!

What the class does for you?

I've seen people often asking whether there is something in C++ like the InputBox function in Visual Basic. I guess the easy way is to create your own dialog class. But I thought an easier way would be to write a class that is not dependent on a resource. CInputBox can be used without having to create a dialog resource. It allows you to set the title of the input-box, the prompt and also the default text.

There are just two public functions in addition to the constructor, of course. The constructor needs to be passed a CWnd*. Mostly you can pass this as that parameter.

The main member function is ShowInputBox which is declared as follows:

int ShowInputBox(CString,CString,CString);

The first CString is the prompt to show. The second CString is the title text for the input-box and the third CString is the default text. All three may be null strings. Though I don't see that happening often for the first two parameters.

Always, always call CloseBox after you have shown the input-box. You can only show the box once. If you want to show it again you MUST call CloseBox and then start all over again from object-construction.

How to use the class?

  • First create your CInputBox object
    CInputBox *m_inputbox = new CInputBox(this);
  • Now call the ShowInputBox function passing the prompt text, the title text and the default text
    int rv = m_inputbox->ShowInputBox("Enter your age","Age box","");
  • Find out whether they cancelled the box or whether they clicked OK
    if(rv==IDCANCEL)
        MessageBox("","You cancelled");
  • If they clicked OK you can retrieve the entered text using the InputText public member variable
    if(rv==IDOK)
        MessageBox(m_inputbox->InputText,"the text you entered");
  • Now call CloseBox to avoid memory leaks
    m_inputbox->CloseBox();

Some points that you should keep in mind

  • You can only call ShowInputBox once per object-life-time. Means you simply do it in three steps. Create the object, show the box, close the object. If you need to show an input-box more than once, then for each time you need to follow all three steps.
  • For some strange and as of now unknown reason, on one occasion, my sample program that used this class didn't terminate and was left hanging in memory. I haven't traced the problem yet. But please do let me know if you face a similar problem

Conclusion

This is just version 1. I guess it needs a lot of enhancements like the ability to call ShowInputBox repeatedly and the ability to specify the size and location of the window. I'll do that when I have time.