adding to list boxes

Hi, i have some code but i cant get it to work the way i want. The code is probably messed up now..

I want when i select a user from the menu it adds the field to speciallist1 which is a listbox. This is fine, when i close program and reopen it saves and opens the listbox fine.
When i try and add the user again, it gives an error and this is perfect. However.. i want to add the user to both listboxes..

speciallist1 and speciallist2

if i try and add the user again, if he is located in listbox2 already (speciallist2 ) dont add him to any boxes.

dont bother checking speciallist1.. only the 2nd listbox.

my messed code:

DBadd1 = edit.text
speciallist1 = listbox
speciallist2 = listbox
mylist11 = save list for 1st listbox
mylist12 = save list for 2nd listbox


Iam working on form 13 and using listboxes on form35.

procedure TForm13.Addcustomertospeciallist1Click(Sender: TObject);

var
  strm : TFileStream;
begin
if
form35.speciallist2.Items.IndexOf(DBadd1.Text) = -1
then begin
 form35.speciallist2.items.add(DBadd1.text);

     strm := TFileStream.Create('MyList12.DAT', fmCreate);
  strm.WriteComponent(form35.speciallist2);
  strm.Free;

end;

if
form35.speciallist1.Items.IndexOf(DBadd1.Text) = -1
then begin
 form35.speciallist1.items.add(DBadd1.text);

     strm := TFileStream.Create('MyList11.DAT', fmCreate);
  strm.WriteComponent(form35.speciallist1);
  strm.Free
 end
else
begin

windows.MessageBox(handle, 'User already special member specialist1',  'SPECIAL LIST - NOT ADDED!!',
         MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONhand)
  end;
  end;
     end;
boycoderAsked:
Who is Participating?
 
jimyXCommented:

procedure TForm13.Addcustomertospeciallist1Click(Sender: TObject);
var
  strm : TFileStream;
begin
if
form35.speciallist2.Items.IndexOf(DBadd1.Text) = -1
then begin
 form35.speciallist2.items.add(DBadd1.text);

     strm := TFileStream.Create('MyList12.DAT', fmCreate);
  strm.WriteComponent(form35.speciallist2);
  strm.Free;

end
else
  begin
     windows.MessageBox(handle, 'User already special member specialist2',  'SPECIAL LIST - NOT ADDED!!',
         MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONhand);
    exit;
  end;

if
form35.speciallist1.Items.IndexOf(DBadd1.Text) = -1
then begin
 form35.speciallist1.items.add(DBadd1.text);

     strm := TFileStream.Create('MyList11.DAT', fmCreate);
  strm.WriteComponent(form35.speciallist1);
  strm.Free
 end
else
  begin

    windows.MessageBox(handle, 'User already special member specialist1',  'SPECIAL LIST - NOT ADDED!!',
         MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONhand);
    exit;
  end;

   Windows.MessageBox(
     handle,
     ' Added to the list',
     'Member added!',
     MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONASTERISK) ;
end;
0
 
jimyXCommented:
If I understand you correctly then:
procedure TForm13.Addcustomertospeciallist1Click(Sender: TObject);
var
  strm : TFileStream;
begin
if
form35.speciallist2.Items.IndexOf(DBadd1.Text) = -1
then begin
 form35.speciallist2.items.add(DBadd1.text);

     strm := TFileStream.Create('MyList12.DAT', fmCreate);
  strm.WriteComponent(form35.speciallist2);
  strm.Free;

end
else                                                                               // exit if the user is found in the speciallist2
  exit;

if
form35.speciallist1.Items.IndexOf(DBadd1.Text) = -1
then begin
 form35.speciallist1.items.add(DBadd1.text);

     strm := TFileStream.Create('MyList11.DAT', fmCreate);
  strm.WriteComponent(form35.speciallist1);
  strm.Free
 end
else
begin

windows.MessageBox(handle, 'User already special member specialist1',  'SPECIAL LIST - NOT ADDED!!',
         MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONhand)
  end;
  end;
     end;

Open in new window

0
 
boycoderAuthor Commented:
that works mate, but it only adds a new user to speciallist2 not both.

thanks
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
jimyXCommented:
It must not exist in speciallist1 to be added. Does it exist in speciallist1?
0
 
boycoderAuthor Commented:
No, when i try and add a new user to both lists at the same time, it adds to speciallist2 but not speciallist1.

When adding a new user they must both be in both lists..

later, if i delete it from speciallist1.. and try to re-add it, if it exists in the backup sepcialist 2, dont add.
0
 
Geert GOracle dbaCommented:
no wonder that you don't want to change form names

good practices:
 * always rename your unit names, forms and components  
 * use indentation
 * do not use form variable names in other forms

i have not enough knowledge about your problem to give you a better approach than what you are doing
but  i'll try:
 
unit uForm13;

interface 
 
... 

implementation

uses uForm35;

procedure TForm13.Addcustomertospeciallist1Click(Sender: TObject);
begin
  AddSpecialist(DBAdd1.Text);
end;

Open in new window


 
unit uForm35;

interface 

type
  TForm35 = class(TForm)
  ...
  private
    procedure AddSpecialistToListbox(aListboxName, aName, aFileName: string; ShowAlert: Boolean = False);
  end;

var Form35: TForm35;

procedure AddSpecialist(aName, aFileName: string);

implementation

procedure AddSpecialist(aName: string);
begin
  if not Assigned(Form35) then 
  begin
    Form35 := TForm35.Create(Application);
    Form35.Show;
    Form35.Update;
  end;
  Form35.AddSpecialistToListbox('SpecialList1', aName, 'MyList11.DAT', True);
  Form35.AddSpecialistToListbox('SpecialList2', aName, 'MyList12.DAT');
end;

procedure TForm35.AddSpecialistToListbox(aListboxName, aName, aFileName: string; ShowAlert: Boolean = False);
var 
  Lb: TListbox;
  strm: TFileStream;
begin
  Lb := TListBox(FindComponent(aListBoxName));
  if Assigned(Lb) then 
  begin
    if Lb.Items.IndexOf(aName) = -1 then 
    begin
      Lb.Items.Add(aName);
      strm := TFileStream.Create(aFileName, fmCreate);
      try
        strm.WriteComponent(Lb);
      finally
        strm.Free;
      Free;
    end else if ShowAlert then 
      windows.MessageBox(handle, 'User already special member specialist1',  'SPECIAL LIST - NOT ADDED!!',
         MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONhand);
  end else 
    MessageDlg('Listbox "' + aListBoxName + '" not found.', mtWarning, [mbOk], 0);
end;

Open in new window


with code like this, i can rename form35 to anything i want without affecting form13
this is very important if you ever somebody to help with your program
and they tell you they will only help after you have changed every unit, form and component name to something meaningfull
0
 
jimyXCommented:
Something wrong somewhere. Please show your final code, you must have something breaks the execution of the code to reach the speciallist1 part. I can expect that you changed the part of "else exit" which gives you unexpected result.
Try this one as it is and let's see how it goes:
procedure TForm13.Addcustomertospeciallist1Click(Sender: TObject);
var
  strm : TFileStream;
begin
if
form35.speciallist2.Items.IndexOf(DBadd1.Text) = -1
then begin
 form35.speciallist2.items.add(DBadd1.text);

     strm := TFileStream.Create('MyList12.DAT', fmCreate);
  strm.WriteComponent(form35.speciallist2);
  strm.Free;

end
else                                                                               // exit if the user is found in the speciallist2
  begin
     windows.MessageBox(handle, 'User already special member specialist2',  'SPECIAL LIST - NOT ADDED!!',
         MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONhand);
    exit;                  // if you have more than one line put them in "Begin & end"
  end;

if
form35.speciallist1.Items.IndexOf(DBadd1.Text) = -1
then begin
 form35.speciallist1.items.add(DBadd1.text);

     strm := TFileStream.Create('MyList11.DAT', fmCreate);
  strm.WriteComponent(form35.speciallist1);
  strm.Free
 end
else
begin

windows.MessageBox(handle, 'User already special member specialist1',  'SPECIAL LIST - NOT ADDED!!',
         MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONhand)
  end;
end;

Open in new window

0
 
boycoderAuthor Commented:
Geert_Gruwez:, Thanks for that advice i didnt actually relise i could set it out that way. It is a little complicated as i an pretty new to pascal and they way i have my code now i can understand it. I dont really want to just paste code in and yay it works, i want to understand it, so the code that jim is working on suits me better. I have added it to a text file and i will take a look at that later today and learn how it works , for now ill wait untill jim answers if you dont mind, but thanks for that script, great to learn from, thanks.
0
 
boycoderAuthor Commented:
sorry jim i tried to add the code below so i get confirmation that a user is added. But it must have messed it up, it works now great but how can i add that code below withought effecting the code ?

   Windows.MessageBox(
     handle,
     ' Added to the list',
     'Member added!,
     MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONASTERISK) ;



0
 
boycoderAuthor Commented:
also what does EXIT; and FREE; mean in delphi.. i always wanted to know.
0
 
Geert GOracle dbaCommented:
nice to know you are open for expert advice :)
not all people are
0
 
Geert GOracle dbaCommented:
exit means you jump to the end of the proc/func
when using exit inside a try finally ... the code in the finally will still be executed !

free means you release the memory (and the object itself) for a object
0
 
Geert GOracle dbaCommented:
it's even better to use FreeAndNil(Object)
this also sets the variable object := nil;
0
 
boycoderAuthor Commented:
Thanks for explanation of FREE AND EXIT. Helps alot!!!

Jim, thanks very much, perfect work as always mate. I willask another Q in a few mins as i need to try something myself and if i have issues, ill post, Thanks again.
0
 
jimyXCommented:
"Exit" is a Delphi procedure that can be used anywhere in a code block (function, procedure, event) to skip executing what's under it, except when used inside Try-Finally block.

"Free" means destroy the object and free its location in memory that was allocated for it. When we say memory we refer to the RAM of the PC. When the computer starts, the Operating System puts everything that you see is running (The OS itself and the other Applications) in the memory and when you close one of the running processes the place that it occupies in the memory must be freed in order to give a space to other processes once started. In programming, you are dealing with the PC memory and whatever objects you create stay in the memory until you clearly destroy them and free their locations to be used by others, or until you restart your PC (because the memory looses the power and the RAM is volatile memory).
All that because every resource is limited, even if you have a large RAM you can't start countless processes, you have a limit no matter what you do. We said if you do not free the objects they remain until you restart the PC and you can imagine if your PC is required to operate 24/7 then that object that is hanging in the memory has reduced your memory and if you have to create that object many times without being freed, all creations stay in the memory.

So FREE is always used to free the objects that you created and no longer need. The other objects that was created in the application such as buttons, labels, listboxes,...etc. get freed when the application is closed (destroyed) because the application is the owner of them, so you do not have to worry about them, but you need to take care of what you create by the code.

You may also check Delphi help files for more information.

From the Delphi help files:
Exit:
Description

In Delphi, the Exit procedure immediately passes control away from the current procedure. If the current procedure is the main program, Exit causes the program to terminate.

Exit will cause the calling procedure to continue with the statement after the point at which the procedure was called.

Note:      Exit passes control away from the current procedure, not merely the current block. But Exit does not violate the flow of control dictated by a try..finally construct; if Exit is called inside the try clause, the finally clause is still executed.

Free:
Description

Use Free to destroy an object. Free automatically calls the destructor if the object reference is not nil. Any object instantiated at runtime that does not have an owner should be destroyed by a call to Free so that it can be properly disposed of and its memory released. Unlike Destroy, Free is successful even if the object is nil; so if the object was never initialized, Free won’t result in an error.

When you call Free for a component, it calls Free for all components that it owns—that is, all components in its component list. Since a form owns all the controls and other components that are created on it in design mode, those components are automatically freed when the form is freed. By default, all forms are owned by the Application object; when the application terminates, it frees the Application object, which frees all forms. For objects that are not components, or for components created with a nil owner, be sure to call Free after you are finished with them; otherwise the allocated memory will not be usable until after the application terminates.

Warning:      Never explicitly free a component within one of its own event handlers or the event handler of a component it owns or contains. For example, don’t free a button, or the form that owns the button, in its OnClick event handler.

To free a form, call its Release method, which destroys the form and releases the memory allocated for it after all its event handlers and those of the components it contains are through executing.
0
 
Ephraim WangoyaCommented:

@boycoder
Just going through this question (I know its closed)
Why are you saving the entire form in a file instead of simply saving the data in the list box?
This is not right. It simply means you will not be able to modify this form in future. This is just something for you to consider
0
 
Geert GOracle dbaCommented:
showing thanx can also be done by assigning points to multiple answers

and i see multiple people having lots of questions about your code
i'd try and ask why ...
assigning poinx to the answers you use is also a indication that you actually understand what experts comment on
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.