Click here to Skip to main content
11,496,146 members (1,363 online)
Click here to Skip to main content

CInputBox 1.0

, 30 Nov 2001 CPOL 105.1K 1.3K 25
A CFrameWnd derived class that provides functionality similar to the VB InputBox function. You don't need a dialog resource to use the class!
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nish Nishant

United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

Comments and Discussions

 
QuestionA couple of slight mods Pin
Doug Joseph13-Oct-11 16:01
memberDoug Joseph13-Oct-11 16:01 
AnswerRe: A couple of slight mods Pin
Nishant Sivakumar14-Oct-11 6:19
mvpNishant Sivakumar14-Oct-11 6:19 
GeneralMy vote of 5 Pin
Doug Joseph13-Oct-11 15:43
memberDoug Joseph13-Oct-11 15:43 
Thanks so much Nish. This is just what I was looking for!
GeneralRe: My vote of 5 Pin
Nishant Sivakumar14-Oct-11 6:19
mvpNishant Sivakumar14-Oct-11 6:19 
QuestionWhere are the explainations? Pin
Umander12-Mar-08 5:51
memberUmander12-Mar-08 5:51 
GeneralOne bug, one improvement (IMHO) Pin
mssg6-Mar-04 2:04
membermssg6-Mar-04 2:04 
Generalthread keeps running in some cases Pin
nullspace200022-Nov-03 10:42
membernullspace200022-Nov-03 10:42 
AnswerRe: thread keeps running in some cases Pin
Gorelik Sasha6-Jan-06 2:31
memberGorelik Sasha6-Jan-06 2:31 
GeneralThanks a lot Pin
Melescher13-May-03 0:29
memberMelescher13-May-03 0:29 
QuestionWhy oh Why? Pin
James Curran3-Dec-01 9:43
memberJames Curran3-Dec-01 9:43 
AnswerRe: Why oh Why? [modified] Pin
Nish [BusterBoy]3-Dec-01 17:11
memberNish [BusterBoy]3-Dec-01 17:11 
AnswerRe: Why oh Why? Pin
Chris Maunder4-Jul-02 19:14
adminChris Maunder4-Jul-02 19:14 
QuestionWhy? Pin
Code1-Dec-01 11:28
memberCode1-Dec-01 11:28 
AnswerRe: Why? Pin
Uwe Keim1-Dec-01 13:20
memberUwe Keim1-Dec-01 13:20 
GeneralRe: Why? Pin
brandonl_webguy3-Dec-01 5:35
memberbrandonl_webguy3-Dec-01 5:35 
GeneralRe: Why? Pin
Jignesh I. Patel3-Dec-01 12:08
memberJignesh I. Patel3-Dec-01 12:08 
GeneralRe: Why? Pin
Zac Howland4-Jun-02 7:23
memberZac Howland4-Jun-02 7:23 
GeneralRe: Why? Pin
JohnAtTopcon7-May-03 0:47
memberJohnAtTopcon7-May-03 0:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 1 Dec 2001
Article Copyright 2001 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid