分類 |
標題 |
大意 |
作者 |
日期 |
| 使用說明 | VB5 初級生範例使用說明 | 老怪 | 1998/7/19 | |
| 檔案 I/O | 文字檔的定位格式化輸出 | Put # & Get # 的使用 | 老怪 | 1998/4/30 |
| 物件外觀 | 物件碰撞程式試作(一) | One Timer | 老怪 | 1998/4/30 |
| 物件外觀 | 物件碰撞程式試作(二) | Two Timer | 老怪 | 1998/4/30 |
| 圖形處理 | 如何保留 Paintpictrue 影像 | 執行Refresh之後圖不見了 | 老怪 | 1998/4/29 |
| 圖形處理 | 圖面背景透明的技巧 | 透明四法 | 老怪 | 1998/5/5 |
| 圖形處理 | 區塊取圖與區塊貼圖 | PaintPicture() 的參數使用 | 老怪 | 1998/5/5 |
| 時間換算 | 日期時間格式的資料型態 | 以 Now() 函數為例 | 老怪 | 1998/5/7 |
| 字串處理 | 用 VB 5.0 算字串長度 | StrConv() 的 LenB() 使用 | 老怪 | 1998/5/2 |
| 字串處理 | TextBox 限輸入數字 | 以 KeyPress Event 為例 | 老怪 | 1998/5/15 |
| 字串處理 | TextBox 限制輸入長度 | 以 TextBox_Change 為例 | 老怪 | 1998/5/20 |
| 字串處理 | 如何顯示 "&" 字元 | 老怪 | 1998/5/21 | |
| 字串處理 | 數字或字母判斷 | IsNumeric() 函數的使用 | 老怪 | 1998/5/23 |
| 字串處理 | 突破 TextBox 32 K 的限制 | Mid() 函數的使用 | 老怪 | 1998/5/23 |
| 字串處理 | 字母轉成循序數字 | Asc() 函數的使用 | 老怪 | 1998/5/26 |
| 字串處理 | 記事本編輯功能模擬 | ClipBoard 物件的使用 | 老怪 | 1998/5/26 |
| 變數使用 | 在執行期產生物件 | As 和 As New | 老怪 | 1998/5/19 |
| 物件引用 | 使用項目與使用元件的分別 | 老怪 | 1998/5/22 | |
| 隨機取樣 | 如何把牌洗散洗亂 | Randomize 和 Rnd() 使用 | 老怪 | 1998/5/23 |
| 界面處理 | 以 Code 刪除字串 | Sendkeys 的使用 | 老怪 | 1998/5/26 |
| 界面處理 | TextBox.Text 的全選功能 | SelStart & SelLength 屬性 | 老怪 | 1998/5/26 |
隨緣、惜緣 撰寫於文章
我使用如下的程式碼
Form1.PaintPicture Form2.Picture, 100, 100, Form2.Picture.Width,
Form2.Picture.Height, , , , , vbSrcAnd
Form1.Refresh
在第一個statement執行完圖會貼過到form1,但form1執行Refresh之後圖又不見了,請問為何會如此??還有那裡要注意的嗎??
老怪答:
Johnson 撰寫於文章
輸出到文字檔:
若有五個欄位的資料,如何做到定位的指定,如在第幾個字為起始點,像c or fortran作格式輸出,另外在最後一個資料輸出後,如何跳到下一列的第一個字元。請幫忙一下。
老怪答:
'宣告一個包含五個欄位的自訂形態變數
Private Type Mytype
FirstName As String * 4
LastName As String * 4
ID_No As String * 10
Salary As Long
BirthDay As Date
End Type
'存放開檔代號的變數
Dim F_Handle As Integer
'宣告 MyRec 為自訂形態變數
Dim MyRec As Mytype
Private Sub Form_Load()
'用 Freefile 函數取得開檔代號
F_Handle = FreeFile()
' For Random
是採用開隨機檔模式,Len(MyRec) 則是指定每次讀入
Mytype 形態
'自定變數之寬度
Open "c:\temp\temp.txt" For Random
As #F_Handle Len = Len(MyRec)
' 隨便輸入些資料,以便觀測寫入檔案之用
MyRec.FirstName = "嘉"
MyRec.LastName = "鄭"
MyRec.ID_No = "A1234"
MyRec.Salary = 5000
MyRec.BirthDay = #2/14/60#
' 用 Put 方法,把 MyRec �堛爾禤じg到
"c:\temp\temp.txt" ��
Put #F_Handle, 1, MyRec
'仝上
MyRec.FirstName = "玉"
MyRec.LastName = "鄭"
MyRec.ID_No = "A0034"
MyRec.Salary = 8000
MyRec.BirthDay = #5/15/60#
Put #F_Handle, 2, MyRec
'用 Get 方法從 "c:\temp\temp.txt"
檔�堮雩禤ヾA放到 MyRec �堙C
' [2] 表示取第二筆記錄
Get #F_Handle, 2, MyRec
Me.Show
Print "[這是第二筆記錄]"
Print MyRec.FirstName
Print MyRec.LastName
Print MyRec.ID_No
Print MyRec.Salary
Print MyRec.BirthDay
' [1] 表示取第一筆記錄
Get #F_Handle, 1, MyRec
Print "[這是第一筆記錄]"
Print MyRec.FirstName
Print MyRec.LastName
Print MyRec.ID_No
Print MyRec.Salary
Print MyRec.BirthDay
' 關閉所開之檔案
Close #F_Handle
End Sub
VERSION 5.00
Begin VB.Form flishape
BackColor =
&H00FFFF80&
Caption
= "Form1"
ClientHeight = 3195
ClientLeft =
60
ClientTop =
345
ClientWidth =
4680
LinkTopic =
"Form1"
ScaleHeight =
3195
ScaleWidth =
4680
StartUpPosition = 3 '系統預設值
Begin VB.Timer Timer1
Interval
= 1
Left
= 1800
Top
= 1320
End
Begin VB.Shape Shape2
BorderWidth
= 3
FillColor
= &H000000FF&
FillStyle
= 0 '實心
Height
= 855
Left
= 3360
Shape
= 4 '圓角矩形
Top
= 2040
Width
= 855
End
Begin VB.Shape Shape1
BorderWidth
= 3
FillColor
= &H0000FFFF&
FillStyle
= 0 '實心
Height
= 855
Left
= 600
Shape
= 4 '圓角矩形
Top
= 240
Width
= 855
End
End
Attribute VB_Name = "flishape"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Go_Oridging()
'Shape 歸位方法,目的在改變視窗大小後,_
'避免 Shape 掉到視窗外
Shape1.Move 0, 0
Shape2.Move Me.ScaleWidth - Shape2.Width, _
Me.ScaleHeight - Shape2.Height
End Sub
Private Sub Form_Load()
Go_Oridging
End Sub
Private Sub Form_Resize()
Go_Oridging
End Sub
Private Sub Timer1_Timer()
'兩個 Shape 的正負向指標
Static Shp1XFwd As Integer, Shp1YFwd As Integer
Static Shp2XFwd As Integer, Shp2YFwd As Integer
'每步步伐
Static l_go As Integer
'設定正負向初值
If Shp1XFwd = 0 Then Shp1XFwd = 1
If Shp1YFwd = 0 Then Shp1YFwd = 1
If Shp2XFwd = 0 Then Shp2XFwd = -1
If Shp2YFwd = 0 Then Shp2YFwd = -1
l_go = 120
'Shape1 的邊界處理
With Shape1
If .Left <0 Or .Left>
Me.ScaleWidth - .Width Then _
Shp1XFwd = Shp1XFwd * -1
If .Top <0 Or .Top>
Me.ScaleHeight - .Height Then _
Shp1YFwd = Shp1YFwd * -1
End With
'Shape2 的邊界處理
With Shape2
If .Left <0 Or .Left>
Me.ScaleWidth - .Width Then _
Shp2XFwd = Shp2XFwd * -1
If .Top <0 Or .Top>
Me.ScaleHeight - .Height Then _
Shp2YFwd = Shp2YFwd * -1
End With
'Shape1 與 Shape2 碰撞處理
If (Shape1.Left + Shape1.Width - Shape2.Left)
> 0 And _
Shape1.Left <(Shape2.Width + Shape2.Left) And _
(Shape1.Top + Shape1.Height - Shape2.Top) > 0 And _
Shape1.Top <(Shape2.Height + Shape2.Top) Then
'路徑轉向
Shp1XFwd = Shp1XFwd * -1
Shp2XFwd = Shp2XFwd * -1
Shp1YFwd = Shp1YFwd * -1
Shp2YFwd = Shp2YFwd * -1
End If
'移動 Shape1
Shape1.Move Shape1.Left + (Shp1XFwd * l_go), _
Shape1.Top + (Shp1YFwd * l_go)
'移動 Shape2
Shape2.Move Shape2.Left + (Shp2XFwd * l_go), _
Shape2.Top + (Shp2YFwd * l_go)
End Sub
這是用兩個計時器的,效果不一樣。
VERSION 5.00
Begin VB.Form flishape
BackColor =
&H00FFFF80&
Caption
= "Form1"
ClientHeight = 3195
ClientLeft =
60
ClientTop =
345
ClientWidth =
4680
LinkTopic =
"Form1"
ScaleHeight =
3195
ScaleWidth =
4680
StartUpPosition = 3 '系統預設值
Begin VB.Timer Timer2
Interval
= 100
Left
= 2400
Top
= 1320
End
Begin VB.Timer Timer1
Interval
= 10
Left
= 1800
Top
= 1320
End
Begin VB.Shape Shape2
BorderWidth
= 3
FillColor
= &H000000FF&
FillStyle
= 0 '實心
Height
= 855
Left
= 3360
Shape
= 4 '圓角矩形
Top
= 2040
Width
= 855
End
Begin VB.Shape Shape1
BorderWidth
= 3
FillColor
= &H0000FFFF&
FillStyle
= 0 '實心
Height
= 855
Left
= 600
Shape
= 4 '圓角矩形
Top
= 240
Width
= 855
End
End
Attribute VB_Name = "flishape"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Go_Oridging()
'Shape 歸位方法,目的在改變視窗大小後,_
'避免 Shape 掉到視窗外
Shape1.Move 0, 0
Shape2.Move Me.ScaleWidth - Shape2.Width, _
Me.ScaleHeight - Shape2.Height
End Sub
Private Sub Form_Load()
Go_Oridging
End Sub
Private Sub Form_Resize()
Go_Oridging
End Sub
Private Sub Timer1_Timer()
'Shape 的正負向指標
Static Shp1XFwd As Integer, Shp1YFwd As Integer
'每步步伐
Dim l_go As Integer
'設定正負向初值
If Shp1XFwd = 0 Then Shp1XFwd = 1
If Shp1YFwd = 0 Then Shp1YFwd = 1
l_go = 120
'Shape1 的邊界處理
With Shape1
If .Left <0 Or .Left>
Me.ScaleWidth - .Width Then _
Shp1XFwd = Shp1XFwd * -1
If .Top <0 Or .Top>
Me.ScaleHeight - .Height Then _
Shp1YFwd = Shp1YFwd * -1
End With
'Shape1 與 Shape2 碰撞處理
If (Shape1.Left + Shape1.Width - Shape2.Left)
> 0 And _
Shape1.Left <(Shape2.Width + Shape2.Left) And _
(Shape1.Top + Shape1.Height - Shape2.Top) > 0 And _
Shape1.Top <(Shape2.Height + Shape2.Top) Then
'路徑轉向
Shp1XFwd = Shp1XFwd * -1
Shp1YFwd = Shp1YFwd * -1
End If
'移動 Shape1
Shape1.Move Shape1.Left + (Shp1XFwd * l_go), _
Shape1.Top + (Shp1YFwd * l_go)
End Sub
Private Sub Timer2_Timer()
Static Shp2XFwd As Integer, Shp2YFwd As Integer
Dim l_go As Integer
If Shp2XFwd = 0 Then Shp2XFwd = -1
If Shp2YFwd = 0 Then Shp2YFwd = -1
l_go = 120
'Shape2
的邊界處理
With Shape2
If .Left <0 Or .Left>
Me.ScaleWidth - .Width Then _
Shp2XFwd = Shp2XFwd * -1
If .Top <0 Or .Top>
Me.ScaleHeight - .Height Then _
Shp2YFwd = Shp2YFwd * -1
End With
'Shape1 與 Shape2 碰撞處理
If (Shape1.Left + Shape1.Width - Shape2.Left)
> 0 And _
Shape1.Left <(Shape2.Width + Shape2.Left) And _
(Shape1.Top + Shape1.Height - Shape2.Top) > 0 And _
Shape1.Top <(Shape2.Height + Shape2.Top) Then
'路徑轉向
Shp2XFwd = Shp2XFwd * -1
Shp2YFwd = Shp2YFwd * -1
End If
'移動 Shape2
Shape2.Move Shape2.Left + (Shp2XFwd * l_go), _
Shape2.Top + (Shp2YFwd * l_go)
End Sub
黃金眼 撰寫於文章
假設我讀進一個字串,此字串可能內含中文字,我想算出該字串的長度,該如何處理。EX : "abc中文" 的長度我想算成 7。
老怪答:
妳是我今生的新娘 撰寫於文章
請問一 下 vb中 的圖面 [背景] 可以設成透明的嘛?? 若可以那要如何用呢???
老怪答:
GIF 89A
MeTalFile (副檔名 *.wmf or *.emf)
圖示檔 (副檔名 *.ico)
亞亞 撰寫於文章
我要在 picture1 中複製 picture2 的圖,但是要它從picture1的右上角開始出現,是用paintPicture這個方法來寫的喔!
老怪答:
現在假設我們要取 Picture2 左下角,20 平方點的圖貼在 picture1 的右上角,那指令是這樣下:
picture1.paintpicture picture2.picture,picture1.scalewidth-20,0,20,20,0,picture2.scaleheight-20,20,20
主旨: Re: 請問VB5中怎麼算時間
老怪答:
狂暴之星 撰寫於文章
我要在text 下...假如是限制數字...不能有字元..要用什麼使用...麻煩一下..:
老怪答:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'KeyAscii 32 以下是一些控制鍵,攔截會造成操作障礙
If KeyAscii >= 33 Then
If KeyAscii <= vbKey9 And KeyAscii>= vbKey0 Then
Else
'把 KeyAscii 設為 0 就是取消輸入
KeyAscii = 0
MsgBox "不可輸入非數字字元"
End If
End If
End Sub
喜歡貓咪的黃金鼠
請問各位高手:VB 5.0 要如何在Run Time, 動態Create new Object? Thank you!
老怪答:
你在 VB Run Time, 想動態 Create new Object,要看 Object 的特性,如果 Object 是 VB Global �堛� Object,不具有 Control 的屬性,那用 Dim YourObjectName As New VBObject 的方式宣告,如果你用的是 VB5.0 ,在你輸入 New 空一格後,系統會通知你那些 Object 是可以 Dim As New 的(不太多,只有幾項)。
如果你要宣告的 Object 是 Control(就是可以放到 Form 面板上的那些東東),請先在設計階段布置一個同類型的 Control 到 Form 上,並在 Control.Index 屬性值上給一個索引值(通常是 0),到了執行階段,就可以不需先行宣告,直接以 Load TestComman(1),Load TestCommand(2)......,來動態增加,不過用完請以 Unload 釋放掉,以養成節省記憶體的良好習慣。
有一點要特別注意,所有 Load 進來的 Control.Visible 屬性都是 False,要把它改成 True 才會顯現,所以 Load 完以後看不見,不要以為是 Load 失敗,剛 Load 進來的 Control 的位置是疊在原 Control 上的,要做 Move 動作才會分為兩個。
最後做一總整理:
寄件者: Annie Chiu
在VB中使用text 的maxlength 屬性,我的資料欄位長度為10, 如何控制text 只能輸入10個英文字, 或5個中文字呢?
老怪答:
放一顆心.. 撰寫於文章
我要如何在Label中顯示"&"這一個字元!!因為如果直接打"&"好像會變成under line.....
老怪答:
連打 "&&" 兩個。
心出發 撰寫於文章
在vb5的環境下,專案/設定使用項目 與 專案/設定使用元件 有何不同呢?
老怪答:
洗牌的設計模型:
其實把牌洗散開來,是有它的洗牌模型在的,據老怪所想到的就有下列幾種:
可以想像成 n 張牌一字排開,任取兩張互換,換愈多次牌愈亂。
可以想像成 n 張牌就有 n 個位置,而所有的牌都放在另個箱子�堙A洗牌時逐一由牌箱中隨機抽出一張放在位置上,每一張牌一定會被抽到一次,而且只會被抽到一次,抽牌次數就是牌張數。
洗牌的設計實例:
下面是我用 2.隨機排列法所寫 VB Code,請眾家高手指教:
Option Explicit
'宣告撲克牌四個花色的常數
Const CardSpear As Integer = 0
Const CardHeart As Integer = 1
Const CardDimond As Integer = 2
Const CardClub As Integer = 3
'宣告一個存放撲克牌花色、點數的自訂變數型態
Private Type GameCard
'撲克牌花色
CardColor As Integer
'撲克牌點數
CardNum As Integer
End Type
'宣告存放 52 張撲克牌的變數
Private Poker(51) As GameCard
Private Sub Command1_Click()
'I,J,K 皆做為迴圈計數器使用
Dim I As Long, J As Integer, K As Integer
'TempCard 用為牌資料戶換的暫存區,RndRec
儲存亂數值。
Dim TempCard As GameCard, RndRec As Long
'把花色代表數換回字串的暫存變數
Dim ColorWord As String
'啟動亂數種子
Randomize
'把各花色牌裝進 Poker 陣列��
For J = 0 To 3
For K = 1 To 13
'純粹防止 Poker 溢位而已,如果你確定自己
'計算無誤,可以拿掉這個判斷式
If I > UBound(Poker) Then
Exit For
End If
Poker(I).CardColor = J
Poker(I).CardNum = K
I = I + 1
Next
Next
'開始用亂數洗牌,基本的設計原理是假設撲克是放在抽取箱�堙A
'要發牌時再一張張由箱中隨機抽出,依序排好,特色是箱中所有
'的牌都被抽到一次,而且只會被抽到一次。
For I = UBound(Poker) To 1 Step -1
'取一個整數亂數來抽一張牌
RndRec = Int((I + 1) *
Rnd)
'把選到的牌花色及點數資料交給
TempCard
TempCard.CardColor =
Poker(RndRec).CardColor
TempCard.CardNum =
Poker(RndRec).CardNum
'把第 I
張牌的資料移到選到的牌上
Poker(RndRec).CardColor = Poker(I).CardColor
Poker(RndRec).CardNum
= Poker(I).CardNum
'把 TempCard
�堛爾禤ぎ儘麮� I 張牌上
Poker(I).CardColor =
TempCard.CardColor
Poker(I).CardNum =
TempCard.CardNum
Next
'把排好的 Poker 顯示一下
List1.Clear
For I = 0 To UBound(Poker)
Select Case
Poker(I).CardColor
Case CardSpear
ColorWord = "Spear\"
Case CardHeart
ColorWord = "Heart\"
Case CardDimond
ColorWord = "Dimond\"
Case CardClub
ColorWord = "Club\"
End Select
List1.AddItem
ColorWord & Poker(I).CardNum
DoEvents
Next
End Sub
離 愁 撰寫於文章 ...
請問把使用者所輸入的一串文字,各別拆開後...該如何 判斷個別字母是否為數字或字母啊?!
老怪答:
請利用 IsNumeric 函數,並參考下面 Vb Code
Dim I As Long, ShortString As String
For I = 1 To Len(Text1.Text)
ShortString =
Mid(Text1.Text, I, 1)
Debug.Print
"[" & ShortString & "]" & _
IIf(IsNumeric(ShortString), "是", "不是")
& _
"數字字串"
Next
mason 撰寫於文章
在Visual Basic中,TextBox所能顯示的Text只有32K Bytes,若要同時顯示 32K Bytes的Text時,要如何解決???
老怪答:
離 愁 撰寫於文章 ...
請教先進...如何讓使用者輸入的字元"A"轉成10,"B"轉成11, "C"轉成12....等...依此類推呢?!
老怪答:
用 asc() 函數做判斷:
dim yourword as string*1
dim yourcode as integer
yourword="你輸入的一個字元"
if asc(yourword)>=asc("A") and asc(yourword)<= asc("Z")
yourcode=asc(yourword)-asc("A")+10
end if
debug.print yourcode
我不是馬拉度納 的文章中提到:
有沒有函數是可以刪除輸入的上一個字元???(就像按下Back Space一樣)
老怪答:
使用 Sendkeys 陳述式,如下:
'讓 text1 取得焦點,以便接收鍵盤字元。
text1.setfocus
'模擬使用者按下 backspace 按鍵
sendkeys "{Backspace}"
球球 撰寫於文章
在文字框快速按滑鼠兩下, 系統就會自動框選文字框內所有的文字,若希望點按一下就能自動框選文字框內所有的文字, 程式要如何寫呢?
老怪答:
Private Sub Text1_Click()
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End Sub
風 撰寫於文章
請問如果想要VB 5.0來寫一個記事本..而裡面的複製、貼上要如何寫呢...
老怪答:
要使用 ClipBoard 物件來達成。
'複製
Private Sub mnuCopy_Click()
Clipboard.SetText Text1.SelText
End Sub
'剪下
Private Sub mnuCut_Click()
Clipboard.SetText Text1.SelText
Text1.SelText = ""
End Sub
'貼上
Private Sub mnuPaste_Click()
Text1.SelText = Clipboard.GetText
End Sub
'全選
Private Sub mnuAll_Click()
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Clipboard.SetText Text1.SelText
End Sub