Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 22-Apr-13 23:21pm
Jith125465
Edited 22-Apr-13 23:23pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
jithinmathew at 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 at 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.
jithinmathew at 23-Apr-13 5:20am
   
thanks for your help..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
jithinmathew at 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 at 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.
jithinmathew at 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 at 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 at 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.
jithinmathew at 23-Apr-13 5:39am
   
thanks for your reply ..it was more than a help...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
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:
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:
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):
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);
}
  Permalink  
v2
Comments
Jith125 at 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)

  Print Answers RSS
0 OriginalGriff 490
1 Maciej Los 299
2 BillWoodruff 174
3 /\jmot 170
4 Suraj Sahoo | Coding Passion 150
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100