Supprimer bip d'erreur ?

cs_f6dqm1 Messages postés 62 Date d'inscription lundi 8 novembre 2004 Statut Membre Dernière intervention 25 mai 2013 - 12 févr. 2012 à 08:52
korgis Messages postés 420 Date d'inscription samedi 17 mai 2003 Statut Membre Dernière intervention 6 mai 2019 - 14 févr. 2012 à 14:25
Hello
Voici un petit problème :
Dans un contrôle EDIT de longueur fixe (par exemple un seul caractère ), si l'utilisateur essaie d'entrer un 2ème caractère, ça bip !
Est-il possible d'inhiber temporairement ce bip d'erreur qui interfère dans mon application avec d'autres sons ?
Peut-être faut-il aller déprogrammer le son d'erreur dans les propriétés son de windows mais je ne sais pas comment faire en Delphi.
Gabriel
WinXP, Delphi7

3 réponses

korgis Messages postés 420 Date d'inscription samedi 17 mai 2003 Statut Membre Dernière intervention 6 mai 2019 17
13 févr. 2012 à 21:55
Salut,

pourquoi ne pas utiliser l'évènement OnKeyPress de l'Edit, ainsi :

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if ((Length(Edit1.Text) = 1) and (Key <> #8)) and (Edit1.SelLength <> 1) then
    Key := #0;
end;


Explications :
1) Key := #0 // plus de son
2) Length(Edit1.Text) 1 // si longueur 1 caractère
3) Key <> #8 // touche "retour" autorisée
4) Edit1.SelLength <> 1 // possibilité de sélectionner pour remplacer ou effacer
5) ...voir si je n'oublie pas quelque possibilité qui m'échappe pour l'instant.

Tu peux même, a priori, dans ces conditions, ne pas affecter de valeur à Edit.MaxLength
0
korgis Messages postés 420 Date d'inscription samedi 17 mai 2003 Statut Membre Dernière intervention 6 mai 2019 17
14 févr. 2012 à 11:37
Bon, j'avais rien d'autre à faire...

uses Clipbrd;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
const
  AuthorizedKeys = #1;
begin
  with Edit1 do
  begin
    if ((Text '') or (SelLength 1)) and (Key = #22) and Clipboard.HasFormat(CF_TEXT) then
    begin
      Text := Clipboard.AsText[1];
      SelStart := 1;
      Key := #0;
    end
    else
    if ((Length(Text) = 1) and not(Key in AuthorizedKeys)) and (SelLength <> 1) then
      Key := #0;
  end;
end;


Si ça te va, tu me dis.
Si y'a un bug, tu dis aussi...
0
korgis Messages postés 420 Date d'inscription samedi 17 mai 2003 Statut Membre Dernière intervention 6 mai 2019 17
14 févr. 2012 à 14:25
Arggghhh...
Je viens de tester, comme ça en passant, y'a un bug.
J'ai oublié le "coller" avec le menu contextuel (clic droit).

Bon, il suffit de rajouter ces instructions dans l'évènement OnMouseDown de l'Edit :

if Clipboard.HasFormat(CF_TEXT) then
    Clipboard.AsText :=  Clipboard.AsText[1];


On récapépète reprend tout :

uses Clipbrd;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
const
  AuthorizedKeys  = #1;
begin
  with Edit1 do
  begin
    if ((Text '') or (SelLength 1)) and (Key = #22) and Clipboard.HasFormat(CF_TEXT) then
    begin
      Text := Clipboard.AsText[1];
      SelStart := 1;
      Key := #0;
    end
    else
    if ((Length(Text) = 1) and not(Key in AuthorizedKeys)) and (SelLength <> 1) then
      Key := #0;
  end;
end;

procedure TForm1.Edit1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Clipboard.HasFormat(CF_TEXT) then
    Clipboard.AsText := Clipboard.AsText[1];
end;


Ca me semble un poil redondant, mais ça fonctionne et j'y reviens plus, na.
0
Rejoignez-nous