Solved

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

Posted on 2011-02-17
8
396 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 36

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 36

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

13 Experts available now in Live!

Get 1:1 Help Now