總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2015年12月9日 星期三

【Microsoft Visual Studio】VB.net 【範例source】開啟檔案→另存新檔

 ****************************************
*          電腦環境:Windows 7 64bit                *
*          Microsoft Visual Studio版本:2010 Professional         *
****************************************


一、開啟Visual Studio開新專案
  View > Toolbox > 拖曳1個TextBox或MaskedTextBox+2個Button到Form視窗中
  分別修改它們的名稱如下↓

 
 


 Private btntbmapping As New Hashtable
    Private Sub CurrentUSPC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '設定textbox和button的關係
        btntbmapping.Add(Me.btnFileChoose_Current, Me.tbFileName_Current)
    End Sub

    Private Sub btnFileChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileChoose_Current.Click
        Dim effectTB As TextBox = CType(btntbmapping.Item(CType(sender, Button)), TextBox)
        Dim OpenFileDialog2 As New OpenFileDialog
        OpenFileDialog2.InitialDirectory = "\\120.100.39.66\Related\更新檔\"
        If Not Directory.Exists(OpenFileDialog2.InitialDirectory) Then
            MessageBox.Show("目錄不存在")
        End If
        OpenFileDialog2.Filter = "文字檔 (*.txt)|*.txt"
        OpenFileDialog2.Multiselect = False
        OpenFileDialog2.ShowDialog()
        effectTB.Text = OpenFileDialog2.FileName
    End Sub

 Private Sub btnFileTrans_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileTrans.Click
        Dim InFile As New FileInfo(tbFileName_Current.Text)
        SaveFileDialog1.Filter = "txt |*.txt"
        SaveFileDialog1.Title = "Save an txt File"
        SaveFileDialog1.FileName = "New-" & InFile.Name.Split(".")(0)
        SaveFileDialog1.ShowDialog()
        Dim th As Thread = New Thread(AddressOf TransFormFile)
        th.Start(tbFileName_Current.Text)
    End Sub

Private Sub TransFormFile(ByVal filePath As String, Optional ByVal Errinfo As String = "")
  'do something
End Sub


程式碼的意思是點下btnFileChoose_Current按鈕時,會跳出預設路徑的開啟檔案視窗,類似這樣↓


選了檔案之後,由於
btntbmapping As New Hashtable
 '設定textbox和button的關係
btntbmapping.Add(Me.btnFileChoose_Current, Me.tbFileName_Current)

的設定,會將檔案路徑由tbFileName_Current顯示



點btnFileTrans(就是「另存」button)後,會自動帶新檔名

 按下存檔後,便開始跑Thread
Dim th As Thread = New Thread(AddressOf TransFormFile)
        th.Start(tbFileName_Current.Text)
 



沒有留言:

張貼留言