簡體   English   中英

如何在MFC的“編輯控件”中更改下一個字符的放置位置?

[英]How to change the position where next character will be place in the Edit Control from MFC?

我有一段代碼可以刪除字符串的最后一個字符,然后將“編輯控件”中的文本設置為該新字符串。 問題在於,此后,接下來要鍵入的字符的位置會發生變化。 例:

編輯控制箱:[12345 | ](斜線是放置下一個鍵入字符的位置)

做完代碼后

編輯控制框:[| 12345](位置現在移到前面的1之前)

如何將位置再次移至字符串的末尾? 我的代碼:

    CString str1 = ""; //Temporary CString
    eb1->GetWindowText(str1); //Store text from Edit Control to the CString
    string strCheck1 =  str1.GetString(); //Place the CString into a regular string
    int s1 = strCheck1.length() -1; //Get index of last character and new size
    bool check1 = true; //Boolean variable for the checking
    //Get if character is valid
    if((strCheck1[s1] <= '0' || strCheck1[s1] >='9') && strCheck1[s1] != '.') {check1 =       false;}
    //If is invalid I erase last character and put it back intact into the Edit Control
    if(check1 == false) {strCheck1.erase(s1,1); eb1->SetWindowTextA(strCheck1.c_str());}

您是否嘗試過編輯控件的SetSel()操作?

// get the initial text length
int nLength = edit.GetWindowTextLength();
// put the selection at the end of text
edit.SetSel(nLength, nLength);

您可以使用CEdit::SetSel() (我假設您正在使用CEdit )。 只要讓選擇的開始和結束都為字符串的結尾,您就應該能夠將光標移到那里。 可以在http://msdn.microsoft.com/zh-cn/library/w9kftda4(v=vs.80).aspx中找到詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM