Link to home
Start Free TrialLog in
Avatar of gil_mo
gil_mo

asked on

How to retrieve text from a dialog box?

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.
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gil_mo
gil_mo

ASKER

Can *you* answer this too?
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)
);

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
Avatar of gil_mo

ASKER

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.
Avatar of gil_mo

ASKER

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.
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

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);
}
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
Avatar of gil_mo

ASKER

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 :)
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.
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.
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

Avatar of gil_mo

ASKER

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.
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.
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




Avatar of gil_mo

ASKER

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.