Click here to Skip to main content
15,064,806 members
Articles / Desktop Programming / MFC
Posted 14 Feb 2003


16 bookmarked

VB Like InputBox for MFC

Rate me:
Please Sign up or sign in to vote.
3.27/5 (11 votes)
14 Feb 2003CPOL2 min read
InputBox for MFC without Dialog Resource.


I have been developing softwares both in VB & VC, and like many of you, wanted to get user input using something like VB's InputBox. For this, I searched at CodeProject and found some solutions (thanx to them). The problem was: all of them used some dialog resources to solve the problem that ultimately increases the size of the application.


I am presenting the InputBox by sub-classing the MessageBox provided by MFC. Last week, I found the "Do not ask again" MessageBoxes by Nicolas Bonamy; that discusses the sub-classing of MessageBox to create a CheckBox. I used the same idea to present an EditBox to get the user input by popping the MessageBox. Thanx Nic for the idea.

Sub-classing MessageBox

This relies on MFC hooking through the AfxHookWindowCreate() function. With this, we redirect the MessageBox messages to us and therefore are able to catch (once more) WM_INITDIALOG. Here, we resize the window, add the EditBox and so on...

The big advantage is that our class is in a way the MessageBox itself with an added functionality of Edit Control; and hence the events from Edit control are handled in our class.

Using the code

To use the CInputBox, simply:

  • Add the InpuBox.h & InputBox.cpp to your project
  • Instantiate the CInputBox object
  • Call the Show method with a string.
  • When the call to Show() returns, the provided string will contain the user input.
CInputBox theInputBox(this);
CString strUserInput;
theInputBox.Show("Please Enter Some Text Here...", strUserInput);



I think you can further enhance the class by setting some methods to implement your specific needs like:

  • Only Numeric set ES_NUMBER
  • Special characters by catching EN_CHANGE
  • More than one Edits for different inputs.


My sincere thanx to:

  • Nicolas Bonamy: whose code inspired me for doing such an implementation.
  • My friend Sadiq: who always helps me whenever I am stuck in problems.
  • My brother Ubaid: who helped me in writing & discussing the code.
  • Most of all, my "teacher" MSDN: you know most of us are nothing without it.


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


About the Author

Muneeb R. Baig
Product Manager
Pakistan Pakistan
Having 20 years of experience developing innovative software targeting multiple industries including, EDA, Insurance, Supply Chain, Support Centers, CRM, Brain Storming (Mind Mapping); Muneeb is a developer turned project manager, who thinks he can still code and finds some time to keep himself abreast on latest trends and best practices—to implement them while managing the projects making the clients happy by ensuring timely deliverables of their expectations.

Comments and Discussions

Questioncould the CEdit control be possible add to CListCtrl ? Pin
foxsir28-Dec-16 14:24
Memberfoxsir28-Dec-16 14:24 
QuestionLicensing terms Pin
Scott Bartine7-Dec-11 4:05
MemberScott Bartine7-Dec-11 4:05 
AnswerRe: Licensing terms Pin
Muneeb R. Baig7-Dec-11 5:16
MemberMuneeb R. Baig7-Dec-11 5:16 
QuestionHow to use in C Pin
NajamUlHaque17-Feb-03 1:54
MemberNajamUlHaque17-Feb-03 1:54 
AnswerRe: How to use in C Pin
Nish Nishant17-Feb-03 8:53
sitebuilderNish Nishant17-Feb-03 8:53 
GeneralNot on Win 98 Pin
PJ Arends16-Feb-03 6:41
professionalPJ Arends16-Feb-03 6:41 

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

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