• 0

VB .NET [Handling Cancel Button on Save Dialog]


Question

Option Explicit On 
Option Strict On
Public Class frmFileTransfer
 ? ?Inherits System.Windows.Forms.Form

 ? ?Private Sub btnSendFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendFile.Click

 ? ? ? ?With OpenFileTransferDialog
 ? ? ? ? ? ?.Title = "File transfer"
 ? ? ? ? ? ?.Filter = "All files (*.*)|*.*|Compressed Files (*.zip;*.rar;*.tar;*.cab)|*.zip;*.rar;*.tar;*.cab|Documents (*.doc;*.txt;*.rtf;)|*.doc;*.txt;*.rtf|Image Files (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif;|Sound Files (*.wav;*.mp3;*.ogg;*.wma)|*.wav;*.mp3;*.ogg;*.wma;|Video Files (*.avi;*.mpeg;*.mov;*.wmv)|*.avi;*.mpeg;*.mov;*.wmv"
 ? ? ? ? ? ?.ShowHelp = True
 ? ? ? ? ? ?.CheckFileExists = True
 ? ? ? ? ? ?.CheckPathExists = True
 ? ? ? ? ? ?.ShowDialog()
 ? ? ? ? ? ?Try
 ? ? ? ? ? ? ? ?.OpenFile()
 ? ? ? ? ? ?Catch ex As System.IndexOutOfRangeException
 ? ? ? ? ? ? ? ?Exit Sub
 ? ? ? ? ? ?End Try
 ? ? ? ?End With
 ? ? ? ?lblTransferedPercent.Text = PBTransfered.Value.ToString + "%"
 ? ? ? ?lblFileTransfer.Text = System.IO.Path.GetFileName(OpenFileTransferDialog.FileName)
 ? ? ? ?lblStatusMessage.Text = "Transferring..."
 ? ? ? ?btnSendFile.Enabled = False
 ? ? ? ?btnCancelTransfer.Enabled = True
 ? ? ? ?Timer1.Start()

 ? ?End Sub

 ? ?Private Sub btnCancelTransfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelTransfer.Click
 ? ? ? ?Me.Close()
 ? ? ? ?Me.Dispose(True)
 ? ?End Sub

 ? ?Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
 ? ? ? ?Timer1.Start()
 ? ? ? ?lblTransferedPercent.Text = PBTransfered.Value.ToString + "&"
 ? ? ? ?PBTransfered.Value += 1
 ? ? ? ?lblTransferedPercent.Text = PBTransfered.Value.ToString + "&"
 ? ? ? ?If PBTransfered.Value = 100 Then
 ? ? ? ? ? ?lblTransferedPercent.Text = PBTransfered.Value.ToString + "%"
 ? ? ? ? ? ?lblStatusMessage.Text = "Transfer Complete!"
 ? ? ? ? ? ?Timer1.Stop()
 ? ? ? ? ? ?btnCancelTransfer.Text = "Close"
 ? ? ? ? ? ?Dim result As DialogResult
 ? ? ? ? ? ?result = MessageBox.Show("Wouldlike to open this file?", "Open file?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2)

 ? ? ? ? ? ?If result = DialogResult.Yes Then
 ? ? ? ? ? ? ? ?Process.Start(OpenFileTransferDialog.FileName)
 ? ? ? ? ? ? ? ?Me.Close()
 ? ? ? ? ? ?ElseIf result = DialogResult.No Then
 ? ? ? ? ? ? ? ?Me.Close()
 ? ? ? ? ? ?End If
 ? ? ? ?End If
 ? ?End Sub
End Class

Right now i'm using:

Try
 ? ? ? ? ? ?.OpenFile()
 ? ? ? ? ? ?Catch ex As System.IndexOutOfRangeException
 ? ? ? ? ? ? ? ?Exit Sub
 ? ? ? ? ? ?End Try

There has to be a better way than that. anyone got any ideas?

12 answers to this question

Recommended Posts

  • 0
  Matrox said:
if OpenFileTransferDialog.FileName = "" then exit sub

Mind if I ask what your making?

a toy. :D

well i'll go see if that code handles the "CANCEL" button. :huh:

  • 0
  weenur said:
If OpenFileTransferDialog.ShowDialog() = DialogResult.Ok Then

thanks weenur (Y)

With OpenFileTransferDialog
 ? ? ? ? ? ?.Title = "PalTalk file transfer"
 ? ? ? ? ? ?.Filter = "All files (*.*)|*.*|Compressed Files (*.zip;*.rar;*.tar;*.cab)|*.zip;*.rar;*.tar;*.cab|Documents (*.doc;*.txt;*.rtf;)|*.doc;*.txt;*.rtf|Image Files (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif;|Sound Files (*.wav;*.mp3;*.ogg;*.wma)|*.wav;*.mp3;*.ogg;*.wma;|Video Files (*.avi;*.mpeg;*.mov;*.wmv)|*.avi;*.mpeg;*.mov;*.wmv"
 ? ? ? ? ? ?.ShowHelp = True
 ? ? ? ? ? ?.CheckFileExists = True
 ? ? ? ? ? ?.CheckPathExists = True
 ? ? ? ? ? ?If OpenFileTransferDialog.ShowDialog = DialogResult.OK Then
 ? ? ? ? ? ? ? ?.OpenFile()
 ? ? ? ? ? ?Else
 ? ? ? ? ? ? ? ?Exit Sub
 ? ? ? ? ? ?End I:Dbr />End with

works as you said it would :D

  • 0

lol...

strange u guys use the if statement

Select Case openDialog.ShowDialog

           Case DialogResult.OK

           'Do the task

           Case DialogResult.Cancel

           'Don't need to add any code here

End Select

that's absolutely as simple as it gets

  • 0
  Winston said:
lol...

strange u guys use the if statement

Select Case openDialog.ShowDialog

           Case DialogResult.OK

           'Do the task

           Case DialogResult.Cancel

           'Don't need to add any code here

End Select

that's absolutely as simple as it gets

the if statement is fine for such a simple statement. switch isn't really necessary for something like that.

  • 0
  bangbang023 said:
  Winston said:
lol...

strange u guys use the if statement

Select Case openDialog.ShowDialog

 ? ? ? ? ? Case DialogResult.OK

 ? ? ? ? ? 'Do the task

 ? ? ? ? ? Case DialogResult.Cancel

 ? ? ? ? ? 'Don't need to add any code here

End Select

that's absolutely as simple as it gets

the if statement is fine for such a simple statement. switch isn't really necessary for something like that.

i would use a case select for 3 or m:rolleyes:, not 2. :rolleyes:

its a matter of code style. It all a:)unts to the same. :)

  • 0
  xStainDx said:
i would use a case select for 3 or more events, not 2. :rolleyes:

its a matter of code style. It all amounts to the same. :)

yeah, I only use switch statements when I want t oavoid nested if's. for a single if statement, I just go with the IF

  • 0
  bangbang023 said:
  xStainDx said:
i would use a case select for 3 or more events, not 2.  :rolleyes:

its a matter of code style. It all amounts to the same. :)

yeah, I only use switch statements when I want t oavoid nested if's. for a single if statement, I just go with the IF

Yeah i guess... personal preference.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.