用两个combobox和一个edit做一个查询~根据在combobox1中选中的项目~combobox2显示相应的子项目~再根据combobox2中选中的子项目~在edit中显示最终结果~我是用sql server2000和delphi连接的~
我写了一下代码~combobox1可以实现~combobox2就不行了~请问问题出现在那里?怎么改?procedure TForm12.FormCreate(Sender: TObject);
begin
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('SELECT distinct brand from CPU');
adoquery1.Open;
combobox1.Items.Clear;
while not adoquery1.Eof do
 begin
 combobox1.Items.Add(adoquery1.fieldbyname('brand').AsString);
 adoquery1.Next;
 end;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('SELECT distinct type from CPU where brand='''+combobox1.SelText+'''');
adoquery1.Open;
combobox2.Items.Clear;
while not adoquery1.Eof do
 begin
 combobox2.Items.Add(adoquery1.fieldbyname('type').AsString);
 adoquery1.Next;
 end;end;

解决方案 »

  1.   

    因为你在处理了第一个即combobox1后,combobox1的text没有默认值,也就是说 
    combobox1.SelText 的值是空的,故第二个Sql查询不到数据,更改一下就可以了:combobox1.ItemIndex := 0; //就是取出combobox1中的第一个值
    adoquery1.Close; 
    adoquery1.SQL.Clear; 
    adoquery1.SQL.Add('SELECT   distinct   type   from   CPU   where   brand='''+combobox1.SelText+''''); 
    adoquery1.Open; 
    combobox2.Items.Clear; 
    while   not   adoquery1.Eof   do 
      begin 
      combobox2.Items.Add(adoquery1.fieldbyname('type').AsString); 
      adoquery1.Next; 
      end;这样应该就可以了,你试试!
      

  2.   

    谢谢赐教~
    不过我的两个combobox的Text属性都有默认值“请选择”~我试了一下您那个~combobox2没有反应~combobx1倒是直接有了内容~
      

  3.   

    在combobox1的onchange事件中
    adoquery1.Close;   
    adoquery1.SQL.Clear;   
    adoquery1.SQL.Add('SELECT   *       from       table       where       brand='''+combobox1.SelText+'''');   
    adoquery1.Open;   
    combobox2.Items.Clear;   
    while       not       adoquery1.Eof       do   
        begin   
        combobox2.Items.Add(adoquery1.fieldbyname('type').AsString);   
        adoquery1.Next;   
    end; 然后在combobox2的onchange事件中书写
    adoquery1.Close;   
    adoquery1.SQL.Clear;   
    adoquery1.SQL.Add('SELECT     *       from      table where       brand='''+combobox2.SelText+'''');   
    adoquery1.Open;   
    edit1.text.Clear;   
    if       not       adoquery1.Eof       do   
        begin   
        edit1.text := 信息  
    end; 
      

  4.   

    谢谢~我试了一下~还是不行~我觉得理论上是正确的~但是就是联系不上~不知道为什么~我是第一次接触delphi~没有系统的学过~想问一下
    procedure TForm12.ComboBox1Change(Sender: TObject);
    是不是就算是一个onchange事件?
      

  5.   

    试试下面的代码procedure TForm1.FromCreate(Sender: TObject);
    var
      i,k:integer;
      ss : string;
    begin
      Combobox1.Items.Clear ;
      //combobox1加载用户名
      with ADOquery1 do
        begin
            ADOQuery1.Active := False;
            SQL.Clear ;
            SQL.Add('select distinct cUserName from ComUserList');
            active := True;
            k := RecordCount;
            if k > 0 then
              begin
                First ;
                For i := 1 to k do
                  begin
                     ss := FieldByName('cUserName').AsString ;
                     Combobox1.Items.Add(ss);
                     Next ;
                  end;
              end;
        end;  
    end;procedure TForm1.ComboBox1Change(Sender: TObject);
    var
      ss ,cUserName: string;
    begin
      //combobox2 加载用户 对应编号
      Combobox2.Items.Clear ;
      cUserName := Combobox1.Text ;
      ss := 'select * from ComUserList where cUserName = '+''''+cUserName +'''';
      with ADOquery1 do
        begin
            Active := False;
            //close;
            SQL.Clear ;
            SQL.Add(ss);
            Active := True;
            //open;
            combobox2.Items.Add(FieldByName('AutoID').AsString) ;    
        end;
    end;
      

  6.   

    谢谢~我试过了~确实可以~但是有一个问题~为什么combobox2只显示一条内容呢?符合查询语句的结果有很多~光显示一个是成的~应该怎么修改一下呢?
      

  7.   

    因为在procedure TForm1.ComboBox1Change(Sender: TObject);中,没有用循环将所有记录加到ComboBOx2中