Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetFocus/OnExit 2

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
If I have a SetFocus call to an edit box in a FormActivate, does the 'OnExit' event of that edit box get called?

I'm having to go through someone else's code and I don't have a clue how he got the program to do what it's doing!

Thanks!
leslie
 
yes, OnExit gets called. SetFocus doesn't do anything magical.

Feel free to post some code, or tell us what it's doing and we'll do our best to help.
 
Right now I just needed to know if the OnExit was called after the SetFocus....the programmer who built this is an RPG programmer and basically took RPG code and converted it to Delphi...there's no rhyme or reason to ANYTHING!!! I'd post the code, but then someone might think that I had written it and I don't want that kind of reputation!

Thanks for confirming!

Leslie
 
If I have a SetFocus call to an edit box in a FormActivate, does the 'OnExit' event of that edit box get called?"

No, the 'OnExit' event will fire when you leave the box not when you enter it.

Aaron
 
I presumed Leslie was referring to when the focus left the edit box after it had received the focus via SetFocus. I can see that the question is ambigious.
 
So here's what was happening, when stepping through the code, I would get to the bold section below and F7:

MainForm - OnActivate:
Code:
procedure TForm_MainPrompt.FormActivate(Sender: TObject);
begin
  StatusBar1.Visible:= False;
  Form_MainPrompt.Edit_CitationNumber.Text:= '';
  [b]Edit_CitationNumber.SetFocus;[/b]
.....
end;

and the next line of code I stop on is the red bold line below:
Code:
procedure TForm_MainPrompt.Edit_CitationNumberExit(Sender: TObject);
var
  sImagePath, agency, num, digit: string;
  agencyint, numint, digitint: integer;
begin
  firsttime:= False;
  citationonfile:= False;
  citationonfileMisd:= False;
  citationonfileTraffic:= False;
  processingimage:= False;
  processingimageUpdate:= False;
  processingimageMisd:= False;
  processingimageTraffic:= False;
  Form_CitationEntry.Panel3.visible:= False;
  Form_MisdemeanorEntry.Panel2.visible:= False;
  err:= 'N';
  Edit_CitationNumber.Color:= clWindow;

  if Edit_CitationNumber.Text = ''
  then Exit;

  DModule.TbCitation.Active:= False;
  DModule.TbCitation.Active:= True;

  if SaveOnce = False
  then savecitation:= Edit_CitationNumber.Text;

  SaveOnce:= True;
  // put Generic edits here
  // CitationCheck is filled in CitationEntryValidation from screen field
  CitationEntryValidation;
  if Err = 'Y'
  then Exit;

  if DModule.TbCitation.FindKey([Edit_CitationNumber.Text])
  then
    begin

      // this check in case duplicate cit numbers have been used by officers:
      Beep;

      if MessageDlg('Citation ' + Edit_CitationNumber.Text +
                    ' is already in the system.' + #13#13 +
                    ' Do you want to continue ?',
        mtConfirmation, [mbYes, mbNo], 0) <> mrYes
      then
        begin
          Form_MainPrompt.Edit_CitationNumber.SetFocus;
          Exit;
        end;
 
      if DModule.TbScannum.FindKey([Edit_CitationNumber.Text])
      then
        begin
          if DModule.TbImagePath.FindKey([DModule.TbScannumDOCUMENTID.Value])
          then
            begin
              processingimageUpdate:= True;

              if IsNumeric(Edit_CitationNumber.Text)
              then
                begin
                  processingimageTraffic:= True;
                  citationonfileTraffic:= True;
                  // Load traffic image into panel 3
                  Form_CitationEntry.Panel3.visible:= True;
                  sImagePath:= DModule.TbImagePathPATH.Value;

                  Try
                    Form_CitationEntry.Lead1.Load(sImagePath, 0, 0, 1);
                  Except
                    showmessage('Couldn''t find image');
                  End;

                  Form_CitationEntry.Edit_CitationNumber.Text:=
                    Edit_CitationNumber.Text;

                  // preset COMMVEH, etc.
                  PresetNewFields(DModule.TbCitation);

                  SetupTrafficImage;
                  StatusBar1.visible:= False;
                  Form_CitationEntry.Show;
                  Edit_CitationNumber.Text:= '';
                  savecitation:= '';
                  SaveOnce:= False;
                  Exit;
                end
              else
                begin
                  citationonfileMisd:= True;
                  processingimageMisd:= True;
                  // Load traffic image into panel 2
                  Form_MisdemeanorEntry.Panel2.visible:= True;
                  sImagePath:= DModule.TbImagePathPATH.Value;

                  Try
                    Form_MisdemeanorEntry.Lead1.Load(sImagePath, 0, 0, 1);
                  Except
                    showmessage('Couldn''t find image');
                  End;

                  Form_MisdemeanorEntry.Edit_CitationNumberMisd.Text:=
                    Edit_CitationNumber.Text;
                  SetupMisdemeanorImage;
                  StatusBar1.visible:= False;
                  Form_MisdemeanorEntry.Show;
                  Edit_CitationNumber.Text:= '';
                  savecitation:= '';
                  SaveOnce:= False;
                  Exit;
                end;
            end;
        end;

      if not DModule.TbScannum.FindKey([Edit_CitationNumber.Text])
      then
        begin
          if IsNumeric(Edit_CitationNumber.Text)
          then      // Traffic:
            begin
              citationonfileTraffic:= True;
              Form_CitationEntry.Edit_CitationNumber.Text:=
                Edit_CitationNumber.Text;
              // preset COMMVEH and other fields:
              PresetNewFields(DModule.TbCitation);
              Form_CitationEntry.Panel3.visible:= False;
              Form_CitationEntry.Showmodal;
              StatusBar1.visible:= False;
              Edit_CitationNumber.Text:= '';
              savecitation:= '';
              SaveOnce:= False;
              Exit;
            end
          else      // Misdemeanor:
            begin
              citationonfileMisd:= True;
              Form_MisdemeanorEntry.Panel2.visible:= False;
              Form_MisdemeanorEntry.Edit_CitationNumberMisd.Text:=
                Edit_CitationNumber.Text;
              Form_MisdemeanorEntry.Showmodal;
              StatusBar1.visible:= False;
              Edit_CitationNumber.Text:= '';
              Btn_FetchNextImage.SetFocus;
              savecitation:= '';
              SaveOnce:= False;
              Exit;
            end;
        end;
    end;

  // not on file
  // check if scanned
  if not DModule.TbCitation.FindKey([Edit_CitationNumber.Text])
  then
    begin
      Unit_Validate.CheckChgct(CitationCheck, err);

      if Err = 'Y'
      then
        begin
          ShowMessage('Citation Number Already Entered On A Case');
          Edit_CitationNumber.SetFocus;
          Exit;
        end;

      if IsNumeric(Edit_CitationNumber.Text)
      then
        begin
          Form_CitationEntry.Edit_CitationNumber.Text:= Edit_CitationNumber.Text;
          citationonfileTraffic:= False;
          // extract 3 parts of citation so you can look for a unprocessed image for 3
          // part citation number. check on the length

          if length(Edit_CitationNumber.Text) = 15
          then
            begin
              agency:= Copy(Edit_CitationNumber.Text, 1, 5);
              num:= Copy(Edit_CitationNumber.Text, 6, 9);
              digit:= Copy(Edit_CitationNumber.Text, 15, 1);
            end;

          if length(Edit_CitationNumber.Text) = 13
          then
            begin
              agency:= Copy(Edit_CitationNumber.Text, 1, 5);
              num:= Copy(Edit_CitationNumber.Text, 6, 7);
              digit:= Copy(Edit_CitationNumber.Text, 13, 1);
            end;

          agencyint:= StrToInt(agency);
          numint:= StrToInt(num);
          digitint:= StrToInt(digit);

          // Format the 3 fields in the file from the citnum field if needed
          if DModule.TbScannum.FindKey([Edit_CitationNumber.Text])
          then
            begin
              if Dmodule.TbScannumCOUNTYAGN.Value = 0
              then
                begin
                  imageid:= DModule.TbScannumDOCUMENTID.Value;

                  if DModule.TbAllimages.FindKey([imageid])
                  then
                    begin
                      if not (DModule.TbAllimages.state in [dsEdit])
                      then DModule.TbAllimages.Edit;

                      DModule.TbAllimagesCOUNTYAGN.Value:= agencyint;
                      DModule.TbALLimagesCITNBR9.Value:= numint;
                      DModule.TbAllimagesCHKDGT.Value:= digitint;
                      DModule.TbAllimages.Post;
                    end;
                end;
            end;

          if DModule.TbScannm3.FindKey([agencyint, numint, digitint])
          then
            begin
              // Check if already entered as 13 long since the 3 field will look the same.
              // for 13 or 15 long citations.
              if (DModule.TbScannm3STATUSCODE.Value = 1) and
                 (length(DModule.TbScannm3CITNUM.Value) = 13)
              then
                begin
                  if length(Edit_CitationNumber.Text) = 15
                  then
                    begin
                      ShowMessage('The Citation Was Entered w/o The 2 Zeros');
                      Edit_CitationNumber.SetFocus;
                      Edit_CitationNumber.Text:='';
                      Exit;
                    end;
                end;

          if (DModule.TbScannm3STATUSCODE.Value <> 5) and
             (DModule.TbScannm3STATUSCODE.Value <> 8)
          then
            begin
              processingimageTraffic:= True;
              CheckForOpenScannedImage;
              // preset COMMVEH and other fields:
              PresetNewFields(DModule.TbScannm3);
              Form_CitationEntry.Show;
              Edit_CitationNumber.Text:= '';
              savecitation:= '';
              SaveOnce:= False;
              Exit;
            end;

          if (DModule.TbScannm3STATUSCODE.Value = 5) or
             (DModule.TbScannm3STATUSCODE.Value = 8)
          then
            begin
              ShowMessage('Image/Citation Has Been Inactivated');
              Edit_CitationNumber.SetFocus;
              Edit_CitationNumber.Text:='';
              Exit;
            end;

        end;

      // extract 3 parts of citation so you can look for a unprocessed image for 3
      // part citation number. check on the length
      if length(savecitation) = 15
      then
        begin
          agency:= Copy(savecitation, 1, 5);
          num:= Copy(savecitation, 6, 9);
          digit:= Copy(savecitation, 15, 1);
        end;

      if length(savecitation) = 13
      then
        begin
          agency:= Copy(savecitation, 1, 5);
          num:= Copy(savecitation, 6, 7);
          digit:= Copy(savecitation, 13, 1);
        end;

      agencyint:= StrToInt(agency);
      numint:= StrToInt(num);
      digitint:= StrToInt(digit);
      // try original entry Savecitation
      if DModule.TbScannm3.FindKey([agencyint, numint, digitint])
      then
        begin
          if (DModule.TbScannm3STATUSCODE.Value <> 5) and
             (DModule.TbScannm3STATUSCODE.Value <> 8)
          then
            begin
              // from Edit_CitationNumber field
              if length(Edit_CitationNumber.Text)= 15
              then
                begin
                  agency:= Copy(Edit_CitationNumber.Text, 1, 5);
                  num:= Copy(Edit_CitationNumber.Text, 6, 9);
                  digit:= Copy(Edit_CitationNumber.Text, 15, 1);
                end;

              if length(Edit_CitationNumber.Text) = 13
              then
                begin
                  agency:= Copy(Edit_CitationNumber.Text, 1, 5);
                  num:= Copy(Edit_CitationNumber.Text, 6, 7);
                  digit:= Copy(Edit_CitationNumber.Text, 13, 1);
                end;

              agencyint:= StrToInt(agency);
              numint:= StrToInt(num);
              digitint:= StrToInt(digit);

              if not (DModule.TbScannm3.state in [dsEdit])
              then DModule.TbScannm3.Edit;

              { TODO : where do these get posted? }
              DModule.TbScannm3COUNTYAGN.Value:= agencyint;
              DModule.TbScannm3CITNBR9.Value:= numint;
              DModule.TbScannm3CHKDGT.Value:= digitint;
              processingimageTraffic:= True;
              CheckForOpenScannedImage;

              // preset COMMVEH and other fields:
              PresetNewFields(DModule.TbScannm3);

              Form_CitationEntry.Show;
              Edit_CitationNumber.Text:= '';
              savecitation:= '';
              SaveOnce:= False;
              Exit;
            end;

          if (DModule.TbScannm3STATUSCODE.Value = 5) or
             (DModule.TbScannm3STATUSCODE.Value = 8)
          then
            begin
              ShowMessage('Image/Citation Has Been Inactivated');
              Edit_CitationNumber.SetFocus;
              Edit_CitationNumber.Text:= '';
              Exit;
            end;

        end;

      // if different use update fields in cmpscancit:

      [b][COLOR=red]if not DModule.TbScannm3.FindKey([agencyint, numint, digitint])[/color][/b]
      then
        begin
          PresetToUnknown;    // Preset new fields to unknown.
          Form_CitationEntry.Showmodal;
          StatusBar1.visible:= False;
          Edit_CitationNumber.Text:= '';
          savecitation:= '';
          SaveOnce:= False;
          Exit;
        end;
    end;

    if not IsNumeric(Edit_CitationNumber.Text)
    then
      begin
        Form_MisdemeanorEntry.Edit_CitationNumberMisd.Text:=
          Edit_CitationNumber.Text;
        citationonfileMisd:= False;

        if DModule.TbScannum.FindKey([Edit_CitationNumber.Text])
        then
          begin
            processingimageMisd:= True;
            CheckForOpenScannedImage;
            Form_MisdemeanorEntry.Show;
            StatusBar1.visible:= False;
            Edit_CitationNumber.Text:= '';
            savecitation:= '';
            SaveOnce:= False;
            Exit;
          end
        else
          begin
            Form_MisdemeanorEntry.Showmodal;
            StatusBar1.visible:= False;
            Edit_CitationNumber.Text:= '';
            savecitation:= '';
            SaveOnce:= False;
            Exit;
          end;

      end;
    end;

  StatusBar1.visible:= False;
  Form_MainPrompt.Edit_CitationNumber.Text:= '';
  Btn_FetchNextImage.SetFocus;
end;

Again, please forgive the code above...I didn't write it and one of my other co-workers had this to say about it:
Good luck working with R’s code. There are huge blocks that never get executed, meaningless variable names, cut and paste instead of subroutines, and worst of all, no way to test except in production. It calls various unidentified RPG programs that don’t do anything you couldn’t do with a line or two of Delphi code. (I realize you already know this, but it’s fun to rant).

Luckily, I've been told that I don't need to do any more research into this issue because I can't make heads or tails of WTF is going on!!!

Thanks anyway!
Leslie
 
I don't know if there's any real good advice on dealing with such a thing. I encountered it all the time when I was working - there are many people that can code to produce a proper result, but produce an obfuscated mess no one can deal with afterwards.

Ultimately it's like a thousand monkeys on a thousand typewriters. Anyone with a small knowledge of programming will come up with something that works, but doesn't work with the programmer that comes after them. Code that works, but the programmer can't understand a line of it in terms of how it works (picking the logic flow out of the code). Of course, NO documentation.

What I had to do when I encountered that and was tasked with making a functional enhancement to it was to just start anew, keeping the old program around, to interpret function and to compare outputs with. Lots of time, trial, and error. And increased cost too, but that's the fault of the original programmer for producing unmaintainable garbage and not you for fixing it.
 
I try to keep my code littered with comments.
Especially about arrays that are passed between forms.
I do NOT intend to keep the job I have for the rest of my life (I'm 23) and do not want to leave for the next person what was left for me.

To be honest I mostly intend to go into IT type job after this. But I will keep coding as a hobby since I enjoy it.

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Sometimes it can be hard to tell if code has been poorly designed, or just poorly documented. When faced with an existing project for the first time, the myriad of function calls, often laid out in an unfamiliar way with unusual naming practices can lead to a lot of hair pulling.

Before embarking on a total rewrite, start from the top of the function and start commenting what each variable is used for by searching for where it's referenced and modified. Comment blocks of code for what purpose they serve. After a single method like the one you've shown us, I get the feeling you'll be in a position to know if the original programmer was insane, or just thought differently to you. And hopefully you'll begin to understand how that programmer thought and the rest of the project should become more self-explanatory all by itself.

If you understand the code and it's a horrible mess - then sure, think about a rewrite. I did that recently to one of my own projects that had gone through a horrible amount of feature creep and I was finding it hard to fix some bugs without causing more. The rewrite was wonderful. Clean, commented code that will handle new features with ease, better logging, etc. etc.
 
thanks for all the suggestions...we are in the middle of some major changes. We are converting from a poorly built custom case managment system (of which the above small portion of code can attest too!) to a canned system which seems to be a decent program. However, we have many specialized processes within the court and with outside agencies that will need to be converted. To top it off, the conversions will be in Java...so I'm having to decipher Delphi code and LEARN how to redo it in Java (since I don't know Java!).

I'm also trying to incorporate some of the more basic OOP principles (which have NEVER been used here and I don't think most people understand even the BASICS that I have a grasp of!).

I found a great set of articles on OOP programming using Delphi as the example language and now even more of the OOP techniques make sense. I wish I had found this paper a few years ago so that my programs would be better.

Leslie

In an open world there's no need for windows and gates
 
This is great, Leslie. It's worth mentioning that it also covers some design patterns, which you don't usually see covered in Delphi.
 
I once had the task of of explaining OOP to non-OOP coders. Even in my own experience of learning OOP, you can read and read until all of a sudden you just "get it". Some just didn't, until I broke it down to something really simple. I wrote a do-nothing program that started with a class called "TBug", with just 2 properties: segments and legs. Then TInsect = class(TBug). TInsect.create set legs:= 6; and segments:= 3; Then TButterfly = class(TInsect) which added property "Wings" and you get the idea.

By letting then single step through the code it became easy for them to see how the inheritance worked. Right away they were able to add their own classes for "TSpider" etc.

They all "got-it".

Roo
Delphi Rules!
 
I may steal that example for our staff meeting later this morning to try and help everyone else understand it's important to analyze the system and find all our "bugs" (pun intended!)

For example, one of my co-workers was assigned to create a function that would return "the six month date" of a case. It's an extremely complicated set of rules that determine when the rule runs and under what circumstances the rule is suspended.

What she created was a SixMonthDate Class. I personally feel that CalculateSixMonthRule would be a method of the Case Class, not a class all by itself.

Does that sound reasonable?
 
>Does that sound reasonable?

Yeah. One of the things some people have trouble with is the notion that a class has methods as well as properties (which, from the outside, appear similar.) Then you wind up with a bunch of one-method classes that used to be functions.

In your case (pun not intended) I think you have:

Case
Case.SixMonthDate (the calculation)
Case.UseSixMonthRule (boolean)

and maybe different properties/methods for the rules that comprise the UseSixMonthRule property.
 
I looked at the OOP documentation in the Turbo Pascal 7 manuals once upon a time and got the general concept. And I think it's generally relatable and logical to most as long as the real-life examples get used like was referenced earlier.

The problem for what I see comes in determining any distinct advantage (decreased time of development, decreased development cost, decreased complexity of code, increased ease of maintenance, etc) to using OOP over procedural coding. Personally, I still don't see any major practical advantages to OOP - I see all the opposites in the list of qualities I posted.

If OOP is so much better, that's really the hill you have to climb in selling it. Not the logical concept of it, but more practical concerns.
 
If OOP is so much better, that's really the hill you have to climb in selling it. Not the logical concept of it, but more practical concerns.
I disagree. For a lot of people (if not most) there's a big, conceptual disconnect. And that makes for a lot of bad OO code, which leads to development and maintenance problems and the associated costs.
 
And that makes for a lot of bad OO code, which leads to development and maintenance problems and the associated costs.

That's what I'm trying to prevent....if we continue on our current direction that's exactly what's going to happen. I'm hoping to put together some information to review with my boss and other staff members to show how important it is that we do this analysis and start off correctly so that we do gain from the benefits.

Roo-
Like I said above, I'm going to steal your example of the bug class. I'm going to put together a Class diagram to include with my information packet; yesterday's staff meeting was canceled so now I have a little time to come up with something more substantial. What methods do you think I could add to this model?

This has gone way off topic, but in such a way that I have learned a lot about another topic that I have been researching. Thanks for all the input.

Leslie
 
Thanks for the star, it was most unexpected since I was just contributing my 2 cents worth, and you're more than welcome to it!

Keep it as simple as possible. I had a form with a dropdown list of bugs and editboxes to show segments, legs and wings. OnSelect called a case statement that created the bug and filled the values in the editboxes. Again, tracing the creates in the IDE was the beauty of it.

I'll see if that W98 box will still boot. It might still be there under D4. If so, I'll post it for you.

I think this thread going "off-topic" could be beneficial to many forum readers.

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top