CEdit SetSel and Scrolling
-
Hello everyone, this is a sounds-so-simple gray hair maker. Please help out: A single line edit control on my dialog contains So far nothing complicated. Now: On an event I would like to add some long text at the cursor position, give the edit box the focus, select the whole text of the edit control ***AND** I would like to see the beginning of my long selected text ... CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus(); The problem is that the SetSel scrolls the edit box to the end of the selection. Is there a way to scroll to the beginning without loosing the selection? If you know the length of the selection, you may do both .SetSel(Start, End) or .SetSel(End, Start) but always the caret and the scroll will be at the end of the selection. WM_HELP Thomas
-
Hello everyone, this is a sounds-so-simple gray hair maker. Please help out: A single line edit control on my dialog contains So far nothing complicated. Now: On an event I would like to add some long text at the cursor position, give the edit box the focus, select the whole text of the edit control ***AND** I would like to see the beginning of my long selected text ... CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus(); The problem is that the SetSel scrolls the edit box to the end of the selection. Is there a way to scroll to the beginning without loosing the selection? If you know the length of the selection, you may do both .SetSel(Start, End) or .SetSel(End, Start) but always the caret and the scroll will be at the end of the selection. WM_HELP Thomas
Thomas Blenkers wrote:
pEdit->SetSel(0, -1); pEdit->SetFocus();
SetSel
has one more parameter which is a default one. Does that help you? Look up the docs.
Nibu thomas Software Developer Faqs by Michael dunn
-
Thomas Blenkers wrote:
pEdit->SetSel(0, -1); pEdit->SetFocus();
SetSel
has one more parameter which is a default one. Does that help you? Look up the docs.
Nibu thomas Software Developer Faqs by Michael dunn
Thanks, Nibu, yes I do read docs before asking ;) The third parameter is called bNoScroll and according to the docs it should scroll or not the caret into view. But this is not the point, since I tried both versions. Both get the same result: the edit box is completely selected with the end portion of the text being shown. My guess is that the problem lies in the line of code before the SetSel: CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus(); The ReplaceSel will put the caret to the end of the edit windows and scrolls the text to the end. Already I have played around with the edit box scrolling functions to no avail. As I said, its making you gray hairs! Regards Thomas
-
Thanks, Nibu, yes I do read docs before asking ;) The third parameter is called bNoScroll and according to the docs it should scroll or not the caret into view. But this is not the point, since I tried both versions. Both get the same result: the edit box is completely selected with the end portion of the text being shown. My guess is that the problem lies in the line of code before the SetSel: CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus(); The ReplaceSel will put the caret to the end of the edit windows and scrolls the text to the end. Already I have played around with the edit box scrolling functions to no avail. As I said, its making you gray hairs! Regards Thomas
Thomas Blenkers wrote:
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus();
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);
pEdit->SetSel(0, -1);
pEdit->ReplaceSel("This is some long and rather useless test text");
CString csText;
pEdit->GetWindowText(csText);
pEdit->SetWindowText(csText);
pEdit->SetFocus();This should help although it doesn't look good. :) Unless another suitable option is found you can use this. :)
Nibu thomas Software Developer Faqs by Michael dunn
-
Thomas Blenkers wrote:
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1); pEdit->ReplaceSel("This is some long and rather useless test text"); pEdit->SetSel(0, -1); pEdit->SetFocus();
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);
pEdit->SetSel(0, -1);
pEdit->ReplaceSel("This is some long and rather useless test text");
CString csText;
pEdit->GetWindowText(csText);
pEdit->SetWindowText(csText);
pEdit->SetFocus();This should help although it doesn't look good. :) Unless another suitable option is found you can use this. :)
Nibu thomas Software Developer Faqs by Michael dunn
Nibu, once again, thanks and great work. This will work as long as indeed I choose to select all of the edit box. In my real application I'm doing some autocompletion so I want a first part of the edit text not selected while my recenty added text is selected. Maybe its my fault that my given lines of code were too much siplified to express this contraint. If you have further ideas, I would be really happy. Regards Thomas
-
Nibu, once again, thanks and great work. This will work as long as indeed I choose to select all of the edit box. In my real application I'm doing some autocompletion so I want a first part of the edit text not selected while my recenty added text is selected. Maybe its my fault that my given lines of code were too much siplified to express this contraint. If you have further ideas, I would be really happy. Regards Thomas
Thomas Blenkers wrote:
In my real application I'm doing some autocompletion so I want a first part of the edit text not selected while my recenty added text is selected.
So no problem. You first do whatever replacement or other stuff that you wish to do. Then do
GetWindowText
andSetWindowText
. I know that's not a natural solution. But here are some other functions that I would like to bring to your notice...SetCaretPos
-->CWnd
PosFromChar
-->CEdit
I tried these two but the caret didn't move. But you can try. Maybe you can make it work. :)
Nibu thomas Software Developer Programming Tips[^]