Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Edit Box content (MFC)

Status
Not open for further replies.

kartoffel

Programmer
Jul 24, 2002
15
CA

Hi

I inserted an edit box in a Dialog, using the Ressource editor. The user can write some text in the edit box and I want to access the content.

From within my Dialog CPP file (say Dialog.cpp) I wrote the following to access each line of the edit box:

for()
{
...
GetDlgItem(IDC_MY_EDITBOX)->GetLine();
...
}

When I compile, I get an error saying that GetLine is not a member of CWnd. How come my edit box is not a CEdit object?

Thanks
 
> How come my edit box is not a CEdit object?

it *is*, but GetDlgItem() always returns a pointer to the base class CWnd regardless of the dialog item type.

A quick&dirty way to do it is to cast the pointer:

((CEdit*)GetDlgItem(IDC_MY_EDITBOX))->GetLine();
 
Thanks for the tip cra.

But now it seems I have another problem. When I try to retrieve each line of the edit box, I get empty strings!

Here is my code (inspired from an MSDN example)

=======================================
CString strCurrentLine, strLine;
CEdit* pmyEdit = (CEdit*)GetDlgItem(IDC_MY_EDITBOX);
int i, nLineCount = pmyEdit->GetLineCount();

// Dump every line of text of the edit control.
for (i=0;i < nLineCount;i++)
{
pmyEdit->GetLine(i,
strCurrentLine.GetBuffer(pmyEdit->LineLength(i)));
strCurrentLine.ReleaseBuffer();

strLine.Format(TEXT(&quot;line %d: '%s'\r\n&quot;), i, strCurrentLine.GetBuffer(0));

MessageBox(strLine, _T(&quot;FileName&quot;), MB_ICONINFORMATION);
}
==============================
In the message box, I always get something like:

Line 0: ''
Line 1: '', etc.
when there actually is some text in the edit box.

What am I doing wrong here?

Thanks
 
I think the problem is the LineLength() function. It takes an index to a character, not a line.

Try pmyEdit->LineLength(pmyEdit->LineIndex(i)).

I think the names of these two functions are confusing & misleading :-(

Hope that helped...
 

hmmm... still doesn't work

I really dont know what the problem is...

Thanks anyway for your help!
 
Very strange, it works for me... However there was another bug in it: ReleaseBuffer() needs the string length or a previously larger string will remain in the buffer.

I use:

int len = pmyEdit->LineLength(pmyEdit->LineIndex(i));
pmyEdit->GetLine(i,strCurrentLine.GetBuffer(len));
strCurrentLine.ReleaseBuffer(len);

and it works perfectly!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top