2021年11月8日 星期一

VB.net 儲存文字檔案

 

儲存文字檔

VB代码

需要引用 system 和 system.IO

方法1: 利用 StreamWriter 写入文档,值得注意的是,要确定要保存的文件夹已经创建,不然会报错。

1
2
3
        Dim sw As StreamWriter = New StreamWriter("C:\temp\test.txt")
        sw.Write("This is Test Text")
        sw.Close()

方法2: 利用 My.Computer.FileSystem.WriteAllText 写入文档

1
        My.Computer.FileSystem.WriteAllText("C:\temp\test.txt""This is test Text")

方法3: 利用 System.IO.File.WriteAllText 写入文档

1
        System.IO.File.WriteAllText("c:\temp\test.txt""this is test file")


VB 代码

方法1:

1
2
3
4
5
6
        Dim sw As StreamWriter = New StreamWriter("C:\temp\test.txt")
        sw.Write("abc" & vbCrLf)
        sw.Close()
        Dim sw2 As StreamWriter = New StreamWriter("C:\temp\test.txt"True)
        sw2.Write("456" & vbCrLf)
        sw2.Close()

方法2:

1
        My.Computer.FileSystem.WriteAllText("test.txt""This is test Text"True)

方法3:

1
        System.IO.File.AppendAllText("c:\temp\test.txt""this is extra test file")


VB 代码

1
2
3
4
5
        Dim sw As StreamWriter = New StreamWriter("C:\temp\test.txt")
        Dim strTemp As String = Me.RichTextBox1.Text
        strTemp = strTemp.Replace(vbLf, vbCrLf)
        sw.Write(strTemp)
        sw.Close()


VB代码

1
        RichTextBox1.SaveFile("test.txt", RichTextBoxStreamType.PlainText)


2019年12月19日 星期四

MsgBox (message box) 對話視窗

一個msgbox圖片範例:
有時候會看到八~
讓我們一起來做做看八

如果你只是要做簡單的顯示字串的話:
MsgBox("你要顯示的字串")
圖片範例:

如果你想要做出可以更改圖示及回復按鈕的msgbox的話:
MsgBox("你要顯示的字串", 回復按鈕 + 圖示)
MsgBox("你要顯示的字串", 0 + 64)

圖示的代碼列表:

按鈕代碼列表:

如果你要更改msgbox的標題的話:
MsgBox("你要顯示的字串", 回復按鈕 + 圖示,"標題")
範例:MsgBox("你要顯示的字串", 0 + 64,"標題")


如果你要設定msgbox的傳回值得話:
就需要用到If Then
而他需要判斷按鈕,所以在這邊列了一張圖片
範例:
X = MsgBox("你有女朋友嗎", 4 + 32, "小知")
        If X = 6 Then
            MsgBox("好好喔", 0 + 64, "小知")
        Else
            MsgBox("加油!", 0 + 64, "小知")
        End If
範例圖片:
選是的話:
選否的話


資料來源
https://forum.gamer.com.tw/Co.php?bsn=60030&sn=1605224

電腦發音、音效、音樂

Public Class Form1
    Declare Function Beep Lib "kernel32" (ByVal dwFreq As Integer, ByVal dwDuration As Integer) As Integer
註:將beep改成Console.Beep就不用上面這一Declare Function Beep......

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '救護車聲
        Dim myAlert As New Threading.Thread(AddressOf Alert)
        Dim I As Short
        For I = 0 To 10
            Beep(1000, 500)
            Beep(2000, 1000)
        Next
    End Sub
  
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        '炸彈掉下聲
        Dim I As Short
        For I = 2500 To 750 Step -10
            Beep(I, 50)
        Next
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        '生日快樂
        Dim i() As Integer = {392, 392, 440, 392, 523, 494, 392, 392, 440, 392, 587, 523, 392, 392, 784, 659, 523, 494, 440, 698, 698, 659, 523, 587, 523}
        Dim j() As Integer = {375, 125, 500, 500, 500, 1000, 375, 125, 500, 500, 500, 1000, 375, 125, 500, 500, 500, 500, 1000, 375, 125, 500, 500, 500, 1000}
        Dim ak As Integer
        For ak = 0 To i.Length - 1
            Beep(i(ak), j(ak))
        Next
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        '警報聲
        Dim I As Short
        For I = 0 To 20

            Beep(75, 200)
            Beep(400, 200)
        Next
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        '單聲
        Console.Beep()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim myAlert As New Threading.Thread(AddressOf Alert)
        myAlert.Start()
    End Sub
    Sub Alert()
        Dim I As Short
        For I = 2500 To 750 Step -10
            Beep(I, 50)
            'Beep(I, 100)
        Next
    End Sub
End Class
-------------------------------------------------------------------------------------------------------------
●MSDN的描述:The Beep function generates simple tones on the speaker. The function is synchronous; it does not return control to its caller until the sound finishes.
●小站已測試平台:Windows9x、Windows NT4、Windows 2000、Windows XP
●建議宣告法: 'VB6
Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
--------------------------------------------------------------------------------
 'VB.Net宣告
Declare Function Beep Lib "kernel32" (ByVal dwFreq As Integer, ByVal dwDuration As Integer) As Integer 
●用法範 例:
●VB6警車聲
Private Sub Form_Activate()
    For I = 0 To 10
        Beep 75, 200
        Beep 400, 200
        DoEvents
    Next
End Sub 
●VB.Net警車聲
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myAlert As New Threading.Thread(AddressOf Alert)
    myAlert.Start()
End Sub
Sub Alert()
    Dim I As Short
    For I = 0 To 10
        Beep(75, 200)
        Beep(400, 200)
    Next
End Sub 

●VB6卡通墜落聲
Private Sub Form_Activate()
    For I = 2500 To 750 Step -10
        Beep I, 50
        DoEvents
    Next
End Sub 
●VB.Net警車聲
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myAlert As New Threading.Thread(AddressOf Alert)
    myAlert.Start()
End Sub
Sub Alert()
    Dim I As Short
    For I = 2500 To 750 Step -10
        Beep(I, 50)
    Next
End Sub 

●dwFreq→頻率(Hz)
●dwDuration→播放時間(千分之一秒)

不會當掉的VB.net Delay (Sleep)

Public Sub delay(ByVal TimeLast) Dim Start As Integer = Environment.TickCount() 'Dim TimeLast As Integer = 7000 ' 要延遲 t 秒,就設為 t *1000 Do If Environment.TickCount() - Start > TimeLast Then Exit Do Application.DoEvents() ' 要記得寫這行,不然都在跑迴圈,畫面可能會不見 Loop End Sub

2015年2月9日 星期一

VB6 等待 做其他事 釋放資源 delay

請你使用timer元件,配合下列副函數,如"wait 5"就是等待5秒
Private Sub Wait(iSec As Long)
Dim iTimer As Long

    iTimer = Timer
    Do Until Timer - iTimer >= iSec
     DoEvents
    Loop
End Sub

2014年6月30日 星期一

文字檔開啟

一、直接使用File opentext的方式
Dim sr As StreamReader = File.OpenText("utf.txt")
二、使用FileInfo的方法
Dim file1 As FileInfo = New FileInfo("b12.txt")
Dim sr As StreamReader = file1.OpenText()
三、使用 File 的 OpenText 或 FileInfo 的 OpenText 預設都以 UTF-8 編碼方式開啟檔案,若遇上文字檔本身並非UTF存檔,會有亂碼產生。若需指定編碼開啟,需使用如下:
Dim sr As StreamReader = My.Computer.FileSystem.OpenTextFileReader("b12.txt", System.Text.Encoding.Default)
Default: 是使用文預設的編碼方式開啟

2014年6月28日 星期六

判斷路徑資料夾是否存在及建立資料夾

VB.NET
判別路徑資料夾是否存在:
 If Not IO.Directory.Exists("Path+資料夾名稱") Then

            '如不存在,建立資料夾
            IO.Directory.CreateDirectory("Path+資料夾名稱")
 End If


判別路徑檔案是否存在:
 System.IO.File.Exists("Path+FileName")

http://programlanguagebymingze.blogspot.tw/2012/10/vbnet.html