Solved

Why does my delphi application run through EditValueChange procedures on FormShow???

Posted on 2011-02-17
8
418 Views
Last Modified: 2012-05-11
I stepped through line by line and realized that on FormShow, my apps run through EditValueChange procedures... right when the dataset is opened. Is there a way to code it so that it doesnt do that?
0
Comment
Question by:Bianca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 34920367
do you debug with 2 monitors ?
1 monitor contains your app
the second monitor contains delphi debugging

if not, and delphi overlaps the app form, then a redraw takes place (or a formshow) to visualize the form

make sure your debugging doesn't cause form refreshes

if you don't what that happening ... don't put code in formshow
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 34920665

When the dataset is opened, the value of your edit is modified by the value of the filed in the dataset so it naturally triggers the ValueChangeEvent

You overcome this by setting a variable which indicates that the data is still being loaded

Add a private member eg

TForm1 = class(TForm)
......
private
   FDataLoading: Boolean;
....
end;

in the formshow procedure, set the dataloading to true

procedure TForm1.FormShow(Sender: TObject);
begin
  FDataLoading := True;
  try
    //initialize your stuff
   Dataset.Active := True;
  finally
    FDataLoading := False;
  end;
end;

in the EditValueChange, check for dataloading and exit
procedure TForm1.EditValueChange(Sender: TObject);
begin
  if FDataLoading then
    Exit;

  //do your other stuff
end;


0
 

Expert Comment

by:SleekProductions
ID: 34924146
Depending on the whole application one may also create all the DB Controls, connect them to Datasets and *then* set the Dataset.Active = true. Depending on DAL and visual controls that might even look better/be faster. An alternative is to activate the dataset *before* assigning it to the DB controls.
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!

 
LVL 21

Expert Comment

by:developmentguru
ID: 34925793
You will need to setup a background variable like:

  fInternalMod : boolean;

In your OnChange routine :

  if not fInternalMod then
    begin

      //your code here

    end;

When opening your dataset (or wherever you want to make changes without triggering the OnChange events)

  fInternalMod := true;
  try
    Dataset.Open;  //opening the dataset will call the OnChange but will not use the code...
    //your code here
  finally
    fInternalMod := false;
  end;

The alternative to this would be to disconnect the on change of all concerned controls before opening the dataset and reconnecting them all after.  This was should be a lot simpler and easier to maintain.  

If your case is this simple, this will work fine.  I have run into more complicated cases where the solution, while the same, would need to be more robust.  Let me know if you need more.
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 34925883
My last comment was almost the same as 34920665.  The more robust method would work like this.


protected
  fChangeBlockCount : integer;
  procedure DisableChangeEvents;
  procedure EnableChangeEvents;
  ...

procedure Form1.DisableChangeEvents;
begin
  inc(fChangeBlockCount);
end;


procedure Form1.EnableChangeEvents;
begin
  if fChangeBlockCount > 0 then
    dec(fChangeBlockCount);
end;

procedure Form1.Edit1OnChange(Sender : TObject);
begin
  if fChangeBlockCount = 0 then
    begin
      //Your code here.
    end;
end;

procedure TForm1.OpenDataset;
begin
  DisableChangeEvents;
  try
    //open datasets... your code here
  finally
    EnableChangeEvents;
  end;
end;

Open in new window

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 34925903
even better ... remote debugging

:)
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 34925913
One final note... you can also modify the last type I showed you to work when you need multiple sources to be able to stop access to any resource... making sure to update that resource once when they are all finished.  To do this you would modify the EnableChangeEvents like this:
procedure Form1.EnableChangeEvents;
begin
  if fChangeBlockCount > 0 then
    begin
      dec(fChangeBlockCount);
      if fChangeBlockCount = 0 then
        //Call your update (or on change) procedure(s) here.  only happens when all are finally unblocked.
    end;
end;

Open in new window

0
 
LVL 2

Expert Comment

by:RezaSadigh
ID: 35081969
Hi my friend,
Write this statement for the first line of the FormShow
  Edit1.OnChange:= nil;  // assume that your editor name is Edit1

and after opening DataSet write this
  Edit1.OnChange:= Edit1OnChange

by this trick you can suppress the event trough opening dataset and activate it again.
best regards
0

Featured Post

Technology Partners: 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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…

710 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