100000011
100000011

Reputation: 89

Splitting up text of listbox item into strings in VB

Pretty new to VB, haven't been using it long and have just been learning as I go so sorry if this is a bit of a noob question.

Basically I'm trying to create a piece of software for a business that doesn't necessarily require users to be on the same network and am currently working on an email-like messenger system. I already have the "new message" function sorted, the message gets stored on an SQL database with some other fields to help identify the messages and certain statuses such as "read" or "flagged".

I am now trying to make the "read messages" screen and am having a little trouble. I have a timer to check for new messages and submit a string to a listbox which is basically this:

    (message id number) - (user it has been sent from) - (message subject)

There are no actual brackets I just mean to demonstrate the layout, and that each segment is separated by " - ". I did originally want to have each selection in the listbox to consist of 2 lines, the first being the user that it was sent from and the second being the subject, but I couldn't find anything on this anywhere so settled for the information to be on 1 line.

Now all I have to do is figure out how to take the selected item of the listbox, and split the information up into strings. So for example I need to change somethis like this:

    100 - Admin - Test Message Subject

into this:

    msg_id = 100
    usr_from = Admin
    msg_subj = Test Message Subject

so that I could use those strings in an SQL query which would be used to get the rest of the message from the database, I hope that makes sense. I know it's such a basic thing I'm trying to do and I should really have learned it long ago but it's not something that I've needed to do until now. Well that is unless someone can offer a solution that would allow me to list the messages how I originally wanted them :P

Any help would be appreciated :) Thanks in advance!

Upvotes: 0

Views: 1999

Answers (1)

Phillip Trelford
Phillip Trelford

Reputation: 6543

You could use String.Split to split on the '-' character:

Dim information = "100 - Admin - Test Message Subject"
Dim fields = information.Split("-")
Dim msg_id = fields(0).Trim()
Dim usr_from = fields(1).Trim()
Dim msg_sub = fields(2).Trim()

Upvotes: 3

Related Questions