组合框中是可能存在的文件名,当从选择一个项目后,我需要先判断所选择的项目对应的文件是否存在,如果存在,则接受这个选择,并做其他处理。如果对应的文件不存在,我想取消刚才的选择。就是让组合框上的内容还原成原来的项目。如何实现?谢谢。

解决方案 »

  1.   

    这个应该很简单,组合框当前显示的值应该就是你要判断的文件名吧,那么我们ComboBox1.DeleteSelected;
    就可以了。怎么样,给分吧
      

  2.   

    再给你具体点,刚才试了一下,给你代码:
    if not fileexists(combobox1.Text) then
    self.ComboBox1.DeleteSelected;
    好了,可以给分了
      

  3.   

    谢谢,
    不过,可能是我没有说清楚。假设组合框中有
    A
    B
    C
    D
    四个项目
    其中
    A
    B
    有对应文件
    A.txt
    B.txt
    -------------
    C
    D
    没有最开始,组合框的.TEXT为空,
    如果我选择了A
    那么,接受这个选择。
    如果我选择了C
    则取消这个选择,还是还原成空经过一次有效的选择后,假设当前的TEXT=B
    这时如果再选择C,那么应该取消该选择,因为C项没有对应的文件,所以上面的TEXT要还是还原成B。
    如果选择的是A,那么接受该选择,不需要取消和还原。我不是要删除。
      

  4.   

    做好了,给你代码,不过注意给分呀
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        ComboBox1: TComboBox;
        procedure ComboBox1Select(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        index:integer;
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ComboBox1Select(Sender: TObject);
    begin
    if fileexists(self.ComboBox1.Text+'.txt') then
    index:=self.ComboBox1.ItemIndex
    else
    self.ComboBox1.ItemIndex:=index;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
    index:=ComboBox1.ItemIndex;
    end;end.
      

  5.   

    谢谢,看来要用一个公用变量,用select事件。我原来用change事件。顺便问一下,在DELPHI中,对象的事件顺序如何知道?哪里有这方面的资料?