Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-17
8
Medium Priority
?
441 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 38

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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 38

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

730 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