Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help with an openFileDialog control and attachments. 1

Status
Not open for further replies.

RotorTorque

Programmer
Apr 29, 2007
100
US
Hi all I have written an email generating app in VB 2005 that allows a user to attach a file. It works fine. The user selects the file from an openFileDialog control selects the file and then the file path is save to a string and the file name is displayed in an attachment text box.
They now have requested the ability to add multiple files. This brings up many issues:
1) I have to some how store the different attachment file paths.
2) Display the multiple file names in the attachment text box.
3) If the user desides to unattach a file I have to remove the file path from the list of attachments and also remove the file name from the attachment text box.

What woudl be the best way to handle this issue?

Here is a sample of my current code:
Code:
    Private Sub btnAttachments_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttachments.Click
        'Set the opendialog properties
        With OpenFileDialog1
            .InitialDirectory = "C:\"
            .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
            .FilterIndex = 1
            .Multiselect = False
            .CheckFileExists = False
            .RestoreDirectory = True
            .Title = "Attach Files"
        End With

        'Show the open dialog and if the user clicks the open button load the file.
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then


            Try
                Dim s As String = OpenFileDialog1.FileName
                Dim objFileInfo As System.IO.FileInfo
                objFileInfo = My.Computer.FileSystem.GetFileInfo(s)
                txtAttachments.Text = objFileInfo.Name
                Dim strFilePath as string = objFileInfo.FullName
            Catch ex As Exception
                Throw ex
            End Try
        End If
    End Sub

Thanks in advance,
RotorTorque ;-)
 
Well, first off I would suggest using a listbox to hold the filenames, instead of a textbox. It is much easier to add and remove items (attachments) with a listbox than a textbox. Here's som modifications (in red) to your existing code:

With OpenFileDialog1
.InitialDirectory = "C:\"
.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
.FilterIndex = 1
.Multiselect = [red]True[/red]
.CheckFileExists = False
.RestoreDirectory = True
.Title = "Attach Files"
End With

'Show the open dialog and if the user clicks the open button load the file.
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then


Try
Dim s As String
Dim objFileInfo As System.IO.FileInfo
[red]
For Each s In OpenFileDialog1.FileNames
objFileInfo = My.Computer.FileSystem.GetFileInfo(s)
ListBox1.Items.Add(objFileInfo.Name)
Next
[/red]
Catch ex As Exception
Throw ex
End Try
End If

Now, you can put a "Remove" button next to the attachment list, and code it to remove any selected items in the listbox. When the email is sent, just loop through any items in the listbox and add them as attachments to the email.

Also, since each file can have a different path, you need to somehow associate those with the file names. I suggest making a class that can hold both the file name (for display in the listbox) and the file's path (for when it actually need to be attached:

Private Class EmailAttachment
Public FileName As String
Public FilePath As String

Public Overrides Function ToString() As String
Return FileName
End Function
End Class




With OpenFileDialog1
.InitialDirectory = "C:\"
.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
.FilterIndex = 1
.Multiselect = [red]True[/red]
.CheckFileExists = False
.RestoreDirectory = True
.Title = "Attach Files"
End With

'Show the open dialog and if the user clicks the open button load the file.
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then


Try
Dim s As String
Dim ea As EmailAttachment
Dim objFileInfo As System.IO.FileInfo
[red]
For Each s In OpenFileDialog1.FileNames
objFileInfo = My.Computer.FileSystem.GetFileInfo(s)
ea = New EmailAttachment
ea.FileName = objFileInfo.Name
ea.FilePath = objFileInfo.FullName
ListBox1.Items.Add(ea)
Next
[/red]
Catch ex As Exception
Throw ex
End Try
End If

Then, to read the file paths from the listbox:

Dim ea As EmailAttachment

For c As Integer = 0 to ListBox1.Items.Count - 1
ea = CType(ListBox1.Items(c), EmailAttachment)

'note: the next code is completely made up. Do whatever it is you do to add the email attachments
Email.Attacmments.Add(ea.FilePath)
Next

I hope this makes sense.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank you jebenson. I will try your suggestion and post my results.

Rotortorque [thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top