Results 1 to 9 of 9

Thread: Adding items to listbox from textbox with tag.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Adding items to listbox from textbox with tag.

    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.

    Code:
     Dim splitcode() As String = Me.ScriptEditor.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
                    For i = 0 To splitcode.Length - 1
                        PlayEvents.ListView1.Items.Add(New ListViewItem({i.ToString, splitcode(i)}))
    
                    Next

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Adding items to listbox from textbox with tag.

    Clearly not a 'quick' question at all. Please supply a sample of the typical contents of the text box, the listview as you would like it to appear and describe what the listview entries are used for and how! At the very least we need to know what you mean by 'contains' tags.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Re: Adding items to listbox from textbox with tag.

    When I say tags think HTML tags like"<b>" "</b>". My application has a built in macro recorder with its own syntax. The user can add the syntax manually or record it and the recording gives an output from the mouse and keyboard hooks.

    The user can play the macro and it sends the syntax and outputs from the textbox into a listview and the program goes through the list item by item and executes each command.

    I am adding a scripting functionality as well so the user can add C# code in the program using a third party component. The only thing is the code between <b></b> can be long so when it transfers over to the listbox it adds to multiple lines and the scripting component won't work. So I figure if the listview displays a tag(meaning listview.item.add.tag) of something small and the tags value is this large string of code, then it should work correctly, because I could get the component to execute the it's value instead of the tag.

    As for the listview it contains only the number of the line on the left (index 1) and the hook output/syntax on the right. It does not have any other colums except for those two.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Re: Adding items to listbox from textbox with tag.

    Ok so I have been messing around and I worked something up. It is somewhat working. I was testing it in a blank project. If I have one set of tags with a string inbetween it works good but when I have multiples of them it adds more than just the string between.

    Im going to incorportate it into my real project to see if it is going to work the way I want it to. I need the items to stay in the same order they are in on the textbox, so we will see if that a problem.

    Code:
    Dim testStr As String = TextBox1.Text
            Dim rx As New Regex("(?<=\<vb\>)(\r\n|.)+(?=\<\/vb\>)")
            If InStr(TextBox1.Text, "<vb>") Then
                Dim result As String
                result = (rx.Match(testStr).Value).Trim()
                Dim mlc As ListViewItem = ListView1.Items.Add("Vb Code")
                mlc.Tag = result
            End If

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Re: Adding items to listbox from textbox with tag.

    Would it be possible to store the regex result to an array? I didn't think of this earlier but the text formatting is probably going to cause some problems if I do it this way. If I store it to an array it should be in the exact same structure as it was in the textbox correct?

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Adding items to listbox from textbox with tag.

    Ok, I see where you going now. The obvious thing to do is to make the listview merely a visible summary which essentially acts as an index to the actual source of the instructions (for which in this case I would recommend a generic list of strings) never actually seen by the user.

    So you add the full instruction to the list and simultaneously a summary of it to the listview thus keeping the indexes identical. Whenever the index of the listview is executed or selected you merely run the instruction from the list. So effectively you would have

    DoSomethingWith List.Item(Listview.Index)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Re: Adding items to listbox from textbox with tag.

    How would I add the items to this list? Also how would I tag those items like I would a listview, I.E. - listview.items.tag ?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Re: Adding items to listbox from textbox with tag.

    Im trying to see if this way works the way I wanted although when I integrated it with my real project code it doesnt work. Do you see where I went wrong?
    Code:
        Dim testStr As String = ScriptEditor.Text
                    Dim rx As New Regex("(?<=\<vb\>)(\r\n|.)+(?=\<\/vb\>)")
                    Dim splitcode() As String = Me.ScriptEditor.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
                    If InStr(ScriptEditor.Text, "<vb>") Then
                        Dim result As String
                        result = (rx.Match(testStr).Value).Trim()
                        Dim mlc As ListViewItem = ListView1.Items.Add("Vb Code")
                        mlc.Tag = result
                    End If
                    For i = 0 To splitcode.Length - 1
                        PlayEvents.ListView1.Items.Add(New ListViewItem({i.ToString, splitcode(i)}))
    
                    Next

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Adding items to listbox from textbox with tag.

    I'm 3 minutes past pumpkin time so I won't be able to do the code now but basically ...

    You have CommandList and CommandListView. You do whatever you do to get the full command, eg.

    IncrediblyComplicatedThing("very,very,very long string stretching into the middle of next week")

    This you add in its entirety to CommandList as item(0)

    Then you do your editing thing to reduce it to, say

    ICT(string)

    and add that to CommandListView as item (0)

    Whenever CommandListView(0) is chosen for action you substitute it with CommandList(0) in the actual program.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width