Link to home
Start Free TrialLog in
Avatar of fastxbl
fastxbl

asked on

how to delete from listbox using edit box

Hi i have almost made the code to delete from listbox  using data from text field, but i am missing the part that needs to say "its not there".

I mean, i need my code to say "if the text is not in the listbox show message "not there", to avoid having a list index out of bounce.

here is my code and i have marked the part i need done


procedure TForm13.unbannickClick(Sender: TObject);

begin
 if  form15.ListBox1.Items.Text=(form13.DBname.text)
 then begin
form15.listBox1.Items.Delete(form15.listbox1.Items.IndexOf(DBname.text))
 end else begin
    ****(If the name thats in the text box is not in listbox1)
THEN begin
showmessage(' Nothing there');

END;

 end;
end.
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Try this:

procedure TForm13.unbannickClick(Sender: TObject);

begin
 if  form15.ListBox1.Items.Text=(form13.DBname.text)
 then begin
  if form15.listBox1.IndexOf(form13.DBname.text) > - 1 then
    form15.listBox1.Items.Delete(form15.listbox1.Items.IndexOf(DBname.text))
 end else begin
showmessage(' Nothing there');
END;
 end;
end.
Avatar of fastxbl
fastxbl

ASKER

Thanks, i did that but same thing happens, it shows nothing there message fine when nothing is there, but also shows same thing when it exsists and doesnt delete it.

it wouldnt compile, so i had to change

if form15.listBox1.IndexOf(form13.DBname.text) > - 1 then

to

if form15.listBox1.items.IndexOf(form13.DBname.text) > - 1 then
ASKER CERTIFIED SOLUTION
Avatar of fastxbl
fastxbl

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hint 1:
rename your forms and components
especially if this is a program which will be used by others
you will be trying to translate the chat form or nick list form to a Form15 or Form13
your users will think you are talking chinese to them if you talk about Form15 and Form13

hint 2:
in methods (procedures or functions of a class), do not refer to the instance of that class
use self instead ... or don't use any reference at all
delphi follows the scope starting local and then expanding globally to look for a variable/procedure/function, ... etc

sample of what will easily break your code:
Form15 := TForm13.Create(application)
Form13 := TForm15.Create(application)

hint3:
use indentation

hint4:
use of placement of then, else, begin, end, etc ... keep the structures always the same, don't start mixing, or your eyes are going to hurt after 20 minutes of writing code
>> i usually find errors within seconds, if hint3 and 4 are followed

hint5:
capitalization helps for easy reading

you code improved with all hints:
>> Assume Form13 = TfrmChat and Form15 = TfrmNicks, ListBox1 = lbNicks

procedure TfrmChat.UnbanNickClick(Sender: TObject);
var y: integer;
begin
  y := frmNicks.lbNicks.Items.IndexOf(dbName.text);
  if y <> -1 then 
    frmNicks.lbNicks.Items.Delete(y)
  else 
    ShowMessage('Nothing found in nick names list');
end;

Open in new window