Solved

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

Posted on 2011-02-17
8
401 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
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:
ewangoya 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now