Board index » delphi » DBComboBox Items Problem

DBComboBox Items Problem

Delphi Hobbyist using Professional ver 4.03, DBISAM ver 2.06

I'm populating the items of a DBCombobox with the below code. MemProof
tells me I have some live pointers. I have reason to think they my be
related to below code. Is it possible? Is there a better way of loading
the items?

procedure TfrmMain.dbcbIncSourceEnter(Sender: TObject);
var
  f:   TextFile;
  ist: String;
begin
  ist := ExtractFilePath(ParamStr(0))+ 'IncSrc.txt';
  if FileExists(ist) then
    begin
      with dbcbIncSource do
        begin
          Items.LoadFromFile(ExtractFilePath(ParamStr(0))+
'IncSrc.txt');
        end
    end
  else
    AssignFile(f, ist);
    ReWrite(f, ist);
    CloseFile(f);
end;

 

Re:DBComboBox Items Problem


You allocate memory to hold the items. They should be live until the
ComboBox is destroyed when the form closes. In some cases, the VCL caches
these objects so the items may not be destroyed exactly when you think they
should. I guess the question is when you are looking for live pointers.
There should be some pointing to the memory holding your strings until the
DBComboBox is destroyed, and occasionally longer.

--
Steve F (Team B)

Re:DBComboBox Items Problem


Quote
Don Sabelhaus wrote in message <3A8F4A68.2D900...@kuntrynet.com>...

>I'm populating the items of a DBCombobox with the below code. MemProof
>tells me I have some live pointers. I have reason to think they my be
>related to below code. Is it possible? Is there a better way of loading
>the items?

>procedure TfrmMain.dbcbIncSourceEnter(Sender: TObject);
>var
>  f:   TextFile;
>  ist: String;
>begin
>  ist := ExtractFilePath(ParamStr(0))+ 'IncSrc.txt';
>  if FileExists(ist) then
>    begin
>      with dbcbIncSource do
>        begin
>          Items.LoadFromFile(ExtractFilePath(ParamStr(0))+
>'IncSrc.txt');
>        end
>    end
>  else
>    AssignFile(f, ist);
>    ReWrite(f, ist);
>    CloseFile(f);
>end;

There's nothing here to cause memory problems, you should be fine. The
LoadFromFile method will correctly free any existing strings.

The only problem I see is you need a begin / end around the calls to
clear the file, as written above only the AssignFile is controlled by
the else statement.:

  else
    begin
      AssignFile(f, ist);
      ReWrite(f, ist);
      CloseFile(f);
    end;

Minor note: you could use your ist variable in the LoadFromFile call
instead of constructing the filename again, and I wonder why you want to
do this every time the combobox gets focus.

--
Wayne Niddery (WinWright Inc.)
RADBooks - http://members.home.net/wniddery/RADBooks/delphibooks.html
New morality: Drink at office party, stop at bar for more, crash and get
hurt on way home, sue employer, get rewarded $300K!

Re:DBComboBox Items Problem


Quote
>Don Sabelhaus wrote:

> I'm populating the items of a DBCombobox with the below code.

You only need the first part, no file I/O at all:

  var ist: String;

  ..
  ist := ExtractFilePath(ParamStr(0))+ 'IncSrc.txt';
    if FileExists(ist) then
      dbcbIncSource.Items.LoadFromFile(ist);  

Don't put this into the OnEnter event, since it gets triggered everytime
you enter the control.
________
Andreas
Delphi 5.01 Pro, NT4.0 Sp5
Direct replies mailto:p...@augsburg.netsurf.de

Re:DBComboBox Items Problem


Quote
"Wayne Niddery (TeamB)" wrote:
> The only problem I see is you need a begin / end around the calls to
> clear the file, as written above only the AssignFile is controlled by
> the else statement.:

>   else
>     begin
>       AssignFile(f, ist);
>       ReWrite(f, ist);
>       CloseFile(f);
>     end;

> Minor note: you could use your ist variable in the LoadFromFile call
> instead of constructing the filename again, and I wonder why you want to
> do this every time the combobox gets focus.

First to all who responded Thank You.

What I'm doing is giving a list of choices to choose from for that field, in
the onExit event I have code that writes new entries to the file if they
don't exist. Is there a better way to accomplish the same thing?

Re:DBComboBox Items Problem


Quote
Andreas Pawlik wrote:
>   ist := ExtractFilePath(ParamStr(0))+ 'IncSrc.txt';
>     if FileExists(ist) then
>       dbcbIncSource.Items.LoadFromFile(ist);

> Don't put this into the OnEnter event, since it gets triggered everytime
> you enter the control.

Thank you for your response.

I'm loading a list of choices for the field, in the onExit event I'm writing
new strings if they don't exist.

Where would a suitable place be to accomplish the same result?

Re:DBComboBox Items Problem


Quote
>Don Sabelhaus wrote:

>I'm loading a list of choices for the field, in the onExit event I'm >writing new strings if they don't exist.
>Where would a suitable place be to accomplish the same result?

Difficult to say without knowing your app, but it seems better to do
this in a separate routine. Where do the new strings come from?
________
Andreas
Delphi 5.01 Pro, NT4.0 Sp5
Direct replies mailto:p...@augsburg.netsurf.de

Re:DBComboBox Items Problem


Quote
Andreas Pawlik wrote:
> Difficult to say without knowing your app, but it seems better to do
> this in a separate routine. Where do the new strings come from?

In the onExit event of the DBComboBox I have the below code. I have 5  DBComboBox's in the application.

procedure TfrmMain.AddStrings(Sender: TDBComboBox);
var
  sta: string;  //Strings to add
begin
  sta := sender.Text;
  with sender as TDBComboBox do
    begin
      if (Length(sta) > 0) and (items.IndexOf(sta) = -1) then
        items.Add(sta);
          if sender = dbcbEPaidTo then
            items.SaveToFile('PdToItms.txt')
          else if sender = dbcbECatagory then
            items.SaveToFile('ECat.txt')
          else if sender = dbcbIncSource then
              items.SaveToFile('IncSrc.txt')
          else if sender = dbcbSSource then
              items.SaveToFile('SSource.txt')
          else if sender = dbcbSInstitution then
              items.SaveToFile('SInst.txt');
    end;
end;

Re:DBComboBox Items Problem


Quote
>Don Sabelhaus wrote:

>In the onExit event of the DBComboBox I have the below code. I have 5  >DBComboBox's in the application.

> procedure TfrmMain.AddStrings(Sender: TDBComboBox);

Since newly added DBComboBox items stay available as long as your app
runs, there seems to be no need to save to file constantly. I suggest
you fill the DBComboBoxes from the disk files at program startup (e.g.
in FormCreate), and write the items back to file in FormClose.
________
Andreas
Delphi 5.01 Pro, NT4.0 Sp5
Direct replies mailto:p...@augsburg.netsurf.de

Re:DBComboBox Items Problem


Quote
Andreas Pawlik wrote:
> Since newly added DBComboBox items stay available as long as your app
> runs, there seems to be no need to save to file constantly. I suggest
> you fill the DBComboBoxes from the disk files at program startup (e.g.
> in FormCreate), and write the items back to file in FormClose.

Thanks Andreas,
  I'll give it a try.

Other Threads