Einzelnen Beitrag anzeigen

Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#24

AW: Memos und Buttons

  Alt 6. Apr 2013, 08:10
Ich hätte noch einen kleinen Vorschlag ...
Delphi-Quellcode:
program NotenAVG;

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

Type
  TNoteArray = Array [0 .. 15] of Integer;

  TWorker = Class
  private
    FNoteArray: TNoteArray;
    function GetAvg: Double;
  public
    procedure AddNote(Sender: TObject);
    procedure ListViewData(Sender: TObject; Item: TListItem);
  End;

var
  Worker: TWorker;
  NoteArray: TNoteArray;
  F: TForm;
  LV: TListView;
  i: Integer;

  { TWorker }

procedure TWorker.AddNote(Sender: TObject);
begin
  FNoteArray[TControl(Sender).Tag] := FNoteArray[TControl(Sender).Tag] + 1;
  LV.Invalidate;
end;

Function TWorker.GetAvg: Double;
var
  i, cnt: Integer;
begin
  cnt := 0;
  Result := 0;
  for i := 0 to 15 do
  begin
    cnt := cnt + FNoteArray[i];
    Result := Result + FNoteArray[i] * i;
  end;
  if cnt > 0 then
    Result := Result / cnt;
end;

procedure TWorker.ListViewData(Sender: TObject; Item: TListItem);
begin
  if Item.Index = 16 then
  begin
    Item.Caption := 'AVG';
    Item.SubItems.Add(FormatFloat('0.00', GetAvg));
  end
  else
  begin
    Item.Caption := Format('%d Punkte', [Item.Index]);
    Item.SubItems.Add(IntToStr(FNoteArray[Item.Index]));
  end;
end;

begin
  Worker := TWorker.Create;
  try
    F := TForm.Create(Application);
    F.ClientHeight := 16 * 24;
    F.ClientWidth := 230;
    F.DoubleBuffered := true;
    LV := TListView.Create(F);
    LV.Parent := F;
    LV.Width := 200;
    LV.ViewStyle := vsReport;
    LV.Font.Style := [fsBold];
    With LV.Columns.Add do
    begin
      Caption := 'Note';
      Width := 100;
    end;
    With LV.Columns.Add do
    begin
      Caption := 'Anzahl';
      Width := 96;
    end;
    LV.Align := alLeft;
    LV.OwnerData := true;
    LV.Items.Count := 17;
    LV.OnData := Worker.ListViewData;
    for i := 0 to 15 do
    begin
      With TButton.Create(F) do
      begin
        Parent := F;
        Tag := i;
        Left := 200;
        Height := 24;
        Width := 30;
        Top := i * Height;
        Caption := IntToStr(i);
        OnClick := Worker.AddNote;
      end;
    end;
    F.ShowModal;
  finally
    Worker.Free;
  end;

end.
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
  Mit Zitat antworten Zitat