【问题标题】:Remove selected component from combobox DELPHI从组合框中删除选定的组件 DELPHI
【发布时间】:2014-04-12 20:51:24
【问题描述】:

我有 ComboBox4、ComboBox1 和 Button5 当我单击 Button5 时,程序应从 ComboBox4 和 ComboBox1 组件列表中删除在 combobox4 中选择的组件。但是我使用以下代码得到列表越界错误...

procedure TForm1.Button5Click(Sender: TObject);
var
cat : Integer;
trinti: TComponent;
catT : String;
begin
catT := ComboBox4.Text;
cat := ComboBox4.Items.IndexOf(catT);
trinti := ComboBox4.Components[cat];

ComboBox1.Items.BeginUpdate;
ComboBox4.Items.BeginUpdate;
  ComboBox4.RemoveComponent(trinti);
  ComboBox1.RemoveComponent(trinti);
ComboBox1.Items.EndUpdate;
ComboBox4.Items.EndUpdate;

removeCat(catT);
end;

请帮忙:(

【问题讨论】:

    标签: delphi combobox


    【解决方案1】:

    Components 属性和RemoveComponent 方法在这里使用是错误的。这些用于所有权和生命周期管理。通常,表单上唯一拥有任何东西的就是表单本身。所以在组合框上使用Components总是会报错。

    您需要使用组合框的Items 属性及其Delete 方法。它可能看起来像这样:

    var
      Index: Integer;
    ....
    catT := ComboBox4.Text;
    Index := ComboBox4.Items.IndexOf(catT);
    if Index <> -1 then
      ComboBox4.Items.Delete(Index);
    

    【讨论】:

    • 像这样:ComboBox4.Items[cat].Delete;?但是没有删除 Items[] 的方法。 ://
    • 我明白了,但我仍然得到列表超出范围,请参阅我的代码:pastebin.com/ruNcU4yf
    • 你没有听我的建议。再次阅读我的答案的第一段。然后问问自己trinti := ComboBox4.Components[cat] 做了什么。
    • 我不想创建新主题,所以也许你也知道如何运行一个程序,而不是由用户,但是当程序启动时,我想加载我的 XML 并将信息从 xml 添加到程序启动时我的组合框。
    • 将方法附加到表单的OnCreate 事件以执行初始化。
    猜你喜欢
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 2018-05-24
    • 2010-10-19
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多