Oppure

Loading
20/05/11 19:11
camaleonteplus
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:
unit Email;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, RzPanel, StdCtrls, Buttons, Mask, ToolWin,
  TLHelp32, Printers,
  ComCtrls, ScktComp,
  XPStyleActnCtrls, ActnList, ActnMan, Menus, RzDlgBtn, jpeg, StdActns, ExtActns,
  RzButton, WinTypes, ShellAPI, mmSystem, Sockets, WinInet, RzShellDialogs,
  RzRadGrp, RzEdit, RzStatus, Registry, RzLabel, RzDBLbl,
  RzRadChk, StrUtils, RzCmboBx, RzSpnEdt;

type
  TForm1 = class(TForm)
    Lista: TRzComboBox;
    Conta: TLabel;
    GroupBox2: TGroupBox;
    Label8: TLabel;
    Server: TRzComboBox;
    NomeServer: TLabel;
    Pannello1: TPanel;
    Contatto: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure ServerChangeClik(Sender: TObject);
    procedure RubricaChangeClik(Sender: TObject);
    procedure CaricaClick(Sender: TObject);

  private

  public
    { Public declarations }
    end;

var
  Form1: TForm1;
  slConfig: TStrings;
const
  CurrentState : byte = 1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
//Carica i contatti
  CaricaClick(Sender);
      end;

//Info Lista Server
procedure TForm1.ServerChangeClik(Sender: TObject);
begin
 NomeServer.Caption := slConfig.Values[Server.Text];
end;

//Info Lista Rubrica
procedure TForm1.RubricaChangeClik(Sender: TObject);
begin
 Contatto.Caption := slConfig.Values[Lista.Text];
end;

procedure TForm1.CaricaClick(Sender: TObject);
var
CLines, I : Integer;
begin
  slConfig := TStringList.Create;
  slConfig.LoadFromFile('Rubrica.ini');
for I := 0 to pred(slConfig.Count) do Lista.Items.Add(slConfig.Names[i]);
  slConfig.LoadFromFile('Server.ini');
for I := 0 to pred(slConfig.Count) do Server.Items.Add(slConfig.Names[i]);
  CLines := 0;
for I := 0 to  Lista.Items.Count - 1 do
begin
  CLines := CLines + 1;
end;
  Conta.Caption := 'Ci Sono '+ IntToStr(CLines) + ' Contatti!';
  CLines := 0;
for I := 0 to  Server.Items.Count - 1 do
begin
  CLines := CLines + 1;
end;
  Label1.Caption := 'Ci Sono '+ IntToStr(CLines) + ' Server!';
end;

end.

Come posso procedere?
aaa
20/05/11 22:48
Goblin
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
21/05/11 13:43
camaleonteplus
come potrei fare? come posso modificare il codice?
aaa
21/05/11 22:53
Goblin
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'

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Server: TComboBox;
    Lista: TComboBox;
    Label1: TLabel;
    Conta: TLabel;
    contatto: TLabel;
    Nomeserver: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ServerChange(Sender: TObject);
    procedure ListaChange(Sender: TObject);
  private
    sPath: String;
    Function Carica(oCombo: TComboBox; sFileName:String; Var oLista:TStringList): Integer;
    { Private declarations }
  public
    oListaPublic: TStringList;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


Function TForm1.Carica(oCombo: TComboBox; sFileName:String; Var oLista:TStringList): Integer;
var I : Integer;
    slConfig: TStringList;
begin
  Result := -1;
  slConfig := TStringList.Create;
  Try
    slConfig.LoadFromFile(sFileName);
    For I := 0 to pred(slConfig.Count) do
       oCombo.Items.Add(slConfig.Names[i]);
  finally
    oLista. Text := oLista. Text + slConfig.Text;
    Result := slConfig.Count;
    slConfig.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  oListaPublic := TStringList.Create;
  sPath:= ExtractFilePath(Application.ExeName);
  Conta.Caption := 'Ci Sono '+ IntToStr(Carica(Lista, sPath + '\Rubrica.Ini', oListaPublic)) + ' Contatti!';
  Label1.Caption := 'Ci Sono '+ IntToStr(Carica(Server,sPath + '\Server.Ini', oListaPublic)) + ' Server!';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  oListaPublic.Free;
end;

procedure TForm1.ServerChange(Sender: TObject);
begin
  NomeServer.Caption := oListaPublic.Values[Server.Text];
end;

procedure TForm1.ListaChange(Sender: TObject);
begin
  Contatto.Caption := oListaPublic.Values[Lista.Text];
end;

end.

Ibis redibis non morieris in bello
23/05/11 12:38
camaleonteplus
Ho fatto in questo modo:
unit Email;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RzCmboBx, ExtCtrls;

type
  TForm1 = class(TForm)
    Lista: TRzComboBox;
    Conta: TLabel;
    GroupBox2: TGroupBox;
    Label8: TLabel;
    Server: TRzComboBox;
    NomeServer: TLabel;
    Pannello1: TPanel;
    Contatto: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure ServerChangeClik(Sender: TObject);
    procedure RubricaChangeClik(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ComboChange(Sender: TObject);

  private
  sPath: String;
    Function Carica(oCombo: TRzComboBox; sFileName:String; Var oLista:TStringList): Integer;
  public
    oListaPublic: TStringList;
    { Public declarations }
    end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Function TForm1.Carica(oCombo: TComboBox; sFileName:String; Var oLista:TStringList): Integer;
var I : Integer;
    slConfig: TStringList;
begin
  Result := -1;
  slConfig := TStringList.Create;
  Try
    slConfig.LoadFromFile(sFileName);
    For I := 0 to pred(slConfig.Count) do
       oCombo.Items.Add(slConfig.Names[i]);
  finally
    oLista. Text := oLista. Text + slConfig.Text;
    Result := slConfig.Count;
    slConfig.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  oListaPublic := TStringList.Create;
  sPath:= ExtractFilePath(Application.ExeName);
  Conta.Caption := 'Ci Sono '+ IntToStr(Carica(Lista, sPath + '\Rubrica.Ini', oListaPublic)) + ' Contatti!';
  Label1.Caption := 'Ci Sono '+ IntToStr(Carica(Server,sPath + '\Server.Ini', oListaPublic)) + ' Server!';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  oListaPublic.Free;
end;

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;

end.

Mi da questo errore:
[Error] Email.pas(42): Declaration of 'Carica' differs from previous declaration
in questa riga:
Function TForm1.Carica(oCombo: TComboBox; sFileName:String; Var oLista:TStringList): Integer;

Perchè:-? e come posso risolvere:-?
aaa
23/05/11 13:42
Goblin
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
24/05/11 12:05
camaleonteplus
Grazie dei tuoi consigli ho messo tutto a posto ecco il codice corretto:
unit Email;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RzCmboBx, ExtCtrls;

type
  TForm1 = class(TForm)
    Lista: TRzComboBox;
    Conta: TLabel;
    GroupBox2: TGroupBox;
    Label8: TLabel;
    Server: TRzComboBox;
    NomeServer: TLabel;
    Pannello1: TPanel;
    Contatto: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ComboChange(Sender: TObject);
    procedure ServerChange(Sender: TObject);
    procedure ListaChange(Sender: TObject);

  private
  sPath: String;
    Function Carica(oCombo: TRzComboBox; sFileName:String; Var oLista:TStringList): Integer;
  public
    oListaPublic: TStringList;
    { Public declarations }
    end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Function TForm1.Carica(oCombo: TRzComboBox; sFileName:String; Var oLista:TStringList): Integer;
var I : Integer;
    slConfig: TStringList;
begin
  Result := -1;
  slConfig := TStringList.Create;
  Try
    slConfig.LoadFromFile(sFileName);
    For I := 0 to pred(slConfig.Count) do
       oCombo.Items.Add(slConfig.Names[i]);
  finally
    oLista. Text := oLista. Text + slConfig.Text;
    Result := slConfig.Count;
    slConfig.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  oListaPublic := TStringList.Create;
  sPath:= ExtractFilePath(Application.ExeName);
  Conta.Caption := 'Ci Sono '+ IntToStr(Carica(Lista, sPath + '\Rubrica.Ini', oListaPublic)) + ' Contatti!';
  Label1.Caption := 'Ci Sono '+ IntToStr(Carica(Server,sPath + '\Server.Ini', oListaPublic)) + ' Server!';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  oListaPublic.Free;
end;

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

procedure TForm1.ServerChange(Sender: TObject);
begin
  NomeServer.Caption := oListaPublic.Values[Server.Text];
end;

procedure TForm1.ListaChange(Sender: TObject);
begin
  Contatto.Caption := oListaPublic.Values[Lista.Text];
end;

end.

c'è una sola cosa che non va:
procedure TForm1.ComboChange(Sender: TObject);
begin
  case (Sender as TRzComboBox).Tag of
   1: NomeServer.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
   2: Contatto.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
  end;
end;

negli TLabel non compare niente viceversa se uso:
procedure TForm1.ServerChange(Sender: TObject);
begin
  NomeServer.Caption := oListaPublic.Values[Server.Text];
end;

procedure TForm1.ListaChange(Sender: TObject);
begin
  Contatto.Caption := oListaPublic.Values[Lista.Text];
end;

Funziona! La tua idea di questo codice:
procedure TForm1.ComboChange(Sender: TObject);
begin
  case (Sender as TRzComboBox).Tag of
   1: NomeServer.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
   2: Contatto.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
  end;
end;

Mi piace e la vorrei applicare mi daresti una mano a migliorarla?
aaa
24/05/11 14:13
Goblin
Postato originariamente da camaleonteplus:

procedure TForm1.ComboChange(Sender: TObject);
begin
  case (Sender as TRzComboBox).Tag of
   1: NomeServer.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
   2: Contatto.Caption := oListaPublic.Values[(Sender as TRzComboBox).Text];
  end;
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 24/05/11 14:18
Ibis redibis non morieris in bello