Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox - output selected AND deselected

Status
Not open for further replies.

delphi6BDE

Programmer
Sep 28, 2009
21
CA

During a larger procedure, I have a multi-select listbox that loads a list of flagged items. this list of flagged items needs to be looped twice to determine what is selected, and what is not selected and hopefully give me two SQL outputs on each pass; when selected, give me an in clause like '
SQL:
In ('item1', 'item2')
' and at the same time give me '
SQL:
not in ('item3')
'. Once the listbox has been completely selected, I would like it to then deselect each item and do the same thing in reverse giving me all possible combinations of these three items.

I'm able to loop and output what has been selected, but not what is not selected. Does anybody know how to finish my sample code into what I'd like to do?

Hopefully I'm making sense here with my question, but if clarification is needed, please ask and I'll try my best to walk you though with more detail.

I would like the results of the loops to be:
Select sample (nothing should be selected at beginning of loop):
pass 1:
SQL:
in ('') and not in ('item1', 'item2', 'item3')
pass 2:
SQL:
in ('item1') and not in ('item2', 'item3')
pass 3:
SQL:
in ('item1', 'item2') and not in ('item3')

Deselect sample (all items should be selected at beginning of loop):
pass 1:
SQL:
not in ('') and in ('item1', 'item2', 'item3')
pass 2:
SQL:
not in ('item1') and in ('item2', 'item3')
pass 3:
SQL:
not in ('item1', 'item2') and in ('item3')

Sort of working so far:
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
  vSel: string;
begin
  for i := 0 to Listbox1.Items.Count -1 do
    begin
      Listbox1.Selected[i]:=true;
      vSel:=vSel + QuotedStr(Listbox1.Items[i]) + ', ';
      //trim last comma
      //vSel:=Copy(vSel, 0, Length(vSel) -2);
      Memo1.Lines.Add(vSel);
      if Listbox1.Selected[i] = false then
        Memo1.Lines.Add(inttostr(i));
    end;
end;
 
I can't figure out how to edit or delete my post, so I will carry it on with an update:

Ignore the code in my original question, the below code is better and is almost working. It's just not giving me what's not selected. Can anybody help finish this simple yet somehow tedious task?

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
  Sel, DeSel: string;
begin
  Sel:='';
  DeSel:='';
  mmoSQL.Clear;
  for i := 0 to Listbox1.Items.Count -1 do
    begin
      Listbox1.Selected[i] := True;
      if not Listbox1.Selected[i] = True then
          DeSel := DeSel + QuotedStr(Listbox1.Items[i]) + ', '
      else
          Sel := Sel + QuotedStr(Listbox1.Items[i]) + ', ';
      mmoSQL.Lines.Add('IN (' + Copy(Sel, 0, Length(Sel) -2) + ')');
      mmoSQL.Lines.Add('NOT IN (' + Copy(DeSel, 0, Length(DeSel) -2) + ')');
    end;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top