Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Following is the code for inserting a character 'A' on the edit box. But when I click the button it inserting some garbage values..... I dunno why it is..., I am suspecting some problem with format function.......

Please help me on this......


C#
void CEditBox_1Dlg::OnBnClickedButton1()
{
    // TODO: Add your control notification handler code here



    CString getvalue='\0';
    usCurPos=0;
    char carrArray[50],csTemp[50];
    char *carrptr ;
    carrptr =carrArray;
    char a = 'A';
    //UpdateData(true);
    m_CntrlDisplay.SetFocus();

    m_TxtSamplingFrequency.Insert(usCurPos,a);

    //((CEdit*)GetDlgItem(IDC_EDIT1))->GetWindowTextW(getvalue);
    m_CntrlDisplay.GetWindowText(getvalue);

    strcpy(carrArray,(LPCSTR) (CStringA)getvalue);

    carrptr += usCurPos;


    sprintf(csTemp,"%s",carrptr);

    for(short sLoop = 0;sLoop<strlen((char*)csTemp);sLoop++)

    {

        *carrptr =csTemp[sLoop]; carrptr++;

    }

    *carrptr = '\0';

    getvalue.Format(L"%s",carrArray);



    //((CEdit*)GetDlgItem(IDC_EDIT1))->SetWindowTextW(getvalue);

    m_CntrlDisplay.SetWindowText(getvalue);

    UpdateData(false);
    usCurPos++;
    m_CntrlDisplay.SetSel(usCurPos,usCurPos);

    //m_CntrlDisplay=atoi(carrSamplFreq);
    m_TxtSamplingFrequency = (getvalue);
    UpdateData(false);

}
Posted
Comments
Richard MacCutchan 23-Dec-13 4:25am    
Step through this code with your debugger and you will soon see what is going on. I suspect there is a lot of redundancy in there.

1 solution

Most of your code seems to be useless and the Unicode/char conversion parts are wrong.

The wrong parts that lead to the garbage values are:
// Wrong:
strcpy(carrArray,(LPCSTR) (CStringA)getvalue);
// Correct:
//  Create CString object passing a pointer to perform the conversion
strcpy(carrArray,CStringA(getvalue.GetString()).GetString());

// Wrong: 
getvalue.Format(L"%s",carrArray);
// Correct: 
//  Tell the format function that the string is of type char* using the 'h' prefix
getvalue.Format(L"%hs",carrArray);

There is no need to copy the string multiple times to insert the character at increasing positions. Just use your m_TxtSamplingFrequency variable:
// Get the text
m_CntrlDisplay.GetWindowText(m_TxtSamplingFrequency);
// Check position; append if beyond the end
if (usCurPos > m_TxtSamplingFrequency.GetLength())
    usCurPos = m_TxtSamplingFrequency.GetLength();
// Insert character
m_TxtSamplingFrequency.Insert(usCurPos, _T('A'));
// Set text
m_CntrlDisplay.SetWindowText(m_TxtSamplingFrequency);
// Set selection behind inserted char
usCurPos++;
m_CntrlDisplay.SetSel(usCurPos,usCurPos);
 
Share this answer
 
Comments
Baakki 23-Dec-13 6:07am    
I already tried with placing a character using Getlength , but the problem with that is.., when you want to place a character in between the word on edit box, we can't .., it will be placed at last position as per the Length value....., So we need find exact caret position to place a character......
Jochen Arndt 23-Dec-13 6:13am    
The GetLength() call from my answer is just to ensure that the position is valid. If you want to insert at the current caret position, call GetSel() to determine the position. It is up to you to decide what to do when nothing is selected or multiple chracters are selected.
Baakki 23-Dec-13 6:34am    
Yes I got it ............ Thank u so much dude :)
Jochen Arndt 24-Dec-13 4:07am    
You should not ask new questions using 'Solutions'. Most users will not answer and some will mark them as 'Not an answer' and it will be deleted when 5 users do so.
So you should create a new question and delete your 'Solution' using the read X link. Then I will have a look on it.
Baakki 24-Dec-13 6:53am    
Its okay I got the answer.... Thank u for ur kind response.

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