Einzelnen Beitrag anzeigen

AlexII

Registriert seit: 28. Apr 2008
1.717 Beiträge
 
FreePascal / Lazarus
 
#1

Erstelle einen Wecker und brauche Hilfe

  Alt 25. Okt 2009, 14:07
Hallo,
ich erstelle nach einem Tutorial nen Wecker. Hab alles fertig, da wird allerdings nicht erklärt wie man beim klicken auf den Ok Button die Weckerzeit usw. übernimmt. Kann mir vllt da jemand weiter helfen? Hab die Forms als Screenshot, die zwei Units hier als Code:

Delphi-Quellcode:
unit Unit4; {Clock Alarms}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Buttons, XPMan, ExtCtrls, DateUtils, jpeg,
  AppEvnts;

type
  TForm4 = class(TForm)
    Image1: TImage;
    Image2: TImage;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    NewButton: TBitBtn;
    EditButton: TBitBtn;
    DeleteButton: TBitBtn;
    CloseButton: TBitBtn;
    AlarmListBox: TListBox;
    ApplicationEvents1: TApplicationEvents;
    Timer1: TTimer;
    procedure FormShow(Sender: TObject);
    procedure CloseButtonClick(Sender: TObject);
    procedure NewButtonClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure EditButtonClick(Sender: TObject);
    procedure DeleteButtonClick(Sender: TObject);
    procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form4: TForm4;

implementation

uses Unit1, Unit6;

{$R *.dfm}

procedure TForm4.FormShow(Sender: TObject);
begin
Image1.Picture:=Form1.Image3.Picture;
end;

procedure TForm4.CloseButtonClick(Sender: TObject);
begin
Close;
end;

procedure TForm4.NewButtonClick(Sender: TObject);
var
  Alarm: TAlarm;
begin
  Form6 := TForm6.Create(Self);
  try
    // ????????? ??????
    if Form6.ShowModal = mrOK then
    begin
      // ??????? ????? ?????? ??????????
      Alarm := TAlarm.Create;
      // ???????? ????????? ?????????? ?? ???????
      Form6.GetData(Alarm);
      // ???????? ????????? ? ?????? ? ??????? ???
      AlarmListBox.ItemIndex := AlarmListBox.Items.AddObject(
        Alarm.GetAlarmStr, Alarm);
    end;
  finally
    Form6.Free;
  end;

end;

procedure TForm4.FormDestroy(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to AlarmListBox.Items.Count - 1 do
    AlarmListBox.Items.Objects[I].Free;
end;

procedure TForm4.EditButtonClick(Sender: TObject);
var
  Alarm: TAlarm;
  SavedIndex: Integer;
begin
  Form6 := TForm6.Create(Self);
  try
    // ???????? ????????? ?????????
    with AlarmListBox do Alarm := TAlarm(Items.Objects[ItemIndex]);
    // ?????????? ??????????? ???????? ??????? ? ???????????? ?
    // ??????????? ??????????
    Form6.SetData(Alarm);
    // ????????? ??????
    if Form6.ShowModal = mrOK then
    begin
      // ???????? ?? ??????? ????? ????????? ??????????
      Form6.GetData(Alarm);
      with AlarmListBox do
      begin
        // ????????? ????? ?????????? ? ?????? ????????
        SavedIndex := ItemIndex;
        // ???????? ????? ????????
        // ??? ???? ??????? ????????? ???? ?????????
        Items.Strings[ItemIndex] := Alarm.GetAlarmStr;
        // ???????????? ????? ?????????? ? ?????? ????????
        ItemIndex := SavedIndex;
      end;
    end;
  finally
    Form6.Free;
  end;
end;


procedure TForm4.DeleteButtonClick(Sender: TObject);
begin
  with AlarmListBox do
  begin
    // ????????? ?????? ??????????
    Items.Objects[ItemIndex].Free;
    // ??????? ?? ?????? ??????????????? ??????? ??????
    Items.Delete(ItemIndex);
  end;
end;


procedure TForm4.ApplicationEvents1Idle(Sender: TObject;
  var Done: Boolean);
begin
  EditButton.Enabled := AlarmListBox.ItemIndex <> -1;
  DeleteButton.Enabled := AlarmListBox.ItemIndex <> -1;
  Done := True; // ????????????? ??????????? ????????? ??????? OnIdle
end;

procedure TForm4.Timer1Timer(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to AlarmListBox.Items.Count - 1 do
    with AlarmListBox.Items.Objects[I] as TAlarm do
      CheckTime;
end;

end.




Delphi-Quellcode:
unit Unit6; {Alarm Details}

interface

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

type
  TAlarm = class
  private
    Handled: Boolean;
  public
    MsgText: string;
    DateTime: TDateTime;
    PlaySound: Boolean;
    Recurring: Integer;
    function GetAlarmStr: string;
    procedure CheckTime;
  end;

type
  TForm6 = class(TForm)
    CloseBitBtn: TBitBtn;
    OkBitBtn: TBitBtn;
    Bevel1: TBevel;
    MessageEdit: TEdit;
    TimeMaskEdit: TMaskEdit;
    MessageLabel: TLabel;
    TimeLabel: TLabel;
    PlaySoundCheckBox: TCheckBox;
    RecurringGroupBox: TGroupBox;
    EverydayRadioButton: TRadioButton;
    WeeklyonRadioButton: TRadioButton;
    DateRadioButton: TRadioButton;
    WeeklyComboBox: TComboBox;
    DateTimePicker1: TDateTimePicker;
    procedure CloseBitBtnClick(Sender: TObject);
    procedure RecurringRadioButtonClick(Sender: TObject);
    procedure MessageEditChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    procedure GetData(Alarm: TAlarm);
    procedure SetData(Alarm: TAlarm);
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}

function TAlarm.GetAlarmStr: string;
begin
  Result := FormatDateTime('hh:mm ', DateTime) + MsgText;
end;

procedure TAlarm.CheckTime;
var
  Hour1, Min1, Sec1, MSec1: Word;
  Hour2, Min2, Sec2, MSec2: Word;
  Match: Boolean;
begin
  // ???????????? ??????? ?????
  DecodeTime(Time, Hour1, Min1, Sec1, MSec1);
  // ????????????? ??????? ????? ??????????
  DecodeTime(DateTime, Hour2, Min2, Sec2, MSec2);
  // ?????????, ??? ??????? ????? ????????? ? ???????? ??????????
  case Recurring of
    0: // ??? ?????????? ?????????????
      Match := (Hour1 = Hour2) and (Min1 = Min2);
    1..7: // ??? ???????????? ?????????????
      Match := (Hour1 = Hour2) and (Min1 = Min2) and
               (Recurring = DayOfWeek(Date));
    8: // ??? ?????????? ????
      Match := (Hour1 = Hour2) and (Min1 = Min2) and
               (Int(DateTime) = Date);
    else
      Match := False;
  end;
  // ?????? ?????? ? ?????? ??????? ???????????
  if Match then
  begin
    if not Handled then // ??????!
    begin
      Handled := True; // ????????????? ????????? ????????????
      if PlaySound then Beep;
      MessageDlg(GetAlarmStr, mtWarning, [mbOk], 0);
    end;
  end
  else
    Handled := False; // ?????????? ??????? ????????????
end;

procedure TForm6.GetData(Alarm: TAlarm);
begin
  with Alarm do
  begin
    // ???????? ?? ??????? ????? ????????? ??????????
    MsgText := MessageEdit.Text;
    // ???????? ?? ??????? ????? ???????????? ??????????
    DateTime := StrToTime(TimeMaskEdit.Text);
    // ???????? ?? ??????? ????????? ????????????? ?????
    PlaySound := PlaySoundCheckBox.Checked;
    // ???????? ?? ??????? ????????????? ???????????? ??????????
    if EverydayRadioButton.Checked then
      Recurring := 0
    else if WeeklyonRadioButton.Checked then
      Recurring := WeeklyComboBox.ItemIndex + 1
    else { DateRadioButton.Checked }
    begin
      Recurring := 8;
      DateTime := DateTimePicker1.Date + DateTime;
    end;
  end;
end;

procedure TForm6.SetData(Alarm: TAlarm);
begin
  with Alarm do
  begin
    // ?????????? ? ???? ??????? ????? ????????? ??????????
    MessageEdit.Text := MsgText;
    // ?????????? ? ???? ??????? ????? ??????????
    TimeMaskEdit.Text := FormatDateTime('hh:mm', DateTime);
    // ?????????? ? ???? ??????? ????????? ????????????? ?????
    PlaySoundCheckBox.Checked := PlaySound;
    // ?????????? ? ???? ??????? ????????????? ??????????
    case Recurring of
      0: // ?????????
        EverydayRadioButton.Checked := True;
      1..7: // ???????????
        begin
          WeeklyonRadioButton.Checked := True;
          WeeklyComboBox.ItemIndex := Recurring - 1;
        end;
      8: // ? ?????????? ????
        begin
          DateRadioButton.Checked := True;
          DateTimePicker1.Date := Int(DateTime);
        end;
    end;
  end;
end;

procedure TForm6.CloseBitBtnClick(Sender: TObject);
begin
Close;
end;

procedure TForm6.RecurringRadioButtonClick(Sender: TObject);
begin
  WeeklyComboBox.Enabled := Sender = WeeklyonRadioButton;
  DateTimePicker1.Enabled := Sender = DateRadioButton;
end;

procedure TForm6.MessageEditChange(Sender: TObject);
begin
OkBitBtn.Enabled := Length(MessageEdit.Text) <> 0;
end;

procedure TForm6.FormCreate(Sender: TObject);
begin
TimeMaskEdit.Text := FormatDateTime('hh:mm', Time);
WeeklyComboBox.ItemIndex := DayOfWeek(DateTimePicker1.Date) - 1;
DateTimePicker1.Date := Now;
end;

end.
Miniaturansicht angehängter Grafiken
screenshotwecker_169.jpg  
Bin Hobbyprogrammierer! Meine Fragen beziehen sich meistens auf Lazarus!
  Mit Zitat antworten Zitat