史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   軟體應用問題討論區 (http://forum.slime.com.tw/f5.html)
-   -   會寫VB的大大請進來看一下 (http://forum.slime.com.tw/thread145356.html)

cocoking 2005-03-22 03:13 PM

會寫VB的大大請進來看一下
 
小弟想寫個「萬年曆」加上「備忘錄」的功能
在萬年曆的部分就卡住了(紅色字為偵錯部份)
如果取消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

mini 2005-03-22 04:22 PM

有沒有錯誤訊息?
改 "年" 是用 "Command2"or"Command3" 改 還是用 鍵盤輸入?

如果單就沒進入 getMMdays 函式就報錯
一般是值的傳遞..等錯誤
還是po一下 錯誤訊息 比較好解決

用 VB 幾??

cocoking 2005-03-22 10:25 PM

引用:

作者: mini
有沒有錯誤訊息?
改 "年" 是用 "Command2"or"Command3" 改 還是用 鍵盤輸入?

如果單就沒進入 getMMdays 函式就報錯
一般是值的傳遞..等錯誤
還是po一下 錯誤訊息 比較好解決

用 VB 幾??

VB6
錯誤代碼為: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月都正確
換年就不行了

魔術王子 2005-03-23 07:28 AM

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將日期轉換成數值!可以嗎?)

cocoking 2005-03-23 08:15 AM

引用:

作者: 魔術王子
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將日期轉換成數值!可以嗎?)

謝謝大大的說明 小弟去試看看
不過就算我把
nYear.Text = Year(Now)
End If
判斷式取消 還是會出現一樣的位置偵錯@@"

魔術王子 2005-03-23 12:12 PM

引用:

判斷式取消

那nYear.Text的值是多少?

對了
您變更"月"時,有沒有輸入10 or 11 or 12 呢?

引用:

Private Sub nYear_Change() '年份改變時更新日曆

是看看加各判斷式
若Length(nYear.Text)==4再繼續執行呢?

cocoking 2005-03-23 01:14 PM

引用:

作者: 魔術王子
那nYear.Text的值是多少?

對了
您變更"月"時,有沒有輸入10 or 11 or 12 呢?

取消對年更動的判斷時
會套用到上述的判定
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年)
引用:

作者: 魔術王子
是看看加各判斷式
若Length(nYear.Text)==4再繼續執行呢?

一樣@@" >.<還是會出現一樣的錯誤

mini 2005-03-23 02:03 PM

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 有同樣效果

cocoking 2005-03-23 02:27 PM

引用:

作者: mini
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 有同樣效果

謝謝大大的教導
小弟馬上來試試^^

cocoking 2005-03-23 02:46 PM

感動中
經過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就不行?
謝謝~~~~

mini 2005-03-23 03:27 PM

這個好像是 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)

cocoking 2005-03-23 03:45 PM

引用:

作者: mini
這個好像是 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)

謝謝大大的指導~~~~ :ddrf567h: :ddrf567h: :ddrf567h:


所有時間均為台北時間。現在的時間是 12:13 AM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2024, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1