Précédent   Forum du club des développeurs et IT Pro > Autres langages > Pascal > Free Pascal
Free Pascal Le compilateur Pascal multiplateforme
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 24/11/2012, 10h36   #1
Roland Chastain
Membre Expert
 
Homme Roland Chastain
Inscription : décembre 2011
Messages : 735
Détails du profil
Informations personnelles :
Nom : Homme Roland Chastain
Âge : 39
Localisation : Mali

Informations professionnelles :
Secteur : Enseignement

Informations forums :
Inscription : décembre 2011
Messages : 735
Points : 1 383
Points : 1 383
Par défaut Simple entrée utilisateur sous Turbo Vision

Bonjour !

J'ai travaillé sur un modèle simple de programme qui traite une chaîne entrée par l'utilisateur. Je propose ce que j'ai trouvé, en espérant que vous m'aiderez à l'améliorer.

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 
program InputBox_01;
 
uses
  Objects, Drivers, Views, Menus, Dialogs, App, MsgBox;
 
var
  s_entree: string;
 
type
  tA = object(tApplication)
         constructor Init;
         procedure NewDialog;
       end;
 
  pD = ^tD;
  tD = object(tDialog)
         procedure HandleEvent(var e: tEvent); virtual;
       end;
 
procedure Traitement;
begin
  MessageBox(#3 + 'Vous vous appelez ' + s_entree + '.',
             nil,
             mfInformation or mfOKButton);
end;
 
constructor tA.Init;
begin
  TApplication.Init;
  s_entree := '';
  NewDialog;
end;
 
procedure tA.NewDialog;
var
  v: pView;
  d: pD;
  r: tRect;
begin
  r.Assign(1, 1, 30, 8);
  d := New(pD, Init(r, 'Votre nom ?'));
  with d^ do
  begin
    r.Assign(03, 02, 26, 03);
    v := New(pInputLine, Init(r, 10));
    Insert(v);
    r.Assign(03, 04, 14, 06);
    Insert(New(pButton, Init(r, 'Ok', cmOk, bfNormal)));
    r.Assign(16, 04, 27, 06);
    Insert(New(pButton, Init(r, 'Cancel', cmCancel, bfNormal)));
  end;
  d^.SetData(s_entree);
  DeskTop^.Insert(d);
end;
 
procedure tD.HandleEvent(var e: tEvent);
begin
  tDialog.HandleEvent(e);
  if e.What = EvCommand then
    case e.Command of
      cmOK: begin
              GetData(s_entree);
              tDialog.Done;
              Traitement;
            end;
      cmCancel: tDialog.done;
    end
  else
    Exit;
  ClearEvent(e);
end;
 
var
  a: tA;
 
begin
  a.Init;
  a.Run;
  a.Done;
end.
Une chose par exemple que j'aimerais bien faire, c'est de pouvoir valider la saisie en appuyant sur Entrée. Toute autre suggestion, variante, amélioration bienvenue.

Une question en passant : Connaîtriez-vous un moyen de convertir les fichiers d'aide de Turbo Pascal dans un format plus accessible ?
__________________
L'Art est long et le Temps est court.
Roland Chastain est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/11/2012, 11h59   #2
M.Dlb
Rédacteur/Modérateur

 
Avatar de M.Dlb
 
Inscription : avril 2002
Messages : 2 278
Détails du profil
Informations personnelles :
Âge : 28

Informations forums :
Inscription : avril 2002
Messages : 2 278
Points : 3 434
Points : 3 434
Hello,

Il faut bien sûr surcharger HandleEvent:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
procedure TCommandInputLine.HandleEvent(var Event: TEvent);
var Command: String;
begin
  if ((Event.What = evKeyDown) and (Event.CharCode in CorrectChars)) then
  begin
    case Event.CharCode of
    #13 : begin
            Event.What := evCommand;
            Event.Command := cmExecuteCommand;
          end;
    #8  : begin
            GetData(Command);
            Command := Copy(Command, 0, Length(Command) - 1);
            SetData(Command);
            SelectAll(False);
            Event.What := evCommand;
            Event.Command := cmBuildCommandListBox;
          end
    else begin
           GetData(Command);
           Command := Command + Event.CharCode;
           SetData(Command);
           SelectAll(False);
           Event.What := evCommand;
           Event.Command := cmBuildCommandListBox;
         end;
    end;
    Event.InfoPtr := Nil;
    PutEvent(Event);
  end;
  if ((Event.What = evMouseDown) and (Event.Buttons = mbLeftButton) and (Event.Double)) then
  begin
    Event.What := evCommand;
    Event.Command := cmExecuteCommand;
  end;
  inherited HandleEvent(Event);
end;
Cet exemple est assez complet. Si l'utilisateur appuie sur Entrée (code 13), je recrée un évènement cmExecuteCommand (commande "user-defined") que je remets dans la file des messages (avec PutEvent). De même si l'utilisateur double clique sur la zone.

Tu remarques le inherited à la fin, qui est préférable au lieu de coder directement la méthode de l'ancêtre en dur.
__________________
M.Dlb - Modérateur z/OS - Rédacteur et Modérateur Pascal
M.Dlb est déconnecté   Envoyer un message privé Réponse avec citation 20
Vieux 24/11/2012, 17h32   #3
Roland Chastain
Membre Expert
 
Homme Roland Chastain
Inscription : décembre 2011
Messages : 735
Détails du profil
Informations personnelles :
Nom : Homme Roland Chastain
Âge : 39
Localisation : Mali

Informations professionnelles :
Secteur : Enseignement

Informations forums :
Inscription : décembre 2011
Messages : 735
Points : 1 383
Points : 1 383
Merci pour ta réponse et pour le code. Le problème de la touche Entrée est résolu.

Code :
1
2
3
4
5
6
type
  tA = object(tApplication)
         constructor Init;
         procedure NewDialog;
         procedure HandleEvent(var e: tEvent); virtual;
       end;
Code :
1
2
3
4
5
6
7
8
9
procedure tA.HandleEvent(var e: tEvent);
begin
  if (e.What = evKeyDown) and (e.CharCode = #13) then
  begin
    e.What := evCommand;
    e.Command := cmOK;
  end;
  inherited HandleEvent(e);
end;
__________________
L'Art est long et le Temps est court.
Roland Chastain est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 11h24.


 
 
 
 
Partenaires

Hébergement Web