![]() |
|
論壇說明 |
歡迎您來到『史萊姆論壇』 ^___^ 您目前正以訪客的身份瀏覽本論壇,訪客所擁有的權限將受到限制,您可以瀏覽本論壇大部份的版區與文章,但您將無法參與任何討論或是使用私人訊息與其他會員交流。若您希望擁有完整的使用權限,請註冊成為我們的一份子,註冊的程序十分簡單、快速,而且最重要的是--註冊是完全免費的! 請點擊這裡:『註冊成為我們的一份子!』 |
![]() ![]() |
|
主題工具 | 顯示模式 |
![]() |
#1 |
註冊會員
|
![]() 小弟想寫個「萬年曆」加上「備忘錄」的功能
在萬年曆的部分就卡住了(紅色字為偵錯部份) 如果取消Private Sub nYear_Change()就能執行,不過當年改變時日曆不會更新 月則會 不知道問題出來哪?(command1(0~41)為顯示日期的 command button,日後還要作備忘錄的功能) 請大大幫忙看一下(form跟textbox我就不po了,跟除錯沒有關係) Private Sub Command2_Click() '按一下此鍵年減1 nYear.Text = nYear.Text - 1 End Sub Private Sub Command3_Click() '按一下此鍵年加1 nYear.Text = nYear.Text + 1 End Sub Private Sub Command4_Click() '按一下此鍵月減1如果月<1 則月=12且年減1 nMonth.Text = nMonth.Text - 1 If nMonth.Text < 1 Then nMonth.Text = 12 nYear.Text = nYear.Text - 1 End If End Sub Private Sub Command5_Click() '按一下此鍵月加1如果月>12則月=1年加1 nMonth.Text = nMonth.Text + 1 If nMonth.Text > 12 Then nMonth.Text = 1 nYear.Text = nYear.Text + 1 End If End Sub Function getMMdays(pnYear, pnMonth) '定義每月的總天數 Select Case pnMonth Case 1, 3, 5, 7, 8, 10, 12 getMMdays = 31 Case 4, 6, 9, 11 getMMdays = 30 Case 2 If (pnYear Mod 4) = 0 Then If (pnYear Mod 100) = 0 Then If (pnYear Mod 400) = 0 Then getMMdays = 29 Else getMMdays = 28 End If Else getMMdays = 29 End If Else getMMdays = 28 End If End Select End Function Function getWeekday(pnYear, pnMonth) '自定函數求每月的第一天星期為? getWeekday = Weekday(DateSerial(pnYear, pnMonth, 1)) End Function Private Sub Form_Load() '表格load nYear.Text = Year(Now) nMonth.Text = Month(Now) nMdays = getMMdays(CInt(nYear.Text), CInt(nMonth.Text)) nWday = getWeekday(CInt(nYear.Text), CInt(nMonth.Text)) Call showMonth(nMdays, nWday) End Sub Function showMonth(pnMdays, pnWday) '定義函數顯示日曆 pnMdays = getMMdays(CInt(nYear.Text), CInt(nMonth.Text)) - 1 pnWday = getWeekday(CInt(nYear.Text), CInt(nMonth.Text)) For I = 0 To pnMdays Command1(pnWday - 1 + I).Caption = I + 1 Next I End Function Private Sub nMonth_Change() '月份改變時更新日曆 If nMonth.Text = "" Then nMonth.Text = Month(Now) End If For I = 0 To 41 Command1(I).Caption = "" Next I nMdays = getMMdays(CInt(nYear.Text), CInt(nMonth.Text)) nWday = getWeekday(CInt(nYear.Text), CInt(nMonth.Text)) Call showMonth(nMdays, nWday) End Sub Private Sub nYear_Change() '年份改變時更新日曆 If nYear.Text = "" Then nYear.Text = Year(Now) End If For I = 0 To 41 Command1(I).Caption = "" Next I nMdays = getMMdays(CInt(nYear.Text), CInt(nMonth.Text)) nWday = getWeekday(CInt(nYear.Text), CInt(nMonth.Text)) Call showMonth(nMdays, nWday) End Sub |
![]() |
送花文章: 74,
![]() |
![]() |
#3 (permalink) | |
註冊會員
|
![]() 引用:
錯誤代碼為:13 型態不符合 Private Sub Command2_Click() '按一下此鍵年減1 nYear.Text = nYear.Text - 1 End Sub Private Sub Command3_Click() '按一下此鍵年加1 nYear.Text = nYear.Text + 1 End Sub 上述command2、3是控制年加和減 因為年是寫在textbox內 所以也可以直接用打的 Private Sub nYear_Change() '年份改變時更新日曆★ If nYear.Text = "" Then nYear.Text = Year(Now) End If For I = 0 To 41 Command1(I).Caption = "" Next I nMdays = getMMdays(CInt(nYear.Text), CInt(nMonth.Text)) nWday = getWeekday(CInt(nYear.Text), CInt(nMonth.Text)) Call showMonth(nMdays, nWday) End Sub ★上述當年更動時對目前日曆作更新 這個加入執行就會出現錯誤代碼13(偵錯是紅色部份) 如果上述不加入 程執就可執行 不會有錯誤訊息 但由於沒有加入更新年 所以萬年曆 會變成2005年1~12月都正確 換年就不行了 |
|
![]() |
送花文章: 74,
![]() |
![]() |
#4 (permalink) |
版區管理員
![]() |
![]() If nYear.Text = "" Then
nYear.Text = Year(Now) End If For I = 0 To 41 Command1(I).Caption = "" Next I nMdays = getMMdays(CInt(nYear.Text), CInt(nMonth.Text)) nWday = getWeekday(CInt(nYear.Text), CInt(nMonth.Text)) 當使用者沒輸入年時,程式便讀取系統時間 (然而系統讀取的是年月日的,不知您是否有試過直接輸入年會不會有誤?或不輸入呢?因為Now傳回的是年月日,而在以CInt將日期轉換成數值!可以嗎?) |
__________________ 『唸金母心咒:嗡。金母。悉地。吽。』 持此咒者,可免一切瘟疫,一切瘟神抱頭四散。 魔術就是欣賞神奇的效果 如果魔術的秘密被破解了 那魔術就失去欣賞的價值 |
|
![]() |
送花文章: 1536,
![]() |
![]() |
#5 (permalink) | |
註冊會員
|
![]() 引用:
不過就算我把 nYear.Text = Year(Now) End If 判斷式取消 還是會出現一樣的位置偵錯@@" |
|
![]() |
送花文章: 74,
![]() |
![]() |
#7 (permalink) | ||
註冊會員
|
![]() 引用:
會套用到上述的判定 Private Sub Form_Load() '表格load nYear.Text = Year(Now) nMonth.Text = Month(Now) nMdays = getMMdays(CInt(nYear.Text), CInt(nMonth.Text)) nWday = getWeekday(CInt(nYear.Text), CInt(nMonth.Text)) Call showMonth(nMdays, nWday) End Sub 也就是說會被定義為現在的年跟月 2005年3月 當變動月時都會顯示正確「2005」年的月曆 像是今天的2月有28天 也是正確的 不管是用command4、5作月的更動或是直接用鍵盤輸入textbox==>nMonth.Text 顯示出來的月曆都是正常的(限於2005年) 引用:
|
||
![]() |
送花文章: 74,
![]() |
![]() |
#8 (permalink) |
管理版主
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() 13 型態不符合
果然是 值的傳遞之問題 建議 Function getMMdays(pnYear, pnMonth) 請改成 Function getMMdays(pnYear As Integer , pnMonth As Integer) Visual Basic 引數傳遞方式預設為 ByRef 如果不指定資料型態或物件型態,且模組中沒有Deftype 陳述式,變數的預設型態為 Variant。 型態不符合 http://yes.nctu.edu.tw/VB/B2_Code/Err_13.htm getMMdays(CInt(nYear.Text), CInt(nMonth.Text)) 改成 getMMdays(Val(nYear.Text), Val(nMonth.Text)) Dim x As Integer x = Val("1.2") 結果 x=1 而不是 1.2 所以與 Cint 有同樣效果 |
![]() |
送花文章: 2029,
![]() |
![]() |
#9 (permalink) | |
註冊會員
|
![]() 引用:
小弟馬上來試試^^ |
|
![]() |
送花文章: 74,
![]() |
![]() |
#10 (permalink) |
註冊會員
|
![]() 感動中
經過mini大大指點 錯誤終於修正 目前完全正常了 再次感謝mini大大 不過小弟還有不懂的地方想請教大大 為何小弟用cint就不行呢? 例: Private Sub Command1_Click() Dim x As Integer x = InputBox("請輸入一數字") Print CInt(x) Print Val(x) End Sub 當我輸入有x=1.3時 print出來為cint(x)==>1 val(x)==>1 如果沒有定義x Dim x As Integer<==取消 則print出來為cint(x)==>1 val(x)==>1.3 所以定義變數為Integer時 val的質會等於cint的質 重點就是為什麼把萬年曆改成val就行呢?cint就不行? 謝謝~~~~ |
![]() |
送花文章: 74,
![]() |
![]() |
#11 (permalink) |
管理版主
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() 這個好像是 VB 的奇怪點
照理說 沒指定時是 Variant 這個類似變數 可任意接受/更換 變數型態 但 一開始時 Form_Load() 執行過一次 getMMdays 第二次要用就不行 我也說不上來 實際上要用 除錯方式監看 引數(變數)型態 的 變換才可知 VB是如何作業的 VB的 強迫轉換 與 C++的強迫轉換 還是有差異的 其實還可以做一些實驗來理解VB的編譯方式 比如 於 Form_Load() 重複執行兩次 nMdays = getMMdays nMdays = getMMdays(CInt(nYear.Text), CInt(nMonth.Text)) nMdays = getMMdays(CInt(nYear.Text), CInt(nMonth.Text)) 來證明是否為上述理論 如是 那又有另一個理論 是否執行第一次時 Function getMMdays(pnYear, pnMonth) 就會變成 Function getMMdays(pnYear as integer, pnMonth as integer) 或 Function getMMdays(pnYear as Variant, pnMonth as Variant) 以致於不能接受其他變數型態 對了 以自己的經驗 你可以用一個變數來間接傳值 這樣就可繼續用 Cint 方式 Private Sub Form_Load() '表格load nYear.Text = Year(Now) nMonth.Text = Month(Now) dim x dim y ' 要分開寫而不是 dim x,y 寫在一起 x=CInt(nYear.Text) y=CInt(nMonth.Text) nMdays = getMMdays(x,y) |
![]() |
送花文章: 2029,
![]() |
![]() |
#12 (permalink) | |
註冊會員
|
![]() 引用:
![]() ![]() ![]() |
|
![]() |
送花文章: 74,
![]() |