如何获得组合框项目数

如何获得组合框项目数

问题描述:

我想要使用以下代码获取组合框项目数。它不会产生错误,也不会产生适量的计数。我想我必须将int转换为字符串,但是如何?如何获得组合框项目数

ComboBox1->ItemIndex = 1; 
int count = ComboBox1->Items->Count; 
Edit1->Text = "Count: " + count; 
+0

你期待什么,在Edit1-> Text中显示什么?你如何将项目添加到ComboBox1以及添加了多少项目? – shf301 2012-08-09 15:24:35

+0

即时对不起,我现在非常喜欢Embarcadero RAD Studio XE2。我创建了一个VCL应用程序 – 2012-08-09 15:26:06

+0

@ shf301它应该是30,并且Edit1-> Text =“” – 2012-08-09 15:28:18

此行

int count = ComboBox1->Items->Count; 

返回您TComboBox的字符串项numnber。你需要设置

ComboBox1->ItemIndex = 1; 

为的ItemIndex是用来设置在组合框中选择的项目是从零开始计数前进行检查。要将整数转换为字符串Embarcadero公司可以使用IntToStr()功能

Edit1->Text = "Count:" + IntToStr(count) 

您需要#include "System.hpp"访问功能

+0

正是我期待的对于。谢谢。 – 2012-08-09 15:46:18

ComboBox1->ItemIndex = 1; 
int count = ComboBox1->Items->Count; 
Edit1->Text = "Count: " + count; 

这里"Count: " + count是其中"Count: "衰减到字符串指针的第一个元素的表达式,count被添加到该指针,其结果是它的字符串(OK)或关闭内的某处或者点字符串的结尾(通常为未定义行为)。

关于使用ComboBox1,你没有显示它的声明,你没有提到你正在使用的GUI框架。

所以没有什么可以说,它没有猜测它是什么。

为了创建插入文本值演示文稿的格式化文本,您可以使用例如一个std::ostringstream<sstream>头,像这样:

std::ostringstream stream; 
stream << "Count: " << count; 
Edit1->text = stream.str().c_str(); 

.c_str()调用可能会或可能不会是必要的,这取决于Edit1.text接受。

ComboBox1->ItemIndex = 1; 
int count = ComboBox1->Items->Count; 
Edit1->Text = "Count: " + count; 

没有任何必要去通过所有这些把戏。一个简单的功能可用于此。

int count = ComboBox1.GetItemCount();