Recent

Author Topic: [SOLVED] Can a component's procedure be called from another procedure?  (Read 8363 times)

Elmug

  • Hero Member
  • *****
  • Posts: 849
A procedure that belongs to a component and has the (Sender: TObject) portion in the declaration, I suppose, that it can not be called for use by code in another procedure?

I ask this because I tried to apply the procedure of an object, when I press a keyboard key, but NO GO.

However, since the button;s procedure was not long at all, I just copypasted the 3 or 4 lines of code into the place I wanted to use it, and it all worked.

But I am still interested in verifying if it is possible to do what I suppose can not be done.

That's because of coding rationale like not repeat code unless really necessary.

Thanks for any help on this, as usual. :)
« Last Edit: July 19, 2011, 12:41:57 am by Elmug »

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Can a component's procedure be called from another procedure?
« Reply #1 on: July 18, 2011, 01:13:20 pm »
Please show some code showing what you tried and didn't work.

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Can a component's procedure be called from another procedure?
« Reply #2 on: July 18, 2011, 03:06:50 pm »
Please show some code showing what you tried and didn't work.

In the single Unit of the project, after the implementation clause:

procedure TFormX.BtnEnterClick(Sender: TObject);
var
  XReal : Real;
  ErNo : Integer;
begin
  Val ( EntryDisplay.Text , XReal, ErNo);   // CONVERT TEXT STRING TO REAL
  DisplayX.Value := XReal ;                  // PASS THE REAL TO the Real CONTAINER DisplayX
    EntryDisplay.Text := '';                      // CLEAR THE TEdit BOX where numbers are keyboard pressed
 end;   


EntryDisplay is a TEdit box into which I key in numbers from the keyboard, so they  are a String.

When the button BtnEnter is clicked, its procedure converts the String into Real number and gives it to the DisplayX box, and that box is of the kind TFloatSpinEdit.

After the real is in the box DisplayX, the EntryDisplay.Text,  is CLEARED, by assigning to it the value '' (empty string).

Now, by another procedure, for reading characters from the keyboard (which is the same procedure that I use to enter the digits from the pc keyboard), when I press the Enter key (#13) I want to call the same procedure TFormX.BtnEnterClick(Sender: TObject) SO THAT EXACTLY the same code executes, in such a way that clicking the Enter button of the form, or Pressing the PC Enter Key are functionally the same.

So, I would like to know how to call the said procedure, as initially posted.

Thanks, I really hope it can be done!

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Can a component's procedure be called from another procedure?
« Reply #3 on: July 18, 2011, 03:13:54 pm »
Simply:
Code: [Select]
BtnEnterClick(nil);
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Can a component's procedure be called from another procedure?
« Reply #4 on: July 18, 2011, 03:31:58 pm »
Or:
Code: [Select]
BtnEnterClick(Sender);

if the procedure has a Sender parameter.
« Last Edit: July 18, 2011, 03:37:03 pm by typo »

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Can a component's procedure be called from another procedure?
« Reply #5 on: July 18, 2011, 04:08:30 pm »
Typo and Blazen,

I tried both of yours and YES, it can be called. Great!

However I still have TWO issues in that I don't get identical behavior (weird!).

The simple code to call the said procedure now is:
   if PCKey = #13 then  // So Enter key does same as button
            begin
                BtnEnterClick(nil);
            end;

                 
And ONE of the differences (seems to have to do with initialization): When I put numbers from the keyboard, and Click the forms' Enter Button, all happens as expected. PERFECT.

BUT, if I RESTART THE PROGRAM,  when I enter the numbers from the keyboard, they enter fine, but when I hit Enter key of the PC, I get a character "0" in the Entry Display, whereas when I do the entering with clicking on the Button, I do get a BLANK string (the entry is CLEARED).

This weird behavior is IDENTICAL to the one when instead of calling the procedure I had simply copy pasted and duplicated its code.

I have ANOTHER issue having to do with DIFFERENT results FROM pushing the form Enter Button and calling the procedure when PCKey = #13.

But I feel first see if this above can be solved first: Why a number "0" appears in the entery instead of a cleared field? That field is as said earlier a String TEdit type of box.

ALSO: Regardless of the order, once I have Clicked on the Form's Enter button, pressing the PC Enter Key, the entry box is also correctly cleared. (On the same session, without restarting the program)...BUT somtimes what is copied to the Real Display box seems to quickly be overwritten by zeroes, which never happens when I only use the Forms Entry button!

Thanks!

« Last Edit: July 18, 2011, 04:18:22 pm by Elmug »

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Can a component's procedure be called from another procedure?
« Reply #6 on: July 18, 2011, 04:24:40 pm »
Which event are you handling to get this "PCKey" ?

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Can a component's procedure be called from another procedure?
« Reply #7 on: July 18, 2011, 04:31:34 pm »
Your description is a little confusing for me.
Maybe solution is: When TButton is focused and you press Enter or Space Bar, it is the same as mouse click. Edit is erased ( Edit.Text:=''; ) and it is interpreted as 0.
So what component is focused when you have this problem ?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Can a component's procedure be called from another procedure?
« Reply #8 on: July 18, 2011, 04:48:51 pm »
Your description is a little confusing for me.
Maybe solution is: When TButton is focused and you press Enter or Space Bar, it is the same as mouse click. Edit is erased ( Edit.Text:=''; ) and it is interpreted as 0.
So what component is focused when you have this problem ?

GREAT OBSERVATION!!

I have checked on what you say, and YES, that's what seems to be happening. When I push a NUMBER button to enter number from the Form's digit buttons, it enters one character onto the TEdit, and I click the PC Enter and it keeps converting strings to numbers and passes them to the DisplayX correctly, but then after the display is cleared it puts the last focused number into the Entry-Display.

So how can I avoid this problem of the Enter or Space key being the same as mouse-click? I really need to use the Enter Key of the PC for entering ( and I also have plans for the Space Bar).

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Can a component's procedure be called from another procedure?
« Reply #9 on: July 18, 2011, 04:59:07 pm »
Quote
So how can I avoid this problem of the Enter or Space key being the same as mouse-click? I

Perhaps changing the ActiveControl property of the Form.

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Can a component's procedure be called from another procedure?
« Reply #10 on: July 18, 2011, 05:20:03 pm »
Quote
So how can I avoid this problem of the Enter or Space key being the same as mouse-click? I

Perhaps changing the ActiveControl property of the Form.

I look into that you say, Typo.

I appreciate all this help.


Elmug

  • Hero Member
  • *****
  • Posts: 849
Avoiding PC-Enter Key and PC-SpaceBar to act as mouse-click
« Reply #11 on: July 19, 2011, 12:41:29 am »
Quote
So how can I avoid this problem of the Enter or Space key being the same as mouse-click? I

Perhaps changing the ActiveControl property of the Form.

I look into that you say, Typo.

I appreciate all this help.

Hi Typo, everyone,

Tried all that was mentioned and more, and have JUST now resolved it by understanding a little more on how things behave, specially "focus".

I actually had to move the focus as the last part of every procedure for every button, and the focus moves to the EntryDisplay TEdit window.

Now, in the TEdit windows, I did already have code to handle the PC Enter Key, plus other filters. So even if the space bar sends whatever it wants to, and ditto for the PC Enter key, it all is destined to the EntryDisplay box, and its code handles it correctly.

I don't believe that there is a "general solution" now, other than somehow DE-ACTIVATING the fact that the SpaceKey and the PC-Enter-key work as if they were mouse clicks (which I personally feel is pretty bad thing to have, or at least there should be an easy way to defeat that behavior). I may open a new thread to see what others say on this.

Thanks everyone!!

All is great now.
« Last Edit: July 19, 2011, 03:01:46 am by Elmug »

 

TinyPortal © 2005-2018