当我点击“A”按钮时,垃圾值会插入到texbox中.... [英] Garbage values are inserting in a texbox , when I click the button 'A'....

查看:58
本文介绍了当我点击“A”按钮时,垃圾值会插入到texbox中....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是在编辑框中插入字符A的代码。但当我点击按钮它插入一些垃圾值.....我不知道它为什么...,我怀疑格式功能的一些问题.......



请帮我这个......





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......


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);

}

推荐答案

你的大部分代码似乎都没用,而且Unicode / char转换部分是错误的。



导致垃圾值的错误部分是:

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);



无需多次复制字符串以在增加的位置插入字符。只需使用 m_TxtSamplingFrequency 变量:


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);


这篇关于当我点击“A”按钮时,垃圾值会插入到texbox中....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆