Solved

How to retrieve text from a dialog box?

Posted on 2001-08-13
17
380 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

863 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

28 Experts available now in Live!

Get 1:1 Help Now