Board index » delphi » hearing bells when i press Esc and Return

hearing bells when i press Esc and Return

Hi,

I have a dialog box with a Ok button as the Default and a Cancel button as the Cancel. I set these both to false when my listbox is visible (my listbox handles Esc and Return) and restore their values when I hide it. But whenever I press either ESC or Return with my listbox visible, I hear a bell ring. How can I disable the bell?

Thanks,
Lawrence

 

Re:hearing bells when i press Esc and Return


Quote
"Lawrence Tse" <l...@safe.com> wrote:
> But whenever I press either ESC or Return with my listbox
> visible, I hear a bell ring.

That's because but keys are by default invalid for that object.

Quote
> How can I disable the bell?

In this case, it's simpler to intercept the Key before it
reaches the object and change the Key value to zero to prevent
the bell.

Set the forms KeyPreview property to true and add an OnKeyDown
event to the form. In the OnKeyDown:

    if ActiveControl is <object name>
       if Key is VK_RETURN
         do what you want for key press enter
         set Key := 0;
       else if Key is VK_ESCAPE
         do what you want for ESC
         set Key := 0;

~ JD

Re:hearing bells when i press Esc and Return


Quote
In article <3e73e3f...@newsgroups.borland.com>, Lawrence Tse wrote:
> I have a dialog box with a Ok button as the Default and a Cancel button
> as the Cancel. I set these both to false when my listbox is visible
> (my listbox handles Esc and Return) and restore their values when
> I hide it. But whenever I press either ESC or Return with my listbox
>  visible, I hear a bell ring. How can I disable the bell?

Handle the OnKeyPress event of the listbox.

  If Key In [#13,#27] THen
    Key := #0;

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:hearing bells when i press Esc and Return


"Peter Below (TeamB)" <100113.1...@compuXXserve.com> wrote:

Quote
> Handle the OnKeyPress event of the listbox.

>  If Key In [#13,#27] THen
>    Key := #0;

Just as a quick test I dropped an Edit and a ListBox on a form.
Pressing Esc and Enter rang the bell for the Edit so I added
OnKeyDown to the edit and tested for ESC and Enter and set Key
to zero. The bell was still rung. I had to move up one level to
the forms OnKeyDown to silence the bell.

Ironically, the ListBox never made a sound which is what the
original post was all about.

~ JD

Re:hearing bells when i press Esc and Return


Quote
"Lawrence Tse" <l...@safe.com> wrote:
> How can I disable the bell?

After testing with the ListBox, it's not the object that's
ringing the bell. You'll need to determine which object(s) is
actually doing it.

If your dialog is a normal form with the BorderStyle set to
bsDialog then you can still use the forms OnKeyDown to
intercept the Key by testing if the ActiveControl is the object(s)
causing the bell to ring.

~ JD

Re:hearing bells when i press Esc and Return


Quote
"JD" <nos...@nospam.com> wrote in message

news:3e74d09b@newsgroups.borland.com...

Quote

> "Peter Below (TeamB)" <100113.1...@compuXXserve.com> wrote:
> > Handle the OnKeyPress event of the listbox.

> >  If Key In [#13,#27] THen
> >    Key := #0;

> Just as a quick test I dropped an Edit and a ListBox on a form.
> Pressing Esc and Enter rang the bell for the Edit so I added
> OnKeyDown to the edit and tested for ESC and Enter and set Key
> to zero. The bell was still rung.........

use OnKeyPRESS, as adviced, not OnKeyDown.
----
jacob

Re:hearing bells when i press Esc and Return


Quote
"jacob muntner" <jaco...@hotmail.co.il> wrote:
> use OnKeyPRESS, as adviced, not OnKeyDown.

Daaa !! Thanks for pointing that out to me. You have no idea
how often I've coded the forms OnKeyDown to handle this. I
think that when I was learning to use the compiler I just
assumed that since the OnKeyDown event fired first that this is
where one would need to do such things but it doesn't explain
to me why OnKeyPress works unless I had the order of operation
wrong in the first place!!!

~ JD

Other Threads