Board index » delphi » Accessing a TEdit Box in 2 ways - Student Q

Accessing a TEdit Box in 2 ways - Student Q

I'm a programming newbie on a crash course which has proceeded from Pascal
to Delphi in two short months.
I am currently working on a class project which involves the maintenance and
improvement of a programme written by last year's students on the same
course. They had to produce a calculator, modelled on the Windows
calculator, in Delphi.
My immediate task is to work on the interface, making it more attractive and
interactive. Last year's lads devised the calculator as taking input by
mouse only. The numbers are presented as TButtons arranged on panels -
different, overlapping panels for decimal, binary, hexadecimal and octal
representations. The system that I inherited reads the captions off the
buttons into a TEdit window, along these lines:
{///////////////}
procedure TForm1.ButtonClick(Sender: TObject);
begin
with panelChiffre do
begin if (ecran.text='0') or (operclic) then ecran.text:="; if
length(ecran.text)<20 then
ecran.text:=concat(ecran.text,Tbutton(sender).caption); end;
operclic:lse; calculfait:lse; end;
{///////////////////}
where "panelChiffre" is the name of the decimal panel (with similar
procedures for the other representation panels) and "ecran" is the name of
the TEdit box. Later, these concatenated strings are transtyped into numeric
values and read into variables.
What I want is to extend the possibilities for  the TEdit box "ecran" to
accept input by keyboard as well as from mouse-clicks on the number buttons.
I also want to add an Edit menu with the common CopyToClipboard and
PasteFromClipboard functions so that the calculator-results can be pasted
between different applications.
For some reason, the fact that the TEdit box is parametered to intercept
mouseclicks seems to preclude it accepting simple keyboard input, as by
default. Is there any way to tell it to do both, while accepting the
limitations on length and other specifications of the procedure for mouse
input given above?
Forgive me for what must be a really basic question. I'm a little at sea in
the vastnesses of Delphi and finding that nothing is ever as obvious as in
the classroom demonstrations!  : )
Many thanks to anyone who can help
Elisa Francesca Roselli
Universit Fran?ois Rabelais de Tours
 

Re:Accessing a TEdit Box in 2 ways - Student Q


Most likely (not having the whole application to examine) the problem is
that the FOCUS is not set to the EditBox, so the keystrokes are going
elsewhere.  It is possible to have all keystrokes directed to the main form
and intercept them there.  This would be good because the focus will tend
to shift from button to button as they are "clicked" with the mouse.  See
the "KeyPreview" property of TForm.  Another approach would be to
explicitly set the FOCUS to the EditBox after each key is "clicked" -- this
way any keystrokes will go to the EditBox.  Be sure to check the EditBox
properties to make sure that input is allowed -- ReadOnly is the property,
I believe.

Re:Accessing a TEdit Box in 2 ways - Student Q


In article <uUpqwmcK#GA....@nih2naab.prod2.compuserve.com>, "Elisa F Roselli"

Quote
<100144.2...@compuserve.com> writes:
>A problem I'm having now is in limiting the input when the calculator
>changes number base. For example, when the PanelBin number panel is loaded,
>the user could only send 0s and 1s to the display window via mouseclick, but
>with my modifications to accept keyboard input he can now send any gibberish
>he pleases. Last year's students apparently decided to limit input
>exclusively to mouse maneuvres as a security measure. In making the program
>more comfortable and interactive, I've also circumvented their security, and
>will have to figure out a way to put it back in. Referring to the Windows
>calculator as my model, I notice that when one is in a binary or octal
>number base and one attempts to enter a number that is not included in the
>base via keyboard, the display window does not take the input and the system
>returns the clinking sound that is associated with illegal actions.

Use sets of char in the OnKeyPress  handler, as the following example in which
I have made an array of character sets which are used as appropriate by setting
the value of a variable of an enumerated type corresponding to the base number.
The base is set with a group of radio buttons, but this example is just to show
you the principle.

Put a TRadioGroup on a form and name it NumberBaseRadGrp, Put a TEdit on the
form and leave it as default name of Edit1. Then put the  following code into
the appropriate clauses and eventhandlers of the form :-

uses
   MMSystem  // insert into top uses clause

type
   TBaseType = (btBin, btOct, btDec, btHex); // insert before TForm1
declaration

var
  BaseType : TBaseType;  // insert befoeimplementation

procedure TForm1.FormShow(Sender: TObject);
begin
  with NumberBaseRadGrp do begin
    SetBounds(25,15,95,100); // set position and size
    Caption := 'Number Base';
    {add radio buttons}
    Items.Add('Bin');
    Items.Add('Oct');
    Items.Add('Dec');
    Items.Add('Hex');
    ItemIndex := 2;  // set to Dec . . .
  end;
  BaseType := btDec; // . . . and corresponding base type
  with Edit1 do begin
    SetBounds(25, 135, 90, 21);
    Edit1.SetFocus;
  end;
end;

procedure TForm1.NumberBaseRadGrpClick(Sender: TObject);
{sets number base appropriate to radiobutton clicked}
begin
  {set base type value. Item index of 0 to 3 is cast to the
  enumerated base type to satisfy compiler}
  BaseType := TBaseType(NumberBaseRadGrp.ItemIndex);
  with Edit1 do begin
    Text := '';
    SetFocus;
  end;
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
const
  BaseKeySets : array[btBin..btHex] of set of char =
    ([#8, '0','1'], [#8, '0'..'7'], [#8, '0'..'9'], [#8, '0'..'9','A'..'F',
'a'..'f']);
    {#8 is backspace}
begin
  if not (Key in BaseKeySets[BaseType]) then begin
    Key := #0;  // for null effect
    PlaySound('Ding.wav', 0, SND_FILENAME or SND_ASYNC);
  end;
end;

Compile and run it

Alan Lloyd
alangll...@aol.com

Re:Accessing a TEdit Box in 2 ways - Student Q


Thank you so much, Alan, for showing me all of this. If I've been a while in
getting back to you, it is because I first had to "sit your class", studying
the code you posted in detail and tying it back in with my application in
progress. Each procedure is a whole lesson. It's up and running now, right
up to the dinging sound, and I'm ever so pleased with it.
While we're on the subject of trapping keystrokes, I should like to include
the tradtional CTRL+C, CTRL+V and CTRL+X keyboard alternatives for the Copy
Paste and Cut functions nowaccessible from the Edit menu. I expect, from
what I have been learning here, that these would be included in the
FormKeyPress procedure, along the "If key = '..' then" lines initially
suggeted by Sasan. But would you know offhand how to reference the CTRL key,
or what is the correct syntax for a function that involves pressing two keys
simultaneously?
Once again, many thanks for your helpful and informative posting.
Elisa Francesca Roselli
Quote
>Use sets of char in the OnKeyPress  handler, ........
>SNIPPED
>Compile and run it

>Alan Lloyd
>alangll...@aol.com

Re:Accessing a TEdit Box in 2 ways - Student Q


Quote
>   "Elisa F Roselli" <100144.2...@compuserve.com> writes:
>  While we're on the subject of trapping keystrokes, I should like to include
>  the tradtional CTRL+C, CTRL+V and CTRL+X keyboard alternatives for the Copy
>  Paste and Cut functions nowaccessible from the Edit menu.

If you already have Copy Paste and Cut on a menu why not set the shortcuts for these menu alternatives to the Ctrl+ alternatives you list. And you are all set without
having to write a lot of code.

I even have invisible menu items to catch several shortcuts for the same option.

johan

Re:Accessing a TEdit Box in 2 ways - Student Q


In article <uYDik$PL#GA....@ntawwabp.compuserve.com>, "Elisa F Roselli"

Quote
<100144.2...@compuserve.com> writes:
>While we're on the subject of trapping keystrokes, I should like to include
>the tradtional CTRL+C, CTRL+V and CTRL+X keyboard alternatives for the Copy
>Paste and Cut functions nowaccessible from the Edit menu

Delphi automatically supplies this functionality to a TEdit control. The
problem is to get rid of it <g>

Alan Lloyd
alangll...@aol.com

Re:Accessing a TEdit Box in 2 ways - Student Q


The simple answer is I don't know how. Any hints on setting a shortcut?

EFR

jo...@lindgren.pp.se a crit dans le message <367fa29...@d2o30.telia.com>...

Quote
>>   "Elisa F Roselli" <100144.2...@compuserve.com> writes:
>>  While we're on the subject of trapping keystrokes, I should like to
include
>>  the tradtional CTRL+C, CTRL+V and CTRL+X keyboard alternatives for the
Copy
>>  Paste and Cut functions nowaccessible from the Edit menu.
>If you already have Copy Paste and Cut on a menu why not set the shortcuts

for these menu alternatives to the Ctrl+ alternatives you list. And you are
all set without
Quote
>having to write a lot of code.

>I even have invisible menu items to catch several shortcuts for the same
option.

>johan

Re:Accessing a TEdit Box in 2 ways - Student Q


If it's automatically supplied, then last year's lads have apparently been
very clever at getting rid of it, as the TEdit display window 'Ecran' on the
calculator does not respond to these functions at all. However, I don't see
anything in the inherited code which seems specifically to preclude it. They
were just apparently very keen to limit value entry to mouse clicks on
buttons. Perhaps they were afraid of people llike me coming after them and
opening cans of worms all over the place : )

Thanks again, and Happy Holidays!

EFR

AlanGLLoyd a crit dans le message
<19981223022039.25439.00000...@ngol06.aol.com>...

Quote

>In article <uYDik$PL#GA....@ntawwabp.compuserve.com>, "Elisa F Roselli"
><100144.2...@compuserve.com> writes:

>>While we're on the subject of trapping keystrokes, I should like to
include
>>the tradtional CTRL+C, CTRL+V and CTRL+X keyboard alternatives for the
Copy
>>Paste and Cut functions nowaccessible from the Edit menu

>Delphi automatically supplies this functionality to a TEdit control. The
>problem is to get rid of it <g>

>Alan Lloyd
>alangll...@aol.com

Other Threads