Solved

How to retrieve text from a dialog box?

Posted on 2001-08-13
17
376 Views
Last Modified: 2013-12-03
What is the proper method to retrieve text from a dialog box and to keep it until after the dialog box destroys?

I'm using DialogBoxParam for creating a dialog that receives user input of text. Upon closing the dialog I now want to manipulate the text. Unfortunately, EndDialog can only return a number...  What do I do?

I'll add 50 pts for a sample program.
0
Comment
Question by:gil_mo
  • 6
  • 3
  • 2
  • +4
17 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 6379447
This is REALLY a basic Windows programming concept and if you haven't "got" this, I'm afraid that there is a lot of other important stuff that's not making sense yet either.

Let me suggest that you pickup a copy of Charles Petzold's book, "Programming Windows" published by Microsoft Press.  It is an excellent introductory text on Windows programming topics and the answer to this question as well as many others that you probably have but just don't realize yet are covered as well.
0
 

Author Comment

by:gil_mo
ID: 6379492
Can *you* answer this too?
0
 
LVL 32

Expert Comment

by:jhance
ID: 6379552
The usual way is to send the edit control a EM_GETLINE message:

SendMessage(
  (HWND) hWnd,              // handle to destination window
  EM_GETLINE,               // message to send
  (WPARAM) wParam,          // line number
  (LPARAM) lParam          // line buffer (LPCTSTR)
);

0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6379689
jhance: this exactly doesn't work when the dialog box is closed.

jhance' code needs to be placed in the OK button command handler, and copy the text to some buffer available from outside.

Anyway, even when we give you a sample for this, Petzold might be a godd idea.

Peter
0
 

Author Comment

by:gil_mo
ID: 6379816
Well, Petzold's method involves using a static inside the dialog proc; the static is initialized using lParam in WM_INITDIALOG.

This seemed like a hack to me, and I was hoping there'd be a proper API for this.

I was hoping a EE Expert would come with a neater solution.
0
 

Author Comment

by:gil_mo
ID: 6379933
Well, Petzold's method involves using a static inside the dialog proc; the static is initialized using lParam in WM_INITDIALOG.

This seemed like a hack to me, and I was hoping there'd be a proper API for this.

I was hoping a EE Expert would come with a neater solution.
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6381254
well... use MFC, or ATL, or Borland Builder... ;-)

using a static for this is well acceptable for a small project; for a larger project you should look out for a generic solution.

The only general thing is: before the dialog is closed, you need to transfer the edit box contents to some "permanent memory". Depending on your project this can be a global variable (OK in many cases), or you pass a pointer to a struct to fill as Create Parameter (which comes in as lParam in WM_INITDIALOG)

The C++ frameworks I know work with a map<HWND, CWindow *>, where the CWindow object "represents" the HWND (rather: the Windows "window" thingie), and stores window-specific data.


Peter

0
 
LVL 1

Expert Comment

by:stephenruan
ID: 6383335
You should get the text before function EndDialog is called.

Such as,
void ClickOKButton(HWND hDlg,int Item,LPTSTR text)
{
   GetWindowText(GetDlgItem(hDlg,Item),text,MAXCOUNT);
   EndDialog(hDlg,0);
}
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Expert Comment

by:JackThornton
ID: 6386811
In most cases (whether or not you use MFC), you would encapsulate the dialog from within a class (I'm assuming here you're using C++ and not vanilla C). The canonical thing to do is to transfer the text from edit controls, states of buttons and checkboxes, etc., into member variables when responding to the WM_COMMAND IDOK. The client then queries the class for results, either by:

(a) directly accessing public variables (bad practice)
(b) using accessor methods that return const values of private member variables
(c) for lots of data, passing a structure to a class method that will copy data to the structure.

- jack
0
 

Author Comment

by:gil_mo
ID: 6387280
Jack,

In any of the cases, you would *have* to use a structure / variable / class that is recognized in both scopes: the dialog's callback scope and the dialog creator scope. This differs conceptually from functions like, say, GetDlgItemText, which actually *fill* the buffer you pass it. Instead, I'm required to build some odd mechanism myself for retrieving the text. Ugly :)
0
 
LVL 3

Expert Comment

by:romanm
ID: 6387438
this "odd mechanism" for retrieving data from an object that in a sense no longer exists is the thing top do.
I will elaborate:
Once EndDialog is called, the controls of the dialog and the dialog itself die, the used resources are freed and information that is retrieved normally by GetDlgItemXXXX is lost, all you can do is store that information internally or globaly.
as for that comment about using "structure / variable / class that is recognized in both scopes", as far as I recall every function you would ever write that does not use the basic data types will need to "recognize" the "structure / variable / class" that it is using.

anyway I see no real difference from using GetDlgItemText then using Jack's reply, concenptually that is.

- R.
0
 
LVL 3

Expert Comment

by:romanm
ID: 6387527
now for the real answer:
manualy create a modaless dialog box, and set it to be modal, internaly,
now when pressing "ok" don't destroy the dialog just hide it, and return control to the parent.
now you can use GetDlgItemText etc.
use EndDialog when destroying the dialog object.
0
 
LVL 2

Expert Comment

by:DarrinE
ID: 6387546
hmmmmm .... this is a boring topic which seems to be attracting a lot of attention for nothing

As I understand it the question is how to retain the text after the dialog has been closed

Not so hard ... tell us what you're programming in ... C++ MFC or C

If you're using MFC or C++ declare a global CString or String variable in your class - if you're using plain C declare in in your primary header file as a char variable either as a pointer (char *) or as a character string char mystr[255]

Now to give you an example in all three might be confusing, so you'll need to tell me what your programming in and I (we) will get an example for you

Hope this helps

DarrinE

0
 

Author Comment

by:gil_mo
ID: 6387609
romanm,
"use EndDialog when destroying the dialog object" - but when DO I destroy it?

DarrinE,
You are absolutely right about the subject. And, as I said before and just as Petzold advises, you suggest using a global which will be recognized in both scopes. Again: ugly, especially if the data you need is more than just text.

I'm currently using Petzold's method. I see no point in continuing this otherwise.
0
 
LVL 3

Expert Comment

by:romanm
ID: 6387645
mydialog* ptr = new mydialog;

if( IDOK == ptr->MyDoModal() )
{
   GetDlgItemText...
}

delete ptr;

// EndDialog should be inside the destructor of mydialog.

this method is more trouble then its worth.
0
 
LVL 2

Expert Comment

by:DarrinE
ID: 6387665
NO - if the data you need is more than text - there is always a way - and besides - you said "text from a dialog..."

I see you avoid answering the question of what you program in .... well what is it ?

>>> I'm using DialogBoxParam for creating a dialog that
>>> receives user input of text. Upon closing the dialog
>>> I now want to manipulate the text. Unfortunately,
>>> EndDialog can only return a number...  What do I do?

This to me suggests "C" ?

If this is right then your question was answered sometime ago

thanks for wasting my time (as well as that of others)

DarrinE




0
 

Author Comment

by:gil_mo
ID: 6387726
DarrinE,
I use Win32 SDK, probably you refer to it as "C" .

Sorry for waisting your time.

The best answer until now was "Do what Petzold tells you" by jhance. Jhance will therefore, in spite of the fact that he didn't give me a direct answer, will receive the points.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now