How to move cursor to end of an edit control

I am trying to move the cursor to the end of text in an edit control programmatically.

The user clicks a button outside of the edit control, (which causes the control to lose focus) and upon that action I am calling the edit control's SetFocus() function which places the cursor at the beginning of the control. How do I get it to place the cursor at the end of any text in the control?

Who is Participating?
CEdit* pEdit = ((CEdit*)GetDlgItem(IDC_YOUR_EDIT));
CString txt;

Good Luck!
You might want to take a look at the following (works for a single line edit box - for multi-line, you'll have to tweak it):

CEdit m_yourEditControl;

int nLen = m_yourEditControl.LineLength();
m_yourEditControl.SetSel(nLen, nLen, TRUE);

[It should work - but I haven't actually compiled this - I'm writing it from memory]
I think I found something in an MFC or SDK sample program that appeared to do what you are asking but I do not rmemmber for sure where. I think it was in a sample communications program that was maintaining a log of activity like Hyperterminal does. If you ask me to I will see if I can find it and if I find it I will post which sample it is as an answer.
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

rascalAuthor Commented:
Thank you. bbousquet I would also like to give you points but there does not appear to be a way???? Please let me know how and I will give you 100 pts as well.

Thanks to all.
Vinayak KumbarSr Program ManagerCommented:

Well, I doubt on that. But I have not checked it. If it works it is fine. Otherwise do the following thing.

m_ctrlEdit.SetSel(m_strEdit.GetLength()-2, m_strEdit.GetLength()-1, FALSE);

Here m_ctrlEdit is the member variable for the edit control of CEdit type.
Here m_strEdit is the member variable for the edit control of CString type.

Hope that solves ur prob.

Just create a new question in this category and label it 'points for bbousquet'. i'll answer it and you'll be able to give me the points too.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.