Delphi7,创建组合框项 [英] Delphi7, create combobox items

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

问题描述

我想使用组合框为用户提供多种选择。因此有2个组合框。第一个选项有大约5个选项,第二个选项将根据用户在第一个组合框中选择的内容来创建。

I would like to give the user a variety of options using combobox. So there are 2 combobox. The 1st one has about 5 options, the second ones items will be created based on what the user will choose at the 1st combobox.

到目前为止,我已经尝试过: Combobox2.Items.Strings [1]:='xxxx'其中向我显示此错误:

So far, I have tried this: Combobox2.Items.Strings[1]:='xxxx' which appears me this error:

List out of Bound.

我该怎么办?

推荐答案

要在运行时填充 TComboBox ,您可以执行以下操作[1]:

To populate a TComboBox at runtime, you can do like this [1]:

ComboBox1.Items.BeginUpdate;
try
  ComboBox1.Items.Clear;
  ComboBox1.Items.Add('Alpha');
  ComboBox1.Items.Add('Beta');
  ComboBox1.Items.Add('Gamma');
  ComboBox1.Items.Add('Delta');
finally
  ComboBox1.Items.EndUpdate;
end;

您还可以分配预制的 TStringList

You can also assign a pre-fabricated TStringList to it:

ComboBox1.Items.Assign(MyStringList);






[1]:


[1]:

try..finally 部分很重要,因为如果没有它,如果引发异常并且在 BeginUpdate 和 EndUpdate ,组合框将保持(卡住)其更新状态,因此从那时起将出现故障。

The try..finally part is important, because, without it, if an exception is raised and not handled between BeginUpdate and EndUpdate, the combobox will remain ("get stuck") in its "updating" state and so will malfunction from that point on.

当然,在这个琐碎的示例中,异常的风险很小,但在其他情况下,它的影响可能很大。而且代码可能会更改:您可以添加 ComboBox1.Items.Add(MightRaise())如果MightRaise()然后添加ComboBox1.Items。将来会添加('Epsilon')

Of course, in this trivial example, the risk of an exception is minute, but in other cases it can be significant. And the code might change: you might add a ComboBox1.Items.Add(MightRaise()) or an if MightRaise() then ComboBox1.Items.Add('Epsilon') in the future.

无论如何,您都希望代码在所有情况下均能100%正常工作,而不是99.9% %。同样,上述模式很容易识别,至少对我来说有助于理解代码。如果您始终使用相同的模式,则代码在脑海中变得更容易解析。

At any rate, you want code that works in 100 % of all cases, not 99.9 %. Also, the above pattern is easily recognized and at least to me aids in the understanding of the code. If you always use the same patterns, the code becomes easier to parse mentally.

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

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