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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close form on Enter 2

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I would like for my form to close when the user presses enter. I have tried the OnKeyDown, OnKeyPress and OnKeyUp to use

if Key = #13 then Self.Close;

but that doesn't work.

I know that I can have a button with the default := True, but I don't have any buttons on the form and don't want to add any.

If anyone knows why the above isn't working or something else to try, I'd appreciate it.

leslie
 
You didn't say whether you used .Show or .ShowModal to display your form.

If you used .ShowModal, all you have to do is set the ModalResult property of the button to mrOK (or mrCancel) as the case may be.

If you used .Show, all you need to do is process the Click event of the button something like this:
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Close;
end;
You can use the Close on Click with ShowModal too, if you want.

Caution, if the window was created by Delphi (auto-create) don't close it yourself, just hide it. Let Delphi close it at end-of-job.
 
except I don't have a button on the form and don't want a button on the form.

I just want the form to close when the enter button is pressed.

What I do is:

Open a JurorSearchInformation form. The user then enters an ID number, a Panel number or the SSN and selects the 'Search' button. If there is a matching juror, then I create the form:

Code:
function GetJurorInfo(AJurorNumber : string): boolean;
begin
  With dmJMS.qryJurorExists do
  begin
    SQL.Clear;
    SQL.Add('SELECT JURNUM FROM JMPMAIN WHERE ');
    If pos('P', AJurorNumber) > 0 then
      SQL.Add('PANLELID = ' + QuotedStr(AJurorNumber))
    else if (IsNumeric(AJurorNumber)) and (length(AJurorNumber) = 9) then
      SQL.Add('SSN = ' + QuotedStr(AJurorNumber))
    else
      SQL.Add('JURNUM = ' + AJurorNumber);
    Active := True;
    If isempty then
    begin
      Result := False;
      Active := False;
    end
    else begin
      With TfrmJurorInformation.Create(frmMain) do
      begin
        qryJurorInformation.SQL.Clear;
        qryJurorInformation.SQL.Add('SELECT * ' +
        ' FROM JMPMAIN WHERE JURNUM = ' + AJurorNumber);
        qryJurorInformation.Active := True;
        Caption := 'Juror Information ' + qryJurorInformation.FieldByName('FirstNAME').AsString + ' ' + qryJurorInformation.FieldByName('LastNAME').AsString;
        Show;
      end;
      Result := True;
    end;
  end;
end;

I want the JurorInformation form that is currently active on the desktop to close when the user presses 'ENTER'.

Thanks,
les
 
hi,

I made a demo with two forms. The first form has a button. In the onclick event of the button i show form2. Pressing enter on the second form closes the form2 returning to form1.

The only thing i did was putting the next code into the onKeypress event of from2:

procedure TForm2.FormKeyPress(Sender: TObject; var Key: Char);
begin
if key = #13 then
close;
end;

Press the button on form1, this activates form2 press enter and it closes form2.

If you have other components on form2 make sure that they donn't have the focus when you open form2. If so you also have to include the onKeypress event for that component. For example if you have a Teditcontrol on form2 make sure you have the onKeypress event also activated for that control, else it wonn't work.

Steph [Bigglasses]


 
just set the Keypreview property of your form to true and set the form onkeypress event that way, it doesn't matter how many components you have on your form, the form will receive the keypress event first....



 
Ok, finally got back to this! Setting the keypreview worked like a charm!

thanks!

les
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top