Mega Code Archive

 
Categories / Delphi / VCL
 

How do I click an empty listbox

Title: How do I click an empty listbox? Question: Normally you cannot click an empty box- the application in this article allows this Answer: The application below is a buzzword generator- click a button and it will generate a phrase of 1-4 words (you select the length). There are 4 lists and you can add words by clicking on an empty part of the list. To delete any existing word just click the word. The lists are automatically saved out and reloaded when the application is run unit genword; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) ListBox1: TListBox; ListBox2: TListBox; ListBox3: TListBox; ListBox4: TListBox; CheckBox1: TCheckBox; CheckBox2: TCheckBox; CheckBox3: TCheckBox; CheckBox4: TCheckBox; Edit1: TEdit; Button1: TButton; procedure FormCreate(Sender: TObject); procedure FormActivate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure AddWords(Sender: TObject); procedure Button1Click(Sender: TObject); procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private declarations } public Aftercreate : boolean; { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin Aftercreate := true; randomize; end; procedure TForm1.FormActivate(Sender: TObject); begin if AFtercreate then begin Aftercreate := false; if fileexists('1st.txt') then listbox1.Items.LoadFromFile('1st.txt'); if fileexists('2nd.txt') then listbox2.Items.LoadFromFile('2nd.txt'); if fileexists('3rd.txt') then listbox3.Items.LoadFromFile('3rd.txt'); if fileexists('4th.txt') then listbox4.Items.LoadFromFile('4th.txt'); end; end; procedure TForm1.FormDestroy(Sender: TObject); begin if listbox1.items.count 0 then listbox1.items.savetofile('1st.txt'); if listbox2.items.count 0 then listbox2.items.savetofile('2nd.txt'); if listbox3.items.count 0 then listbox3.items.savetofile('3rd.txt'); if listbox4.items.count 0 then listbox4.items.savetofile('4th.txt'); end; procedure TForm1.AddWords(Sender: TObject); var str : string; boxnum : integer; begin Str := ''; boxnum := (sender as tlistbox).tag; str:= inputbox('Enter new word','New Word',Str); if trim(str) '' then begin case boxnum of 0:listbox1.items.add(str); 1:listbox2.items.add(str); 2:listbox3.items.add(str); 3:listbox4.items.add(str); end; end; end; function Randword(Box : tlistbox):string; var Index : integer; begin Index := random(box.items.count); result := box.Items[Index]; end; procedure TForm1.Button1Click(Sender: TObject); var str,str1,Str2,Str3,Str4 : string; begin str1 := ''; str2 := ''; str3 := ''; str4 := ''; if checkbox1.checked and (listbox1.items.count0) then str1 := RandWord(listbox1); if checkbox2.checked and (listbox2.items.count0) then str2 := RandWord(listbox2); if checkbox3.checked and (listbox3.items.count0) then str3 := RandWord(listbox3); if checkbox4.checked and (listbox4.items.count0) then str4 := RandWord(listbox4); if str1 '' then Str := Str1; if str2 '' then Str := Str +' '+ Str2; if str3 '' then Str := Str +' '+ Str3; if str4 '' then Str := Str +' '+ Str4; edit1.text := str; Str := ''; if str1 '' then Str := Str1[1]; if str2 '' then Str := Str + Str2[1]; if str3 '' then Str := Str + Str3[1]; if str4 '' then Str := Str + Str4[1]; edit1.text := edit1.text +' ('+str+')'; end; function Confirm(S:string):boolean; begin Result := MessageDlg(S+'?', mtConfirmation, [mbYes, mbNo], 0)= Mryes; end; procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var selIndex : integer; str : string; p : Tpoint; begin p.X := X; P.y := Y; SelIndex:= (sender as Tlistbox).ItemAtPos(p,true); If SelIndex =-1 then AddWords(sender) else begin str := (sender as tlistbox).items[SelIndex]; if Confirm('Delete '+str) then (Sender as Tlistbox).Items.Delete(SelIndex); end; end; end. object Form1: TForm1 Left = 313 Top = 243 Width = 682 Height = 343 Caption = 'title Generator' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnActivate = FormActivate OnCreate = FormCreate OnDestroy = FormDestroy PixelsPerInch = 96 TextHeight = 13 object ListBox1: TListBox Left = 48 Top = 42 Width = 127 Height = 173 IntegralHeight = True ItemHeight = 13 Items.Strings = ( 'Oberon') TabOrder = 0 OnMouseDown = ListBox1MouseDown end object ListBox2: TListBox Tag = 1 Left = 186 Top = 42 Width = 127 Height = 173 IntegralHeight = True ItemHeight = 13 TabOrder = 1 OnMouseDown = ListBox1MouseDown end object ListBox3: TListBox Tag = 2 Left = 318 Top = 42 Width = 127 Height = 173 IntegralHeight = True ItemHeight = 13 TabOrder = 2 OnMouseDown = ListBox1MouseDown end object ListBox4: TListBox Tag = 3 Left = 456 Top = 42 Width = 127 Height = 173 IntegralHeight = True ItemHeight = 13 TabOrder = 3 OnMouseDown = ListBox1MouseDown end object CheckBox1: TCheckBox Left = 54 Top = 222 Width = 97 Height = 17 Caption = '1st' Checked = True State = cbChecked TabOrder = 4 end object CheckBox2: TCheckBox Left = 186 Top = 222 Width = 97 Height = 17 Caption = '2nd' Checked = True State = cbChecked TabOrder = 5 end object CheckBox3: TCheckBox Left = 318 Top = 222 Width = 97 Height = 17 Caption = '3rd' Checked = True State = cbChecked TabOrder = 6 end object CheckBox4: TCheckBox Left = 456 Top = 222 Width = 97 Height = 17 Caption = '4th' Checked = True State = cbChecked TabOrder = 7 end object Edit1: TEdit Left = 54 Top = 252 Width = 523 Height = 21 TabOrder = 8 end object Button1: TButton Left = 588 Top = 252 Width = 75 Height = 25 Caption = 'Go' TabOrder = 9 OnClick = Button1Click end end