locked
Attachment Field in VB.Net RRS feed

  • Question

  • Hi,

    Is there any relevant field in VB.Net for adding attachments or do I need to use a normal text field ? I just need the field for adding attachment, no need to rest of the code.

    If there is no build it field available, can you guide me for third party controls ?

    Thanks,


    Wednesday, October 30, 2013 1:34 PM

Answers

  • There is no out of the box attachment control.

    The closest standard control you can use is the richtextbox like.

    OpenFileDialog1.ShowDialog() Dim Paths As New System.Collections.Specialized.StringCollection Paths.AddRange(OpenFileDialog1.FileNames) Clipboard.SetFileDropList(Paths) RichTextBox1.Paste()

    this little piece of code adds the files (plus file icons) you selected in the file dialog to the richtextbox as pictures.


    • Edited by Rbie Thursday, October 31, 2013 8:12 AM
    • Marked as answer by Carl Cai Tuesday, November 5, 2013 8:31 AM
    Thursday, October 31, 2013 8:11 AM

All replies

  • if you are using WPF, you can have a look at

    http://www.codeproject.com/Articles/47585/WPF-Attachment-Control

    Wednesday, October 30, 2013 2:09 PM
  • If you are asking about MS-Access attachment fields then see the following code bank article

    Retrieving & Viewing Attachments from Access 2007 or Later ACCDB Files


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Wednesday, October 30, 2013 3:03 PM
  • I am working on windows forms but the link your shared seems to have a tough code.

    Don't we have any simple controls for attachment field ?

    Wednesday, October 30, 2013 7:09 PM
  • I am working on windows forms but the link your shared seems to have a tough code.

    Don't we have any simple controls for attachment field ?

    Do you mean this one:

    If so, try this:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim myStream As Stream = Nothing
            Dim openFileDialog1 As New OpenFileDialog()
    
            openFileDialog1.InitialDirectory = "c:\"
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
            openFileDialog1.FilterIndex = 2
            openFileDialog1.RestoreDirectory = True
    
            If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                'Try
                '    myStream = openFileDialog1.OpenFile()
                '    If (myStream IsNot Nothing) Then
                '        ' Insert code to read the stream here. 
                '    End If
                'Catch Ex As Exception
                '    MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
                'Finally
                '    ' Check this again, since we need to make sure we didn't throw an exception on open. 
                '    If (myStream IsNot Nothing) Then
                '        myStream.Close()
                '    End If
                'End Try
                TextBox1.Text = openFileDialog1.FileName
            End If
    
        End Sub

    Nice day. : )


    remember make the reply as answer and vote the reply as helpful if it helps.

    Thursday, October 31, 2013 6:55 AM
  • There is no out of the box attachment control.

    The closest standard control you can use is the richtextbox like.

    OpenFileDialog1.ShowDialog() Dim Paths As New System.Collections.Specialized.StringCollection Paths.AddRange(OpenFileDialog1.FileNames) Clipboard.SetFileDropList(Paths) RichTextBox1.Paste()

    this little piece of code adds the files (plus file icons) you selected in the file dialog to the richtextbox as pictures.


    • Edited by Rbie Thursday, October 31, 2013 8:12 AM
    • Marked as answer by Carl Cai Tuesday, November 5, 2013 8:31 AM
    Thursday, October 31, 2013 8:11 AM
  • There is no out of the box attachment control.

    The closest standard control you can use is the richtextbox like.

    OpenFileDialog1.ShowDialog() Dim Paths As New System.Collections.Specialized.StringCollection Paths.AddRange(OpenFileDialog1.FileNames) Clipboard.SetFileDropList(Paths) RichTextBox1.Paste()

    this little piece of code adds the files (plus file icons) you selected in the file dialog to the richtextbox as pictures.


    This could be one of the solution but in richtextbox attachment is looking very ugly. Can't we have something like below ?

    Screen hot take from MS Outlook.


    Wednesday, November 6, 2013 10:55 PM
  • This could be one of the solution but in richtextbox attachment is looking very ugly. Can't we have something like below ?

    In outlook that is a reference to an object which will be included as an attachment to the email that is being composed.   At the point where you have taken the screenshot there is no attchement - there is simply information in the program about how that email will be constructed (it will be constructed so that it includes the attachment).

    So your 'attachment' will be an object of some sort within your application.  Whether or not there is a facility in .Net that makes that simple depends entirely on what you intend to do with that attachment.   As far as the screenshot is concerned, it's nothing more than some text formatted in a particular way - there are a large number of simple ways to do that.

    Wednesday, November 6, 2013 11:33 PM
  • ok... in outlook also you might have used Richtextbox but it looks really beautiful. How can I have such thing in my VB.Net application ?
    Thursday, November 7, 2013 6:35 PM
  • ok... in outlook also you might have used Richtextbox but it looks really beautiful. How can I have such thing in my VB.Net application ?

    Do you mean that you are only interested in the appearance - some underlined text and an icon - and not the functionality associated with an Outlook attachment?

    You can achieve that appearance with a label control using the image properties and the text formatting properties.   See:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.linklabel(v=vs.110).aspx

    Thursday, November 7, 2013 7:21 PM
  • Carl I know you answered this a long time ago.  I am new to visual studio 2017 and need a attachment field also.  I set up your code but am not sure how it runs.  where do I put your code?
    Wednesday, April 17, 2019 7:02 PM