Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using a single line Edit control ,in which I got a text in it, I need to change only a particular character in the text and the other characters need to remain as it is.What will be the best approach for doing it..Can somebody please come up with some suggestions .Thanks in Advance !!
Posted
Updated 22-Apr-13 22:23pm
v2

I assume that you are working Win32 level in C.

Send an EM_SETSEL message to the edit control and set thereby the selection to the character or characters you would like to change. See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb761661%28v=vs.85%29.aspx[^]

Then send an EM_REPLACESEL message to do the replacement. See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb761633%28v=vs.85%29.aspx[^]
 
Share this answer
 
Comments
Jith125 23-Apr-13 5:00am    
Thanks for your response !I have used the EM_SETSEL message for the selection but it isn't working...(SendMessage (hEdit,EM_SETSEL,j, -1);)j where j is the postion of the char which need to be changed ....Is this the way to do it??
nv3 23-Apr-13 5:11am    
What are trying to do with the -1 as specification for the end position. Looks to me that you should use j+1 instead, if you want to change just one character.
Jith125 23-Apr-13 5:20am    
thanks for your help..
It's difficult to give an naswer to your question, but you should check the documentation[^] to see which methods or messages would best suit your purpose.
 
Share this answer
 
Comments
Jith125 23-Apr-13 5:04am    
Thanks for your response !! I am really new to the windows programming !!! i am using a super classed single line edit control , I am using a windows CE device which is connected to a keypad..My task is that i need to write the software for the keypad in such a way that it works like a mobile keypad. so when i press a particular time let say the KEY 2 for the first time it should show the char "2" , second time "A",n so on !!
Richard MacCutchan 23-Apr-13 5:11am    
Then you need to be handling the input characters and changing them as the keys are pressed. you could start by capturing the full text in a string constant, then every time a key is pressed you modify the string and re-display it in the text box. Either way, you will need to keep track of which character position you are working on.
Jith125 23-Apr-13 5:18am    
yeah thats the logic which I was trying to implement .I have used the EM_SETSEL message for the selection but it isn't working...(SendMessage (hEdit,EM_SETSEL,j, -1);)j where j is the position of the char which need to be changed ....Is this the way to do it???
Richard MacCutchan 23-Apr-13 5:26am    
I don't think the -1 value is correct there. According to the documentation the LPARAM value should be the ending position of the selection, which I think should be j + 1. You need to do some more experimentation to check all possibilities.
Richard MacCutchan 23-Apr-13 5:32am    
"but it's not working" does not tell us anything. You need to use your debugger to step through your code to see exactly what is happening, as we cannot guess what results you are seeing, and how they are different from what you expect.
Edit controls does not provide functions to manipulate single characters. You have two options:

1. Get the complete text, modify it, and store it back
Use GetWindowText() to retrieve the text, modify it, and put it back using SetWindowText(). When using a CString, a single character can be replaced by the SetAt() member function:
C++
void CMyEdit::SetChar1(int nPos, TCHAR c)
{
    if (nPos >= 0 && nPos < GetWindowTextLength())
    {
        CString s;
        GetWindowText(s);
        s.SetAt(nPos, c);
        SetWindowText(s.GetString());
    }
}

This method is appropiate for controls with small amount of texts and when multiple operations should be performed, e.g. replacing each occurrence of a character in the string:
C++
void CMyEdit::ReplaceChar(TCHAR cOld, TCHAR cNew)
{
    CString s;
    GetWindowText(s);
    if (s.Replace(cOld, cNew))
        SetWindowText(s.GetString());
}


2. Use string replacement with edit control
Use SetSel() passing the index of the character to be replaced as start and one more as end point followed by a call to ReplaceSel() using a string created from the new character:
C++
void CMyEdit::SetChar2(int nPos, TCHAR c)
{
    if (nPos >= 0 && nPos < GetWindowTextLength())
    {
        TCHAR lpszReplace[2] = {c, _T('\0') };
        // Save the selection that might be set by the user
        DWORD dwSaveSel = GetSel();
        SetSel(nPos, nPos+1, 1);
        ReplaceSel(lpszReplace);
        SetSel(dwSaveSel, 1);
    }
}


[UPDATE]
From the comments it becomes clear that you need a keypad emulation. This can be done implementing a WM_CHAR handler for the edit control and some member variables storing the last pressed char code, event time, and number of repeated presses. Inside the handler check if the char code is identical to the last one and the last event was inside the repeat time span. If so, change the char code passed to the default handler by the new one according to the repeated presses and increment the repeat counter (which may wrap back to null when toggling through all codes):
C++
void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    UINT nNewChar = nChar;
    DWORD dwTime = ::GetTickCount();
    if (nChar == m_nLastChar && dwTime - m_dwLastTime < TIMEOUT)
    {
        // Set nNewChar here according to nChar and m_nRepeatCount
//      nNewChar = pCharTable[nChar][m_nRepeatCount];
        m_nRepeatCount++;
        // When cycled through all combinations, clear m_nRepeatCount

        // Select last char; so it will be replaced by the new one
        // Without tracking the position, 
        //  just use GetWindowTextLength() - 1 as position
        SetSel(m_nPos, m_nPos+1, 1);
    }
    else
    {
        m_nLastChar = nChar;
        m_nRepeatCount = 0;
    }
    m_dwLastTime = dwTime;
    CEdit::OnChar(nNewChar, nRepCnt, nFlags);
}
 
Share this answer
 
v2
Comments
Jith125 23-Apr-13 6:45am    
thanks a lot for your help....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900