添加字符串到组合框 [英] add strings to comboboxes

查看:101
本文介绍了添加字符串到组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在组合框中添加40个字符串。我正在使用基于对话框的mfc应用程序。我在对话框中有20个组合框,我想在所有对话框中添加相同的40个字符串。

我怎么能这样做?我可以分别为每个组合框添加字符串,但程序将变得冗长。他们是其他任何方法吗?请Helep我。



提前谢谢。

I want to add 40 strings to combobox. I am using dialogbased mfc applications. I have 20 comboboxes on dialog and I want to add same 40 strings to all the dialogboxes.
How i can do that? I can add strings to each combobox separatly but the program will become lengthy. Is their any other method? Please Helep me.

Thanks in advance.

推荐答案

在20个组合框中添加40个字符串不应该采取很多时间;在某个地方肯定会有其他一些问题。



你至少应该有一个数组,其中包含每个组合框的ID(或者可能是组合框本身的数组),以便更快地访问。



我刚试过这样的东西,似乎工作正常。



Adding 40 strings to 20 combobox should NOT take much time; there must be some other issues somewhere.

You should at least have an array with the ID of each combobox (or maybe an array of the combobox themselves) for quicker access.

I just tried something like this, and it seems to be working fine.

const int numberOfStrings = 40;
CString arrayOfString[numberOfStrings ];

const int numberOfComboBox = 20;
UINT arrayOfId[numberOfComboBox ];
// fill the array with the ID of the combobox
//...


for ( unsigned int comboIndex = 0; comboIndex < numberOfComboBox; comboIndex++ )
{
  CComboBox* combo = (CComboBox*) GetDlgItem( arrayOfId[comboIndex] );
  for (unsigned int stringIndex = 0; stringIndex < numberOfStrings; stringIndex++ 
  {
    combo->AddString(arrayOfString[stringIndex]);
  }
}





但是正如理查德写的那样,应该有更好的方法来做你想做的事,而不必在一个单独的20+组合表格。



Max。



But as Richard wrote, there should be a better way of doing what you want to do without have to have 20+ combo in a single form.

Max.


你还没有说明字符串是否是静态信息(例如Jan, Feb,Mar,...)或每次激活dlg时都可以更改的动态信息。如果信息是静态的,您可以在资源编辑器中为对话框添加字符串。然后将字符串编译成资源数据并没有显示在你的C ++代码中。这比在运行时手动添加要快一点。



另外,如果信息是' '通常''一组字符串或一些信息总是在那里,你可以在资源编辑器和eith中设置默认信息添加到它(AddString())或在代码中替换它(ResetContent())。
You haven''t stated if the strings are static information (eg. "Jan", "Feb", "Mar", ...) or dynamic information that can change each time you activate the dlg. If the information is static, you can add the strings in the resource editor for the dialog. The strings are then compiled into the resource data and don''t show up in your C++ code. Doing this is a little faster than manually adding at runtime.

Also, if the information is ''usually'' one set of strings or some of the information is always there, you can set the default info in the resource editor and either add to it ("AddString()") or replace it ("ResetContent()") in code.


这篇关于添加字符串到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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