• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

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.
0
gil_mo
Asked:
gil_mo
  • 6
  • 3
  • 2
  • +4
1 Solution
 
jhanceCommented:
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
 
gil_moAuthor Commented:
Can *you* answer this too?
0
 
jhanceCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
peterchen092700Commented:
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
 
gil_moAuthor Commented:
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
 
gil_moAuthor Commented:
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
 
peterchen092700Commented:
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
 
stephenruanCommented:
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
 
JackThorntonCommented:
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
 
gil_moAuthor Commented:
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
 
romanmCommented:
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
 
romanmCommented:
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
 
DarrinECommented:
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
 
gil_moAuthor Commented:
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
 
romanmCommented:
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
 
DarrinECommented:
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
 
gil_moAuthor Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now