[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

adding to list boxes

Posted on 2011-05-06
17
Medium Priority
?
342 Views
Last Modified: 2012-05-11
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;
0
Comment
Question by:boycoder
  • 6
  • 5
  • 5
  • +1
17 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35705146
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
 

Author Comment

by:boycoder
ID: 35705308
that works mate, but it only adds a new user to speciallist2 not both.

thanks
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35705328
It must not exist in speciallist1 to be added. Does it exist in speciallist1?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:boycoder
ID: 35705361
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35705363
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
 
LVL 24

Expert Comment

by:jimyX
ID: 35705447
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
 

Author Comment

by:boycoder
ID: 35705462
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
 

Author Comment

by:boycoder
ID: 35705536
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
 

Author Comment

by:boycoder
ID: 35705546
also what does EXIT; and FREE; mean in delphi.. i always wanted to know.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35705551
nice to know you are open for expert advice :)
not all people are
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35705563
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35705569
it's even better to use FreeAndNil(Object)
this also sets the variable object := nil;
0
 
LVL 24

Accepted Solution

by:
jimyX earned 2000 total points
ID: 35705579

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
 

Author Comment

by:boycoder
ID: 35705630
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
 
LVL 24

Expert Comment

by:jimyX
ID: 35706013
"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
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35706050

@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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35708059
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question