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!

Help with a small newbie app

Status
Not open for further replies.

MikeAbrams

Programmer
Jul 7, 2005
4
US
Hello all. I am brand new to VC++ but I am learning as I go.
My current project is to output user input from an Edit Box to another Edit Box or Static Text Box. My problems are three-fold at the moment:

1. First string is sent fine, subsequent strings overwrite the existing text rather than appending on a new line.

2. Lack of carriage return feature in output box.

3. Previous string entered by user remains in Input Edit Box.


My code is as follows and any help would be appreciated:


void CHostDlg::OnSend()
{
UpdateData(TRUE);

CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);
CEdit* pEditShow = (CEdit*)GetDlgItem(IDC_EDIT2);

pEdit->GetWindowText(m_EDIT1);
pEditShow->SetWindowText(m_EDIT1);
}
 
1. Of course, SetWindowText() replaces all previous contents. If you want to append a text, read previous contents, modify it in memory then send back to edit box.
2. Use multi-line edit control.
3. Probably misprint: write SetWindowText(m_EDIT2) - see your snippet.

Addition: GetDlgItem() returns CWnd*, not CEdit*. Don't cast returned value to CEdit* (see MSDN for details). Don't worry: CWnd class has GetWindowText/SetWindowText member functions.
 
So.. By read previous contents are you saying to incorporate the entire contents of the Edit Box into a single variable, add the new submitted content onto the variable and re-write it back out to the Edit Box? Is there no other way to avoid have an insanely large variable down the line?

Multi-Line Edit Control will automatically provide carriage returns?

As to the additional comment I'm a little confused. What I have in place works as specified, is there an advantage to changing it to what you've listed?
 
1. May be there is another way, but I don't know it (alas;).
2. No, use explicit "\r\n" pair in a string (not only '\n' as in text mode stream i/o). It breaks line (visually)automatically, of course. Try it with simplest MFC dialog-based application.
3. It's a rather delicate moment in MFC. Using CWnd* as CEdit* works in some circumstances, but fails in another ones (with mysterious app crash etc). Better read MSDN articles about MFC edit controls.
 
Apropos, don't forget: standard MLE control accepts Ctr-Enter as a line break (insert "\r\n" in its contents). Simple Enter reserved as a default action for outer form.
 
Hehe, most of this is completely new to me, as I am just starting out with VC++, but I am experimenting and trying to work in what you are suggesting.

So far I've changed IDC_EDIT2 to a multi-line edit box, but no matter how I try to append a "\r\n" onto the end of the m_EDIT1 string its not line feeding in the output box. I have the output box as read-only, but I don't think that would effect this negatively.
 
Well, I have tested it before my post above.
Get your append/replace code, please...
 
Here is the current incarnation of the OnSend portion based on your suggestions:


void CHostDlg::OnSend()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString strCR;
strCR = "\r\n";

m_EDIT1 = m_EDIT1 + strCR;

CWnd* pEdit = (CWnd*)GetDlgItem(IDC_EDIT1);
CWnd* pEditShow = (CWnd*)GetDlgItem(IDC_EDIT2);

pEdit->GetWindowText(m_EDIT1);
pEditShow->SetWindowText(m_EDIT1);
}
 
I'm sorry, I don't understand...
Where is text appending? See:

1. You append CR/LF to m_EDIT1 string (I don't know what's its contents, and you?).
2. You totally overwrite this string by GetWindowText(EDIT1) call.
3. You send this (EDIT1) contents to EDIT1 (again?!) contents (why?).

No any appending, no EDIT2 contents at all...
Sorry, but it's so simple code...

As far as I know, you want:
1. Read edit1
2. read edit2
3. concatenate edit2 + "\r\n" + edit1
4. sent the result to edit2

Well, do it!
 
Sorry.. It was 3am when I wrote that last reply. Turns out what was killing it was using m_EDIT1 = m_EDIT2 + CRLF + m_EDIT1 and then writing m_EDIT1 back out to the screen. Once I created a new CString m_EDIT3 and used it to append the two Edit boxes it worked out fine. Sorry to be a pain.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top