![](/img/trans.png)
[英]Create a “save as” dialog when user clicks button and subsequently download the file
[英]Save Dialog when User Cancel
我需要在我的應用程序中保存來自不同位置的文件,所以我為此創建了一個子文件; 一切正常,除非用戶在保存對話框出現時單擊取消; 如果用戶點擊“取消”,表格將關閉; 我嘗試了下面代碼中顯示的兩個選項但兩個都沒有用; 任何建議將不勝感激:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SaveFileDialog()
End Sub
Sub SaveFileDialog()
SaveFileDialog1.Filter = "TXT Files (*.txt)|*.txt"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
Dim MekdamSaveFile = SaveFileDialog1.FileName
System.IO.File.WriteAllText(MekdamSaveFile, "")
My.Computer.FileSystem.WriteAllText(MekdamSaveFile, RichTextBox2.Text, True)
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dim result = MessageBox.Show("The File: has been changed, do you want to save it? ", _
"Mekdam Message 701", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If result = DialogResult.Cancel Then
e.Cancel = True
ElseIf result = DialogResult.No Then
'PROCEED...
ElseIf result = DialogResult.Yes Then
SaveFileDialog()
End If
End Sub
結束班
將SaveFileDialog子例程轉換為函數,如果完成了其他任何操作,則返回False,然后在FormClosing EventHandler中測試它並停止關閉。
修改后的SaveFileDialog
Function SaveFileDialog() As Boolean
SaveFileDialog1.Filter = "TXT Files (*.txt)|*.txt"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
Dim MekdamSaveFile = SaveFileDialog1.FileName
System.IO.File.WriteAllText(MekdamSaveFile, "")
My.Computer.FileSystem.WriteAllText(MekdamSaveFile, RichTextBox2.Text, True)
Return True 'Return True if Ok is clicked
Else
Return False 'return false this will give you something to conditionaly test
End If
End Function
修改了FormClosing EventHandler
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dim result = MessageBox.Show("The File: has been changed, do you want to save it? ", _
"Mekdam Message 701", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If result = DialogResult.Cancel Then
e.Cancel = True
ElseIf result = DialogResult.No Then
'PROCEED...
ElseIf result = DialogResult.Yes Then
If Not SaveFileDialog() Then e.Cancel = True 'this will abort the close
End If
End Sub
不太清楚你需要什么......試試這個:
Public Class Form1
Private Sub Test() Handles MyBase.Load
Do Until Not ShowSaveFileDialog() = DialogResult.Cancel
ShowSaveFileDialog()
Loop
End Sub
Private Function ShowSaveFileDialog() As DialogResult
Using SFD As New SaveFileDialog With
{
.Filter = "TXT Files (*.txt)|*.txt",
.ValidateNames = True
}
AddHandler SFD.FileOk, AddressOf SFD_FileOk
Return SFD.ShowDialog()
RemoveHandler SFD.FileOk, AddressOf SFD_FileOk
End Using
End Function
Private Sub SFD_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs)
IO.File.AppendAllText(sender.FileName, RichTextBox2.Text, System.Text.Encoding.Default)
End Sub
End Class
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.