Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Delphi - Rubrica (Delphi 7)
Forum - Delphi - Rubrica (Delphi 7)

Avatar
camaleonteplus (Normal User)
Pro


Messaggi: 95
Iscritto: 05/03/2009

Segnala al moderatore
Postato alle 21:11
Venerdì, 20/05/2011
con un codice riuscivo a visualizzare in un label un nominativo associato ad un server, ad esempio mail.libero.it=infostrada, mail.libero.it compariva in un Tedit e infostrada in un label. Adesso questo codice l'ho modificato per ottenere lo stesso risultato anche per una rubrica ma non vuole funzionare. Posto il codice:
Codice sorgente - presumibilmente Delphi

  1. unit Email;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, ExtCtrls, RzPanel, StdCtrls, Buttons, Mask, ToolWin,
  8.   TLHelp32, Printers,
  9.   ComCtrls, ScktComp,
  10.   XPStyleActnCtrls, ActnList, ActnMan, Menus, RzDlgBtn, jpeg, StdActns, ExtActns,
  11.   RzButton, WinTypes, ShellAPI, mmSystem, Sockets, WinInet, RzShellDialogs,
  12.   RzRadGrp, RzEdit, RzStatus, Registry, RzLabel, RzDBLbl,
  13.   RzRadChk, StrUtils, RzCmboBx, RzSpnEdt;
  14.  
  15. type
  16.   TForm1 = class(TForm)
  17.     Lista: TRzComboBox;
  18.     Conta: TLabel;
  19.     GroupBox2: TGroupBox;
  20.     Label8: TLabel;
  21.     Server: TRzComboBox;
  22.     NomeServer: TLabel;
  23.     Pannello1: TPanel;
  24.     Contatto: TLabel;
  25.     Label1: TLabel;
  26.     Label2: TLabel;
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure ServerChangeClik(Sender: TObject);
  29.     procedure RubricaChangeClik(Sender: TObject);
  30.     procedure CaricaClick(Sender: TObject);
  31.  
  32.   private
  33.  
  34.   public
  35.     { Public declarations }
  36.     end;
  37.  
  38. var
  39.   Form1: TForm1;
  40.   slConfig: TStrings;
  41. const
  42.   CurrentState : byte = 1;
  43.  
  44. implementation
  45.  
  46. {$R *.dfm}
  47.  
  48. procedure TForm1.FormCreate(Sender: TObject);
  49. begin
  50. //Carica i contatti
  51.   CaricaClick(Sender);
  52.       end;
  53.  
  54. //Info Lista Server
  55. procedure TForm1.ServerChangeClik(Sender: TObject);
  56. begin
  57.  NomeServer.Caption := slConfig.Values[Server.Text];
  58. end;
  59.  
  60. //Info Lista Rubrica
  61. procedure TForm1.RubricaChangeClik(Sender: TObject);
  62. begin
  63.  Contatto.Caption := slConfig.Values[Lista.Text];
  64. end;
  65.  
  66. procedure TForm1.CaricaClick(Sender: TObject);
  67. var
  68. CLines, I : Integer;
  69. begin
  70.   slConfig := TStringList.Create;
  71.   slConfig.LoadFromFile('Rubrica.ini');
  72. for I := 0 to pred(slConfig.Count) do Lista.Items.Add(slConfig.Names[i]);
  73.   slConfig.LoadFromFile('Server.ini');
  74. for I := 0 to pred(slConfig.Count) do Server.Items.Add(slConfig.Names[i]);
  75.   CLines := 0;
  76. for I := 0 to  Lista.Items.Count - 1 do
  77. begin
  78.   CLines := CLines + 1;
  79. end;
  80.   Conta.Caption := 'Ci Sono '+ IntToStr(CLines) + ' Contatti!';
  81.   CLines := 0;
  82. for I := 0 to  Server.Items.Count - 1 do
  83. begin
  84.   CLines := CLines + 1;
  85. end;
  86.   Label1.Caption := 'Ci Sono '+ IntToStr(CLines) + ' Server!';
  87. end;
  88.  
  89. end.


Come posso procedere?

PM Quote
Avatar
Goblin (Member)
Expert


Messaggi: 375
Iscritto: 02/02/2011

Segnala al moderatore
Postato alle 0:48
Sabato, 21/05/2011
Mi spiace, ma non capisco tutta questa contorsione mentale....
Lista e server sono 2 combobox che riempi con dei valori prelevati da 2 file ini .. e sino a qui .... avrei qualcosa (molto :) ) da ridire sul codice, ma ... cmq il tuo problema è che usi una sola tstrings caricando dentro prima la rubrica.ini e poi server.ini e dalla stessa lista vuoi estrarre il contatto e il server, ma il contatto è ormai sovrascritto ... secondo me c'e' molta confusione ... e molto codice rindondante
Scrivi una funzione che passando il nome del fini .ini e del combo, carica i valori nel combo destinazione
scrivi una funzione che carica entrambe le liste le fonde e ne restituisce una unica
in questo modo risulta un po' più pulito e comprensibile

poi ... 8-|
G.


Ibis redibis non morieris in bello
PM Quote
Avatar
camaleonteplus (Normal User)
Pro


Messaggi: 95
Iscritto: 05/03/2009

Segnala al moderatore
Postato alle 15:43
Sabato, 21/05/2011
come potrei fare? come posso modificare il codice?

PM Quote
Avatar
Goblin (Member)
Expert


Messaggi: 375
Iscritto: 02/02/2011

Segnala al moderatore
Postato alle 0:53
Domenica, 22/05/2011
Butto giù qualcosa al volo, visto l'orario ... tenendo la stessa tua logica, ovviamente io non ho la visuale su tutti i problemi del tuo applicativo, ma una vista solo parziale del problema.
Ho optato per una singola funzione (Carica) che si prende cura di fare il lavoro di riempimento dei combo e della lista principale e nello stesso tempo restituisce il numero degli elementi presenti, in questo modo c'e' solo un punto delegato a questa azione, questo perchè se ci sarà bisogno di un terzo combo non bisogna triplicare il codice di caricamento, ma basta passare i parametri alla funzione e ci pensa lei. Volendo si potrebbe estremizzare anche la procedura di change, semplicemente assegnando un tag al combo e nell'evento change selezionare il combo prescelto per poi assegnare il valore alla label in base al Tag, forse è più difficile spiegarlo che scriverlo ;) cmq qualcosa del tipo:

procedure TForm1.ComboChange(Sender: TObject);
begin
  case (Sender as TcomboBox).Tag of
   1: NomeServer.Caption := oListaPublic.Values[(Sender as TcomboBox).Text];
   2: Contatto.Caption := oListaPublic.Values[(Sender as TcomboBox).Text];
  end;
end;

Entrambi gli eventi change puntano alla stessa procedura, in questo modo abbiamo centralizzato anche la procedura di assegnazione, in modo che nel caso di aggiunta di un terzo combo basta aggiungere una riga alla procedura combochange.
Più riusciamo a tenere il codice compatto e autoparlante più la manutenzione del nostro programma sarà facilitata in futuro...
NB: questa è solo un idea, ovviamente migliorabile
PS: il codice è testato 'al volo'

Codice sorgente - presumibilmente Delphi

  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Server: TComboBox;
  12.     Lista: TComboBox;
  13.     Label1: TLabel;
  14.     Conta: TLabel;
  15.     contatto: TLabel;
  16.     Nomeserver: TLabel;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure FormDestroy(Sender: TObject);
  19.     procedure ServerChange(Sender: TObject);
  20.     procedure ListaChange(Sender: TObject);
  21.   private
  22.     sPath: String;
  23.     Function Carica(oCombo: TComboBox; sFileName:String; Var oLista:TStringList): Integer;
  24.     { Private declarations }
  25.   public
  26.     oListaPublic: TStringList;
  27.     { Public declarations }
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.dfm}
  36.  
  37.  
  38. Function TForm1.Carica(oCombo: TComboBox; sFileName:String; Var oLista:TStringList): Integer;
  39. var I : Integer;
  40.     slConfig: TStringList;
  41. begin
  42.   Result := -1;
  43.   slConfig := TStringList.Create;
  44.   Try
  45.     slConfig.LoadFromFile(sFileName);
  46.     For I := 0 to pred(slConfig.Count) do
  47.        oCombo.Items.Add(slConfig.Names[i]);
  48.   finally
  49.     oLista. Text := oLista. Text + slConfig.Text;
  50.     Result := slConfig.Count;
  51.     slConfig.Free;
  52.   end;
  53. end;
  54.  
  55. procedure TForm1.FormCreate(Sender: TObject);
  56. begin
  57.   oListaPublic := TStringList.Create;
  58.   sPath:= ExtractFilePath(Application.ExeName);
  59.   Conta.Caption := 'Ci Sono '+ IntToStr(Carica(Lista, sPath + '\Rubrica.Ini', oListaPublic)) + ' Contatti!';
  60.   Label1.Caption := 'Ci Sono '+ IntToStr(Carica(Server,sPath + '\Server.Ini', oListaPublic)) + ' Server!';
  61. end;
  62.  
  63. procedure TForm1.FormDestroy(Sender: TObject);
  64. begin
  65.   oListaPublic.Free;
  66. end;
  67.  
  68. procedure TForm1.ServerChange(Sender: TObject);
  69. begin
  70.   NomeServer.Caption := oListaPublic.Values[Server.Text];
  71. end;
  72.  
  73. procedure TForm1.ListaChange(Sender: TObject);
  74. begin
  75.   Contatto.Caption := oListaPublic.Values[Lista.Text];
  76. end;
  77.  
  78. end.



Ibis redibis non morieris in bello
PM Quote
Avatar
camaleonteplus (Normal User)
Pro


Messaggi: 95
Iscritto: 05/03/2009

Segnala al moderatore
Postato alle 14:38
Lunedì, 23/05/2011
Ho fatto in questo modo:
Codice sorgente - presumibilmente Delphi

  1. unit Email;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, RzCmboBx, ExtCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Lista: TRzComboBox;
  12.     Conta: TLabel;
  13.     GroupBox2: TGroupBox;
  14.     Label8: TLabel;
  15.     Server: TRzComboBox;
  16.     NomeServer: TLabel;
  17.     Pannello1: TPanel;
  18.     Contatto: TLabel;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     procedure FormCreate(Sender: TObject);
  22.     procedure ServerChangeClik(Sender: TObject);
  23.     procedure RubricaChangeClik(Sender: TObject);
  24.     procedure FormDestroy(Sender: TObject);
  25.     procedure ComboChange(Sender: TObject);
  26.  
  27.   private
  28.   sPath: String;
  29.     Function Carica(oCombo: TRzComboBox; sFileName:String; Var oLista:TStringList): Integer;
  30.   public
  31.     oListaPublic: TStringList;
  32.     { Public declarations }
  33.     end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.  
  38. implementation
  39.  
  40. {$R *.dfm}
  41.  
  42. Function TForm1.Carica(oCombo: TComboBox; sFileName:String; Var oLista:TStringList): Integer;
  43. var I : Integer;
  44.     slConfig: TStringList;
  45. begin
  46.   Result := -1;
  47.   slConfig := TStringList.Create;
  48.   Try
  49.     slConfig.LoadFromFile(sFileName);
  50.     For I := 0 to pred(slConfig.Count) do
  51.        oCombo.Items.Add(slConfig.Names[i]);
  52.   finally
  53.     oLista. Text := oLista. Text + slConfig.Text;
  54.     Result := slConfig.Count;
  55.     slConfig.Free;
  56.   end;
  57. end;
  58.  
  59. procedure TForm1.FormCreate(Sender: TObject);
  60. begin
  61.   oListaPublic := TStringList.Create;
  62.   sPath:= ExtractFilePath(Application.ExeName);
  63.   Conta.Caption := 'Ci Sono '+ IntToStr(Carica(Lista, sPath + '\Rubrica.Ini', oListaPublic)) + ' Contatti!';
  64.   Label1.Caption := 'Ci Sono '+ IntToStr(Carica(Server,sPath + '\Server.Ini', oListaPublic)) + ' Server!';
  65. end;
  66.  
  67. procedure TForm1.FormDestroy(Sender: TObject);
  68. begin
  69.   oListaPublic.Free;
  70. end;
  71.  
  72. procedure TForm1.ComboChange(Sender: TObject);
  73. begin
  74.   case (Sender as TcomboBox).Tag of
  75.    1: NomeServer.Caption := oListaPublic.Values[(Sender as TcomboBox).Text];
  76.    2: Contatto.Caption := oListaPublic.Values[(Sender as TcomboBox).Text];
  77.   end;
  78. end;
  79.  
  80. end.


Mi da questo errore:
[Error] Email.pas(42): Declaration of 'Carica' differs from previous declaration
in questa riga:
Codice sorgente - presumibilmente Delphi

  1. Function TForm1.Carica(oCombo: TComboBox; sFileName:String; Var oLista:TStringList): Integer;


Perchè:-? e come posso risolvere:-?

PM Quote
Avatar
Goblin (Member)
Expert


Messaggi: 375
Iscritto: 02/02/2011

Segnala al moderatore
Postato alle 15:42
Lunedì, 23/05/2011
Guarda la dichiarazione della funzione carica nella sezione private ... e poi guarda come l'hai implementata ... sono 2 classi diverse ... da  una parte l'hai chiamata "TRzComboBox" l'altra invece è restata "TComboBox" uniforma con la classe giusta.
cmq... l'errore che ti ha dato delphi mi sembra molto esplicativo ;)

G.


Ibis redibis non morieris in bello
PM Quote
Avatar
camaleonteplus (Normal User)
Pro


Messaggi: 95
Iscritto: 05/03/2009

Segnala al moderatore
Postato alle 14:05
Martedì, 24/05/2011
Grazie dei tuoi consigli ho messo tutto a posto ecco il codice corretto:
Codice sorgente - presumibilmente Delphi

  1. unit Email;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, RzCmboBx, ExtCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Lista: TRzComboBox;
  12.     Conta: TLabel;
  13.     GroupBox2: TGroupBox;
  14.     Label8: TLabel;
  15.     Server: TRzComboBox;
  16.     NomeServer: TLabel;
  17.     Pannello1: TPanel;
  18.     Contatto: TLabel;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     procedure FormCreate(Sender: TObject);
  22.     procedure FormDestroy(Sender: TObject);
  23.     procedure ComboChange(Sender: TObject);
  24.     procedure ServerChange(Sender: TObject);
  25.     procedure ListaChange(Sender: TObject);
  26.  
  27.   private
  28.   sPath: String;
  29.     Function Carica(oCombo: TRzComboBox; sFileName:String; Var oLista:TStringList): Integer;
  30.   public
  31.     oListaPublic: TStringList;
  32.     { Public declarations }
  33.     end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.  
  38. implementation
  39.  
  40. {$R *.dfm}
  41.  
  42. Function TForm1.Carica(oCombo: TRzComboBox; sFileName:String; Var oLista:TStringList): Integer;
  43. var I : Integer;
  44.     slConfig: TStringList;
  45. begin
  46.   Result := -1;
  47.   slConfig := TStringList.Create;
  48.   Try
  49.     slConfig.LoadFromFile(sFileName);
  50.     For I := 0 to pred(slConfig.Count) do
  51.        oCombo.Items.Add(slConfig.Names[i]);
  52.   finally
  53.     oLista. Text := oLista. Text + slConfig.Text;
  54.     Result := slConfig.Count;
  55.     slConfig.Free;
  56.   end;
  57. end;
  58.  
  59. procedure TForm1.FormCreate(Sender: TObject);
  60. begin
  61.   oListaPublic := TStringList.Create;
  62.   sPath:= ExtractFilePath(Application.ExeName);
  63.   Conta.Caption := 'Ci Sono '+ IntToStr(Carica(Lista, sPath + '\Rubrica.Ini', oListaPublic)) + ' Contatti!';
  64.   Label1.Caption := 'Ci Sono '+ IntToStr(Carica(Server,sPath + '\Server.Ini', oListaPublic)) + ' Server!';
  65. end;
  66.  
  67. procedure TForm1.FormDestroy(Sender: TObject);
  68. begin
  69.   oListaPublic.Free;
  70. end;
  71.  
  72. procedure TForm1.ComboChange(Sender: TObject);
  73. begin
  74.   case (Sender as TRzComboBox).Tag of
  75.    1: NomeServer.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
  76.    2: Contatto.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
  77.   end;
  78. end;
  79.  
  80. procedure TForm1.ServerChange(Sender: TObject);
  81. begin
  82.   NomeServer.Caption := oListaPublic.Values[Server.Text];
  83. end;
  84.  
  85. procedure TForm1.ListaChange(Sender: TObject);
  86. begin
  87.   Contatto.Caption := oListaPublic.Values[Lista.Text];
  88. end;
  89.  
  90. end.


c'è una sola cosa che non va:
Codice sorgente - presumibilmente Delphi

  1. procedure TForm1.ComboChange(Sender: TObject);
  2. begin
  3.   case (Sender as TRzComboBox).Tag of
  4.    1: NomeServer.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
  5.    2: Contatto.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
  6.   end;
  7. end;


negli TLabel non compare niente viceversa se uso:
Codice sorgente - presumibilmente Delphi

  1. procedure TForm1.ServerChange(Sender: TObject);
  2. begin
  3.   NomeServer.Caption := oListaPublic.Values[Server.Text];
  4. end;
  5.  
  6. procedure TForm1.ListaChange(Sender: TObject);
  7. begin
  8.   Contatto.Caption := oListaPublic.Values[Lista.Text];
  9. end;


Funziona! La tua idea di questo codice:
Codice sorgente - presumibilmente Delphi

  1. procedure TForm1.ComboChange(Sender: TObject);
  2. begin
  3.   case (Sender as TRzComboBox).Tag of
  4.    1: NomeServer.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
  5.    2: Contatto.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
  6.   end;
  7. end;


Mi piace e la vorrei applicare mi daresti una mano a migliorarla?

PM Quote
Avatar
Goblin (Member)
Expert


Messaggi: 375
Iscritto: 02/02/2011

Segnala al moderatore
Postato alle 16:13
Martedì, 24/05/2011
Testo quotato

Postato originariamente da camaleonteplus:

Codice sorgente - presumibilmente Delphi

  1. procedure TForm1.ComboChange(Sender: TObject);
  2. begin
  3.   case (Sender as TRzComboBox).Tag of
  4.    1: NomeServer.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
  5.    2: Contatto.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
  6.   end;
  7. end;


Mi piace e la vorrei applicare mi daresti una mano a migliorarla?



Devi settare la property tag dei componenti combobox a 1 e a 2 dipende dal tipo, il combo del server avrà tag 1 e il combo del contatto avrà tag 2, entrambi fanno riferimento allo stesso evento ComboChange.
Infatti se leggi il case (case (Sender as TRzComboBox).Tag of) guarda il tag del componente se è 1 fai una cosa se è 2 fai un altra

Ultima modifica effettuata da Goblin il 24/05/2011 alle 16:18


Ibis redibis non morieris in bello
PM Quote
Avatar
camaleonteplus (Normal User)
Pro


Messaggi: 95
Iscritto: 05/03/2009

Segnala al moderatore
Postato alle 11:14
Mercoledì, 25/05/2011
Ok Funziona!!!!!
Grazie!!!!:k:

PM Quote