INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Listbox - output selected AND deselected

Listbox - output selected AND deselected

(OP)

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 '

CODE --> sql

In ('item1', 'item2') 
' and at the same time give me '

CODE --> 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:

CODE --> sql

in ('') and not in ('item1', 'item2', 'item3') 
pass 2:

CODE --> sql

in ('item1') and not in ('item2', 'item3') 
pass 3:

CODE --> sql

in ('item1', 'item2') and not in ('item3') 

Deselect sample (all items should be selected at beginning of loop):
pass 1:

CODE --> sql

not in ('') and in ('item1', 'item2', 'item3') 
pass 2:

CODE --> sql

not in ('item1') and in ('item2', 'item3') 
pass 3:

CODE --> sql

not in ('item1', 'item2') and in ('item3') 

Sort of working so far:

CODE --> delphi

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; 

RE: Listbox - output selected AND deselected

(OP)
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 --> Delphi

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; 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close