Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,559 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,906 people online right now. Registration is fast and FREE... Join Now!




I think I need to concatenate!

 
Reply to this topicStart new topic

I think I need to concatenate!

Robp
9 Apr, 2008 - 04:08 AM
Post #1

New D.I.C Head
*

Joined: 9 Apr, 2008
Posts: 1

Hi all, I'm not only new to dic but fairly new to VB.
I'm trying to grab a list of files (RTF's in this case) and put them in a list box (I've done this bit).
I get the result like this:
c:\test\test1.rtf
c:\test\test2.rtf

I then need to grab the results (all of them) and turn them into:
c:\test\test1.rtf;c:\test\test2.rtf

so it needs to go on a single line with a semi colon between the results.

I think i need to concatenate the results (?) but i seem to be banging my head a bit. my code for grabbing the files and displaying the results is:

CODE
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo("c:\test")
        Dim diar1 As IO.FileInfo() = di.GetFiles("*.rtf")
        Dim dra As IO.FileInfo
        'list the names of all files in the specified directory
        For Each dra In diar1

            ListBox1.Items.Add(dra.FullName)
        Next
    End Sub
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    End Sub


Any help guys would be really appreciated, I'm not looking for a complete solution cos i like to figure things out (better for learning), but i'm really getting fed up eith this one.

P.S. the reason i need it in the linear fashion is thati'm then using the result to attach to an email so each file needs to have full details and be separated by semi colon.

Robp
User is offlineProfile CardPM
+Quote Post

nofear217
RE: I Think I Need To Concatenate!
10 Apr, 2008 - 11:14 AM
Post #2

D.I.C Head
Group Icon

Joined: 8 Nov, 2007
Posts: 164



Thanked: 1 times
Dream Kudos: 175
My Contributions
could do something like declare a string variable to hold them (draFiles) and then do draFiles += & ";" & dra.FullName within the for loop.

Something like this:
CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim di As New IO.DirectoryInfo("c:\test")
        Dim diar1 As IO.FileInfo() = di.GetFiles("*.rtf")
        Dim dra As IO.FileInfo
        Dim draFiles As String = ""

        For Each dra In diar1

            ListBox1.Items.Add(dra.FullName)
            If draFiles = "" Then

                draFiles = dra.FullName.ToString

            Else

                draFiles += "" & ";" & dra.FullName

            End If

        Next

        Label1.Text = draFiles

    End Sub

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: I Think I Need To Concatenate!
10 Apr, 2008 - 11:24 AM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
I would look into using the String.Join Method for this. This does exactly what you're looking to do with zero concatenating. Here is an example on how this method works


vb

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' make a reference to a directory
Dim di As New IO.DirectoryInfo("c:\test")
'Create our string array for hold the array of files
Dim diar1 As IO.FileInfo() = di.GetFiles("*.rtf")
'Create a string object to hold the delimited string
Dim diar1String As String
'Create our delimited string
diar1String = String.Join(";",diar1)
End Sub

User is offlineProfile CardPM
+Quote Post

nofear217
RE: I Think I Need To Concatenate!
11 Apr, 2008 - 11:15 AM
Post #4

D.I.C Head
Group Icon

Joined: 8 Nov, 2007
Posts: 164



Thanked: 1 times
Dream Kudos: 175
My Contributions
Thanks PC....learn something new everyday.

Speaking of which, how do you wrap the code with the numbered lines?

This post has been edited by nofear217: 11 Apr, 2008 - 11:15 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: I Think I Need To Concatenate!
11 Apr, 2008 - 11:19 AM
Post #5

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
@nofear217: It's in how you do your code tags. As you know code tags are [*code][/code*] (minus the *'s of course). For the colored syntax and line numbers do your code tags like (this example is for C#) [*code=csharp][/code*] (Minus the *'s of course) smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:31PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month