Adding items to listbox from textbox with tag.

WinDev

Member
Joined
Jul 10, 2012
Messages
9
Programming Experience
1-3
I have a quick question, I have a textbox that the user inputs information into. Then they click a button it adds all the text into a listview on another form. So far I have done this and it is working great.

Some of the items in the textbox contain tags such as <k>teststring</k>
I would like for the program to be able to take the full string between the tag and add it to the listview with a tag. So when then user can see it in the listview the text just says "code string", but it is still the same item. The reason for doing this is that these string can be on multiple lines so when I add it to the listview it adds on multiples lines and is too long.

This is the code im using to add the text items to the list. It adds numbers to the lines in the first column as well. I know I need to user a if then statement to check for the tags somehow and if there add the current item to the listview. Although I need to keep it in the same order as it appears on the textbox. Thanks in advance.


VB.NET:
[LEFT][COLOR=#000000]
Dim splitcode() As String = Me.ScriptEditor.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)[/COLOR][/LEFT]

                For i = 0 To splitcode.Length - 1                    PlayEvents.ListView1.Items.Add(New ListViewItem({i.ToString, splitcode(i)})) [LEFT][COLOR=#000000]                Next
[/COLOR][/LEFT]
[COLOR=#000000][FONT=verdana]
[/FONT][/COLOR]

 
To get the text between 2 tags you should use Regex (Regular Expressions), Here is an example to get the text between 2 '<k>' tags.

Dim pattern As String = ""
Dim splitcode() As String = Me.ScriptEditor.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)


For i = 0 To splitcode.Length - 1


    If splitcode(i).contains("<k>") And splitcode(i).contains("</k>") Then 'Only check for expressions if the tag is present.
        pattern = "\<k>(.*?)\</k>" 'Reset pattern, I put it in here incase you want to add more tags.
        
        PlayEvents.ListView1.Items.Add(New ListViewItem({i.ToString, splitcode(i)})) 'Add the whole string to the listbox.
        Dim insideText = Regex.Matches(splitcode(i), pattern) 'Use Regex to check for matches.
        
        For Each m As Match In title 'Go through all the matches that were found in the string.
            PlayEvents.ListView1.Items.Add(New ListViewItem({i.ToString, m.Groups(1).Value})) 'Add the text inside the tags to the listbox.
        Next
    Else
        PlayEvents.ListView1.Items.Add(New ListViewItem({i.ToString, splitcode(i)})) 'No tags so just add the whole string.
    End If


Next


Keep note I havn't tested it and I have no idea if it works or not. It should work though.. Just post again if you want more help :)

Regards,
Bryce Gough
 
Back
Top