【问题标题】:C++ Builder XE 2 -- How to display variable data in a TEdit control?C++ Builder XE 2 -- 如何在 TEdit 控件中显示变量数据?
【发布时间】:2012-06-25 11:30:38
【问题描述】:

我希望你们能在这里帮助我,伙计们。我在过去 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    

【问题讨论】:

  • 您尝试了所有尝试,它们的错误信息是什么?
  • 嗨约阿希姆。好问题忘了发布:
  • 还尝试逐行调试,当检查变量内容时,我清楚地看到了温度和符号。即: temp_sign -> 包含'+', temp_temp_int -> 包含(26,这是我期望包含的)和 temp_temp_dec ->(23,这是小数部分,当然如预期的那样正确)。我没有得到的是显示数据。

标签: c++ c++builder


【解决方案1】:

我建议如下:

    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;

【讨论】:

  • 谢谢阿特姆。但是根据您的建议,我得到了:+0.0
  • @user1479770 所以我们已经接近解决方案了,这很好。你能告诉我变量 temp_sign、temp_temp_int、temp_temp_dec 的类型吗?
  • @user1479770 如果值为 '+'、0 和 0,你应该得到“+ 0.0”,这是预期的。
  • 好的。我刚刚调试了代码,我可以看到温度值已加载到变量中。我进入 temp_sign = + , temp_temp_int=26 和 temp_temp_dec = .26 ,这是正确的。但问题仍然存在我可以在文本框中获取这些值在这一刻我只得到 0。是的,就是 0。有什么建议吗?谢谢大家。
  • 对于类似的情况,我更喜欢 deleaker 或 valgrind。
【解决方案2】:

您得到了错误的结果,因为您的尝试与您正在尝试的内容是错误的,并且大多数都源于相同的根本问题 - 您误用了 , 运算符。在表达式而不是参数列表中,, 运算符执行两边的运算,运算符的结果是右手运算的结果。您在表达式中将多个 , 操作链接在一起,并期望它们产生实际上不会产生的结果。

temp_txtBox_Cond->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->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 属性,然后在它返回的临时String 上调用printf(),但之后您没有将String 分配回Text 属性。

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

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

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;

你在Text 中得到"0",因为temp_temp_dec 开头为0(尽管你认为你在调试器中看到了什么)。

temp_txtBox_Cond->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->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"。您无法使用该代码在Text 中获得"+0.0"

综上所述,请改用以下其中一种:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    相关资源
    最近更新 更多