C ++ Builder XE 2 - 如何在TEdit控件中显示变量数据?

时间:2012-06-25 11:30:39

标签: c++ c++builder

我希望你能在这里帮助我。我在过去3周内所做的是尝试显示3个变量的内容(1个char变量,其中包含一个' - '或'+'符号,另外两个包含温度测量值的整数变量)我是什么我试图做id来在TEdit控件中显示这些变量的内容,如:+ 26.3。就这么简单。

我尝试了几个没有成功的功能,让我告诉你:

(其中temp_txtBox_Cond是TEdit控件的名称)

temp_txtBox_Cond->Text=(L"%c %i. %i",temp_sign,temp_temp_int,temp_temp_dec,test_buf,n);

temp_txtBox_Cond->Text.printf(L"%c %i. %i",temp_sign,temp_temp_int,temp_temp_dec);

sprintf(test_buf,"%c %i.%i",temp_sign,temp_temp_int,temp_temp_dec);
temp_txtBox_Cond ->Text.sprintf(L"%c %i.%i",temp_sign,temp_temp_int,temp_temp_dec,test_buf);

temp_txtBox_Cond->Text=("%c %i. %i",temp_sign,temp_temp_int,temp_temp_dec);

temp_txtBox_Cond->Text = AnsiString(temp_sign,temp_temp_int,temp_temp_dec);

temp_txtBox_Cond->Text = (AnsiString(temp_sign)+AnsiString(temp_temp_int)+AnsiString    (temp_temp_dec));

String s(temp_sign);
String d(temp_temp_dec);
String i(temp_temp_int);

temp_txtBox_Cond->Text = s+" "+i+"."+d;

没有成功。这非常令人沮丧。

请大家需要你的帮助。谢谢你提前。


这有点奇怪,因为大多数上述功能我得到的是0+0.0hq3Pˆ15PPhå(这听起来像是内存问题)。让我告诉你:

使用:

temp_txtBox_Cond->Text = (L"%c %i.%i",temp_sign,temp_temp_int,temp_temp_dec,test_buf);

我得到:hq3Pˆ15PPhå

使用:

temp_txtBox_Cond->Text.printf(L"%c %i. %i",temp_sign,temp_temp_int,temp_temp_dec);

我得到Nada文本框是空白的

使用:

sprintf(test_buf,"%d. %d",temp_temp_int,temp_temp_dec);
temp_txtBox_Cond->Text =(test_buf,"%c %d. %d",temp_sign,temp_temp_int,temp_temp_dec);`

我得到0。

使用:

temp_txtBox_Cond->Text =("%c %d. %d",temp_sign,temp_temp_int,temp_temp_dec);`

我也是0。

和使用:

temp_txtBox_Cond->Text = AnsiString(temp_sign,temp_temp_int,temp_temp_dec): I got +0.0

这些是错误。

嗨,雷米,

当然没问题。这是我用来获取temp_sig,temp_temp_int和temp_temp_dec的代码:

 WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{    
ikor_msg_id = ikor_id_bld (app_priority, zone, dest_UCP, 0x20, 0x03);
wait_for_msg(h, 0x5200, 500, data);
if (data[0] == 1)
{
temp_sign = '+';
}
else if (data[0] == 0)
{
temp_sign = '-';
}
else
{
temp_sign = '?';
}

temp_temp_int = data[1];
temp_temp_int <<= 8;
temp_temp_int += data[2];
temp_temp_dec = temp_temp_int;
temp_temp_int /= 10;
temp_temp_dec -= (temp_temp_int * 10);
}

在这里我试图显示数据:

void __fastcall TForm1::temp_txtBox_CondChange(TObject *Sender )
{
unsigned long ikor_msg_id = 0;
long ikor_id_bld( long p_temp, long z_temp, long d_temp, int m_temp, int s_temp);
int wait_for_msg(CANHANDLE handle,int id,int timeout,unsigned char *data);
CANHANDLE h;
unsigned char data [8];

/***Here is where i tried all the attempts that I posted ****/

}

我在标题中声明了变量:

#ifndef __Variables_Cond__
#define __Variables_Cond__

#ifdef __cplusplus
extern "C" {
#endif



unsigned int temp_temp_int;
unsigned int temp_temp_dec;
unsigned char temp_sign;


#ifdef __cplusplus

}     #ENDIF

#endif    

2 个答案:

答案 0 :(得分:2)

我建议如下:

    TCHAR temp_buf[256];
    wsprintf(temp_buf, _T("%c %d.%d"), temp_sign, temp_temp_int, temp_temp_dec);

    temp_txtBox_Cond->Text = temp_buf;

答案 1 :(得分:0)

您得到的结果是错误的,因为您尝试的尝试是错误的,并且大部分都源于同一根问题 - 您滥用,运算符。在表达式而不是参数列表中,,运算符执行双方的运算,运算符的结果是右手运算的结果。您在表达式中将多个,操作链接在一起,并期望它们能够导致他们真正没有的操作。

  

temp_txtBox_Cond-&gt; Text =(L“%c%i。%i”,temp_sign,temp_temp_int,temp_temp_dec,test_buf);

这实际上与以下内容相同:

L"%c %i.%i"; // no-op
temp_sign; // no-op
temp_temp_int; // no-op
temp_temp_dec; // no-op
temp_txtBox_Cond->Text = test_buf;     

您得到hq3Pˆ15PPhå,因为test_buf的内容尚未事先初始化。

  

temp_txtBox_Cond-&gt; Text.printf(L“%c%i。%i”,temp_sign,temp_temp_int,temp_temp_dec);

这实际上与以下内容相同:

String temp = temp_txtBox_Cond->Text;
L"%c %i. %i"; // no-op
temp_sign; // no-op
temp_temp_int; // no-op
temp_temp_dec; // no-op
temp.printf(temp_temp_dec);     

您得到一个空白的结果,因为您根本没有为Text属性分配任何内容。您正在阅读Text属性,然后在其返回的临时printf()上调用String,但之后您没有将String分配回Text之后的财产。

  

sprintf(test_buf,“%d。%d”,temp_temp_int,temp_temp_dec);
  temp_txtBox_Cond-&gt; Text =(test_buf,“%c%d。%d”,temp_sign,temp_temp_int,temp_temp_dec);

这实际上与以下内容相同:

sprintf(test_buf, "%d. %d", temp_temp_int, temp_temp_dec);     
test_buf; // no-op
"%c %d. %d"; // no-op
temp_sign; // no-op
temp_temp_int; // no-op
temp_txtBox_Cond->Text = temp_temp_dec;

"0"中得到Text,因为temp_temp_dec的开头为0(尽管您认为在调试器中看到了这一点)。

  

temp_txtBox_Cond-&gt; Text =(“%c%d。%d”,temp_sign,temp_temp_int,temp_temp_dec);`

这实际上与以下内容相同:

"%c %d. %d"; // no-op
temp_sign; // no-op
temp_temp_int; // no-op
temp_txtBox_Cond->Text = temp_temp_dec;

与上述结果相同。

  

temp_txtBox_Cond-&gt; Text = AnsiString(temp_sign,temp_temp_int,temp_temp_dec)

这实际上与以下内容相同:

temp_sign; // no-op
temp_temp_int; // no-op
temp_txtBox_Cond->Text = AnsiString(temp_temp_dec);

这将再次导致"0"。您无法使用该代码在"+0.0"中获得Text

如上所述,请改用其中之一:

Char temp_sign = L'+'; // note: upper 'C'har = "%c"
int temp_temp_int = 26;
int temp_temp_dec = 3;
temp_txtBox_Cond->Text = String().sprintf(L"%c %i.%i", temp_sign, temp_temp_int, temp_temp_dec); 

char temp_sign = '+'; // note: lower 'c'har = "%hc"
int temp_temp_int = 26;
int temp_temp_dec = 3;
temp_txtBox_Cond->Text = String().sprintf(L"%hc %i.%i", temp_sign, temp_temp_int, temp_temp_dec); 

Char temp_sign = L'+'; // or lower-case 'c'har, doesn't matter
int temp_temp_int = 26;
int temp_temp_dec = 3;
temp_txtBox_Cond->Text = Format(L"%s %i.%i", ARRAY_OF_CONST(( temp_sign, temp_temp_int, temp_temp_dec )) ); 

Char temp_sign = L'+'; // note: upper 'C'har = "%C"
int temp_temp_int = 26;
int temp_temp_dec = 3;
char test_buf[24];
sprintf(test_buf, "%C %i.%i", temp_sign, temp_temp_int, temp_temp_dec); 
temp_txtBox_Cond->Text = test_buf; 

char temp_sign = '+'; // note: lower 'c'har = "%c"
int temp_temp_int = 26;
int temp_temp_dec = 3;
char test_buf[24];
sprintf(test_buf, "%c %i.%i", temp_sign, temp_temp_int, temp_temp_dec); 
temp_txtBox_Cond->Text = test_buf; 

Char temp_sign = L'+'; // or lower 'c'char, doesn't matter
int temp_temp_int = 26;
int temp_temp_dec = 3;
temp_txtBox_Cond->Text = String(temp_sign) + L" " + String(temp_temp_int) + L"." + String(temp_temp_dec);