Click here to Skip to main content
Click here to Skip to main content

CInputBox 1.0

, 30 Nov 2001
Rate this:
Please Sign up or sign in to vote.
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.

License

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

About the Author

Nish Sivakumar

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
 
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com.
 
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
 
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
 
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Comments and Discussions

 
QuestionA couple of slight mods PinmemberDoug Joseph13-Oct-11 15:01 
AnswerRe: A couple of slight mods PinmvpNishant Sivakumar14-Oct-11 5:19 
GeneralMy vote of 5 PinmemberDoug Joseph13-Oct-11 14:43 
GeneralRe: My vote of 5 PinmvpNishant Sivakumar14-Oct-11 5:19 
QuestionWhere are the explainations? PinmemberUmander12-Mar-08 4:51 
GeneralOne bug, one improvement (IMHO) Pinmembermssg6-Mar-04 1:04 
Generalthread keeps running in some cases Pinmembernullspace200022-Nov-03 9:42 
AnswerRe: thread keeps running in some cases PinmemberGorelik Sasha6-Jan-06 1:31 
GeneralThanks a lot PinmemberMelescher12-May-03 23:29 
I'm using my own ListCtrl with only one .cpp and .h File on disk for many many Projects. Now i added some function with simple user input of text in this class for searching text in rows without! adding a dialog resource to any Project. Only two file-links to your CInputBox class in every Projct are needed.Smile | :)
 
Thanks
 

 

QuestionWhy oh Why? PinmemberJames Curran3-Dec-01 8:43 
AnswerRe: Why oh Why? [modified] PinmemberNish [BusterBoy]3-Dec-01 16:11 
AnswerRe: Why oh Why? PinadminChris Maunder4-Jul-02 18:14 
QuestionWhy? PinmemberCode1-Dec-01 10:28 
AnswerRe: Why? PinmemberUwe Keim1-Dec-01 12:20 
GeneralRe: Why? Pinmemberbrandonl_webguy3-Dec-01 4:35 
GeneralRe: Why? PinmemberJignesh I. Patel3-Dec-01 11:08 
GeneralRe: Why? PinmemberZac Howland4-Jun-02 6:23 
GeneralRe: Why? PinmemberJohnAtTopcon6-May-03 23: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 | Mobile
Web01 | 2.8.140721.1 | Last Updated 1 Dec 2001
Article Copyright 2001 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid