VB.Net/GUI/Dialog File Save Open — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:44, 26 мая 2010
Содержание
Add action Listener to Save Button on A Save File Dialog
<source lang="vbnet"> Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Imports System.Diagnostics Imports System.Drawing.Printing Imports System.ruponentModel
Public Class MainClass
Shared Dim WithEvents dlgSaveData As System.Windows.Forms.SaveFileDialog Shared Sub Main() dlgSaveData = New System.Windows.Forms.SaveFileDialog If dlgSaveData.ShowDialog() = Windows.Forms.DialogResult.OK Then MessageBox.Show(dlgSaveData.FileName) End If End Sub Shared Private Sub dlgSaveData_FileOk(ByVal sender As Object, _ ByVal e As System.ruponentModel.CancelEventArgs) Handles dlgSaveData.FileOk If Not dlgSaveData.FileName.EndsWith(".dat") Then MsgBox("File " & dlgSaveData.FileName & _ " is not a .dat file", _ MsgBoxStyle.Exclamation, _ "Invalid File Type") e.Cancel = True End If End Sub
End Class
</source>
File open dialog
<source lang="vbnet"> Imports System Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D
Public Class MainClass
Shared Sub Main() Dim dlg As OpenFileDialog = New OpenFileDialog() Dim res As DialogResult = dlg.ShowDialog() If res = DialogResult.OK Then MessageBox.Show("You picked: " & dlg.FileName) End If End Sub
End Class
</source>
File Open dialog: file filter
<source lang="vbnet"> Imports System Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.IO Imports System.Drawing.Printing
Public Class MainClass
Shared Sub Main() "Declare a OpenFileDialog object Dim objOpenFileDialog As New OpenFileDialog "Set the Open dialog properties With objOpenFileDialog .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*" .FilterIndex = 1 .Title = "Demo Open File Dialog" End With "Show the Open dialog and if the user clicks the Open button, "load the file If objOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then Dim allText As String Try "Read the contents of the file allText = My.ruputer.FileSystem.ReadAllText( _ objOpenFileDialog.FileName) "Display the file contents in the TextBox System.Console.WriteLine(allText) Catch fileException As Exception Throw fileException End Try End If "Clean up objOpenFileDialog.Dispose() objOpenFileDialog = Nothing End Sub
End Class
</source>
File save dialog
<source lang="vbnet"> Imports System Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D
Public Class MainClass
Shared Sub Main() Dim dlg As SaveFileDialog = New SaveFileDialog() Dim res As DialogResult = dlg.ShowDialog() End Sub
End Class
</source>
File save dialog: File filter, Default name, Overwrite Prompt and title
<source lang="vbnet"> Imports System Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.IO Imports System.Drawing.Printing
Public Class MainClass
Shared Sub Main() "Declare a SaveFileDialog object Dim objSaveFileDialog As New SaveFileDialog "Set the Save dialog properties With objSaveFileDialog .DefaultExt = "txt" .FileName = "Test Document" .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*" .FilterIndex = 1 .OverwritePrompt = True .Title = "Demo Save File Dialog" End With "Show the Save dialog and if the user clicks the Save button, "save the file If objSaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then Try Dim filePath As String "Open or Create the file filePath = System.IO.Path.rubine( _ My.ruputer.FileSystem.SpecialDirectories.MyDocuments, _ objSaveFileDialog.FileName) "Replace the contents of the file My.ruputer.FileSystem.WriteAllText(filePath, "C:\\a.txt", False) Catch fileException As Exception Throw fileException End Try End If "Clean up objSaveFileDialog.Dispose() objSaveFileDialog = Nothing End Sub
End Class
</source>
Open file dialog: Filter,Initial Directory,Title, CheckFileExists
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Data Imports System.IO Imports System.Collections Imports System.Windows.Forms Imports System.Drawing.Printing Public Class MainClass
Shared Sub Main() Dim myFileDialog As OpenFileDialog = New OpenFileDialog() With myFileDialog .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*" .FilterIndex = 1 .InitialDirectory = "C:\" .Title = "Open File" .CheckFileExists = False End With If myFileDialog.ShowDialog() = DialogResult.OK Then Console.WriteLine(myFileDialog.FileName) End If myFileDialog = Nothing End Sub
End Class
</source>