請問哪裡有錯 |
尚未結案
|
tea15
初階會員 ![]() ![]() 發表:64 回覆:72 積分:27 註冊:2005-06-15 發送簡訊給我 |
|
malanlk
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:20 回覆:694 積分:577 註冊:2004-04-19 發送簡訊給我 |
Delphi 的 case 只能以有序數(整數是一種)做引數, 不能以字串做引數 像你的 例子 可以用 TStringList 的 Sort 及 find 能力去做 fLst:= TStringList.Create; // 加到 OnFormCreate or OnFormOpen
fLst.Add('0101');
fLst.Add('0228');
....
R := Indate;
if fLst.Find(FormatDateTime('MMdd',Indate)) then
R := Indate-1;
Case DOW of
6 : R := R-1;
7 : R := R-2;
1 : R := R-3;
end;
.... fLst.Free; // 記得找地方釋放ㄚ OnFormClose or OnFormDestory 您是什麼行業啊? 禮拜ㄧ也不用辦事喔...
|
tea15
初階會員 ![]() ![]() 發表:64 回覆:72 積分:27 註冊:2005-06-15 發送簡訊給我 |
malanlk你好..
我不太懂你的程式耶!!
fLst是什麼呀?
我不懂TStringList是啥米東西耶!
可以跟我說一下嗎?
引言: Delphi 的 case 只能以有序數(整數是一種)做引數, 不能以字串做引數 像你的 例子 可以用 TStringList 的 Sort 及 find 能力去做 fLst:= TStringList.Create; // 加到 OnFormCreate or OnFormOpen fLst.Add('0101'); fLst.Add('0228'); .... R := Indate; if fLst.Find(FormatDateTime('MMdd',Indate)) then R := Indate-1; Case DOW of 6 : R := R-1; 7 : R := R-2; 1 : R := R-3; end; .... fLst.Free; // 記得找地方釋放ㄚ OnFormClose or OnFormDestory 您是什麼行業啊? 禮拜ㄧ也不用辦事喔... |
malanlk
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:20 回覆:694 積分:577 註冊:2004-04-19 發送簡訊給我 |
fLst 是 TStringList 類別的變數 TStringList 用法請看 Help 請宣告在 priviate 區裡, fLst: TStringList; 然後在 FormCreate 時
fLst:= TStringList.Create; // 建立 fLst
fLst.Add('0101'); // 加入ㄧ筆字串 內容是 '0101'
fLst.Add('0228'); // 再加入ㄧ筆字串 內容是 '0228'
fLst.Sorted := True; // 將日期排序
.... 把所有國定假日加入 紅色這行忘了加, 有加, 後面 Find 才會快...
=============================================
在 FormDestory 中加入
fLst.Free; // 釋放 fLst
=============================================
R := Indate; // FormatDateTime('MMdd',Indate) 取出Indate的月份日期 // fLst.Find('0101') 在 fLst 字串列表中找找看有沒有 '0101' 這個字串 // 所以合在一起就是 請在 fLst串列中找一下是否有字串和Indate的月份日期一樣 if fLst.Find(FormatDateTime('MMdd',Indate)) then R := Indate-1; // 如果找到 就是國定假日, 先扣ㄧ天再說 Case DOW of // 繼續比對 是不是..... 6 : R := R-1; 7 : R := R-2; 1 : R := R-3; // 週一的話就改成週五, 為什麼週一不辦公? end; |
tea15
初階會員 ![]() ![]() 發表:64 回覆:72 積分:27 註冊:2005-06-15 發送簡訊給我 |
你好..
我這個程式主要是在寫「繳款日期」..
所以必須在前一天去繳,但又要跳過假日...
所以若繳款日為星期一,就必須在星期五去繳款..
而我使用了你下面那段程式..
其中一行會出現錯誤.. if fLst.Find(FormatDateTime('MMdd',Indate)) then 但我找不出來哪裡錯耶!!
引言: fLst 是 TStringList 類別的變數 TStringList 用法請看 Help 請宣告在 priviate 區裡, fLst: TStringList; 然後在 FormCreate 時 fLst:= TStringList.Create; // 建立 fLst fLst.Add('0101'); // 加入ㄧ筆字串 內容是 '0101' fLst.Add('0228'); // 再加入ㄧ筆字串 內容是 '0228' fLst.Sorted := True; // 將日期排序 .... 把所有國定假日加入 紅色這行忘了加, 有加, 後面 Find 才會快... ============================================= 在 FormDestory 中加入 fLst.Free; // 釋放 fLst =============================================R := Indate; // FormatDateTime('MMdd',Indate) 取出Indate的月份日期 // fLst.Find('0101') 在 fLst 字串列表中找找看有沒有 '0101' 這個字串 // 所以合在一起就是 請在 fLst串列中找一下是否有字串和Indate的月份日期一樣 if fLst.Find(FormatDateTime('MMdd',Indate)) then R := Indate-1; // 如果找到 就是國定假日, 先扣ㄧ天再說 Case DOW of // 繼續比對 是不是..... 6 : R := R-1; 7 : R := R-2; 1 : R := R-3; // 週一的話就改成週五, 為什麼週一不辦公? end; |
malanlk
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:20 回覆:694 積分:577 註冊:2004-04-19 發送簡訊給我 |
|
tea15
初階會員 ![]() ![]() 發表:64 回覆:72 積分:27 註冊:2005-06-15 發送簡訊給我 |
你們好..
我一真想不透哪裡出錯…請你們幫幫忙..
以下是我的程式碼
private
{ Private declarations }
d:integer;
e,f:string;
a:word;
Function MyWorkDate(InDate : TDate) : TDate;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
datetimepicker1.Date:=EncodeDate(strtoint(copy(edit1.Text,1,4)),strtoint(copy(edit1.Text,5,2)),strtoint(copy(edit1.Text,7,2)));
Edit2.Text:=DateToStr(MyWorkDate(DateTimePicker1.Date));
e:=datetostr(datetimepicker1.Date);
f:=copy(e,5,4);
end;
Function Tform1.MyWorkDate(InDate : TDate) : TDate;
var
DOW :word;
R : TDate;
i,j:integer;
x:array of string;
begin
x[0]:='0101';
x[1]:='0228';
R := InDate;
DOW := DayOfTheWeek(R);
if RadioButton1.Checked then
for i:=0 to 2 do
begin
if f=x[i] then
R := indate-1;
end;
begin
Case DOW of
6 : R := InDate -1;
7 : R := InDate -2;
1 : R := InDate -3;
else
R := InDate-1;
end;
end;
label1.caption := '星期'+IntToStr(DayOfTheWeek(R));
Result := R;
end; 可是在執行時,在這一行,卻一直出現這個錯誤
Edit2.Text:=DateToStr(MyWorkDate(DateTimePicker1.Date));
|
malanlk
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:20 回覆:694 積分:577 註冊:2004-04-19 發送簡訊給我 |
|
tea15
初階會員 ![]() ![]() 發表:64 回覆:72 積分:27 註冊:2005-06-15 發送簡訊給我 |
但是…為什麼會在edit2那一行出錯呢??
那我若把for i:=0 to 10 do
意思是讓它的迴圈數增大,這樣子還會出現錯誤嗎?
還是我應該如何修改?
引言: x:array of string; // 你用動態配置 x[0]:='0101'; x[1]:='0228'; Delphi 依你設定會幫你配兩個字串空間 R := InDate; DOW := DayOfTheWeek(R); if RadioButton1.Checked then for i:=0 to 2 do // 你的迴圈跑了 3 次 i=2 時就出狀況了 begin |
malanlk
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:20 回覆:694 積分:577 註冊:2004-04-19 發送簡訊給我 |
|
tea15
初階會員 ![]() ![]() 發表:64 回覆:72 積分:27 註冊:2005-06-15 發送簡訊給我 |
|
malanlk
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:20 回覆:694 積分:577 註冊:2004-04-19 發送簡訊給我 |
|
tea15
初階會員 ![]() ![]() 發表:64 回覆:72 積分:27 註冊:2005-06-15 發送簡訊給我 |
|
malanlk
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:20 回覆:694 積分:577 註冊:2004-04-19 發送簡訊給我 |
程式1
Function Tform1.MyWorkDate(InDate : TDate) : TDate; var i,Idx:integer; DOW :word; R : TDate; begin R := InDate; DOW := DayOfTheWeek(R); if RadioButton1.Checked then begin Case DOW of 6 : R := InDate -1; //變成本周星期五 7 : R := InDate -2; //變成本周星期五 1 : R := InDate -3; //變成本周星期五 else R := InDate-1; //都向後推一天 end; end; label1.caption := '星期' IntToStr(DayOfTheWeek(R)); if fLst.Find(FormatDateTime('MMdd',Indate), Idx) then R := Indate-1; DOW := DayOfTheWeek(R); Case DOW of 6 : R := R-1; 7 : R := R-2; 1 : R := R-3; end; label1.caption := '星期' IntToStr(DayOfTheWeek(R)); Result := R; end;程式2 f:=copy(e,5,6); if (f='/01/01') or (f='/02/28') then |
tea15
初階會員 ![]() ![]() 發表:64 回覆:72 積分:27 註冊:2005-06-15 發送簡訊給我 |
malanlk你好..
我做了一些修改,程式如下:
private
{ Private declarations }
f,t:string;
Function MyWorkDate(InDate : TDate) : TDate;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
datetimepicker1.Date:=EncodeDate(strtoint(copy(edit1.Text,1,4)),strtoint(copy(edit1.Text,5,2)),strtoint(copy(edit1.Text,7,2)));
f:=DateToStr(MyWorkDate(DateTimePicker1.Date));
t:=copy(f,5,6);
if radiobutton1.Checked then
begin
if (t='/01/01') or (t='/02/28') or (t='/04/05')or (t='/10/10') then
edit1.Text:=datetostr(myworkdate(datetimepicker1.Date-1))
else
edit2.Text:=datetostr(myworkdate(datetimepicker1.Date))
end
else if radiobutton2.Checked then
begin
if (t='/01/01') or (t='/02/28') or (t='/04/05')or (t='/10/10') then
edit2.Text:=datetostr(myworkdate(datetimepicker1.Date+1))
else
edit2.Text:=datetostr(myworkdate(datetimepicker1.Date))
end
end;
Function Tform1.MyWorkDate(InDate : TDate) : TDate;
var
DOW :word;
R : TDate;
begin
R := InDate;
DOW := DayOfTheWeek(R);
if RadioButton1.Checked then
begin
Case DOW of
6 : R := R -1; //變成本周星期五
7 : R := R -2; //變成本周星期五
1 : R := R -3; //變成本周星期五
else
R := R-1; //都向後推一天
end;
end;
if RadioButton2.Checked then
begin
Case DOW of
5 : R := R +3; //變成下一周星期一
6 : R := R +2; //變成下一周星期一
7 : R := R +1; //變成下一周星期一
else
R := R+1; //都向前推一天
end;
end;
label1.caption := '星期'+IntToStr(DayOfTheWeek(R));
Result := R;
end; 我的radiobutton1是「繳款日」的功能,表示要提早一天。
radiobutton2是「兌現日」的功能,表示會隔一天。
那麼,當我輸入'20050226'時,卻出現的是'20050228'
由於0228這一天必須跳掉,可是在「兌現日」卻出現這一個日期..
我不知道該從何下手去修改耶~
|
malanlk
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:20 回覆:694 積分:577 註冊:2004-04-19 發送簡訊給我 |
unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, DateUtils; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Label3: TLabel; DateTimePicker1: TDateTimePicker; Label1: TLabel; Button2: TButton; Edit2: TEdit; RadioButton1: TRadioButton; Label2: TLabel; RadioButton2: TRadioButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } d,i:integer; e,f,s,w:string; fLst: TStringList; function MyWorkDate(InDate : TDate) : TDate; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin datetimepicker1.Date:=EncodeDate(strtoint(copy(edit1.Text,1,4)),strtoint(copy(edit1.Text,5,2)),strtoint(copy(edit1.Text,7,2))); Edit2.Text:=DateToStr(MyWorkDate(DateTimePicker1.Date)); e:=datetostr(datetimepicker1.Date); f:=copy(e,5,4); label3.Caption:=s; end; function TForm1.MyWorkDate(InDate : TDate) : TDate; var Idx: Integer; R: TDate; function IsHoliday(CheckDate : TDate): Boolean; begin if RadioButton1.Checked then begin Result := (DayOfTheWeek(CheckDate) in [6,7,1]); Result := Result or fLst.Find(FormatDateTime('MMdd',CheckDate), Idx); end else if RadioButton2.Checked then begin Result := (DayOfTheWeek(CheckDate) in [6,7,5]); Result := Result or fLst.Find(FormatDateTime('MMdd',CheckDate), Idx); end else Result := False; end; begin R := InDate; while IsHoliday(R) do begin if RadioButton1.Checked then begin if fLst.Find(FormatDateTime('MMdd',R), Idx) then R := R-1; case DayOfTheWeek(R) of 6 : R := R-1; 7 : R := R-2; 1 : R := R-3; end; end else if RadioButton2.Checked then begin if fLst.Find(FormatDateTime('MMdd',R), Idx) then R := R 1; case DayOfTheWeek(R) of 5 : R := R 3; 6 : R := R 2; 7 : R := R 1; end; end end; label1.caption := '星期' IntToStr(DayOfTheWeek(R)); Result := R; end; procedure TForm1.Button2Click(Sender: TObject); begin application.Terminate; end; procedure TForm1.FormCreate(Sender: TObject); begin fLst:= TStringList.Create; // 建立 fLst fLst.Add('0101'); // 加入ㄧ筆字串 內容是 '0101' fLst.Add('0228'); // 再加入ㄧ筆字串 內容是 '0228' fLst.Add('0405'); // 再加入ㄧ筆字串 內容是 '0405' fLst.Add('1010'); // 再加入ㄧ筆字串 內容是 '1010' fLst.Sorted := True; // 將日期排序 end; procedure TForm1.FormDestroy(Sender: TObject); begin fLst.Free; end; end. |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |