Results 1 to 25 of 25

Thread: Export items from Listview to textbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Export items from Listview to textbox

    Hello VbForums community
    I need to export items from listview to a multiline textbox in a special manner.
    When I click an item in listviw it goes to textbox but when it comes to the last line it will export again in the first line.
    Code:
    Text1.Text = Text1.Text & vbCrLf & lvw.SelectedItem
    The attached image explains what I need to do.

    Name:  pic.png
Views: 235
Size:  5.7 KB

    thank you

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    Your image does NOT explain what you want to do (neither does your 'explanation').

    What do you mean by 'when it comes to the last line (last line in what?) it will export again (click on the same item more than once?) in the first line.' (first line of what?).

    Please be more specific and detailed in an explanation.
    Sam I am (as well as Confused at times).

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    Do you mean that your multiline textbox is only supposed to have three lines, so that when you click the fourth item in your listview, it will be appended to the first line of the text box?. And then susequently each item is added to the next line (until the third line is reached, and then go to the first line AGAIN?)
    Sam I am (as well as Confused at times).

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: Export items from Listview to textbox

    Quote Originally Posted by SamOscarBrown View Post
    Do you mean that your multiline textbox is only supposed to have three lines, so that when you click the fourth item in your listview, it will be appended to the first line of the text box?. And then susequently each item is added to the next line (until the third line is reached, and then go to the first line AGAIN?)
    thank you for your interest
    yes that 's exactly what I'm looking for.
    Is this possible?
    thank you for your help

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    This is probably a homework assignment, so my 'solution' (alternate solution) will not get you an "A", but it is an example of doing what you want, except I, in my example, am using a listBOX and a grid (in my case a VB Flexgrid control, but could be an MSFlexgrd, an MSHFlexgrid, or the VSFlexgrid (links available somewhere on this forum). In my example, I limit the number of Columns in the grid to 3.

    Put a listbox on a form. Add a grid. Put the following code in the form and run it.

    Code:
    Option Explicit  'you should ALWAYS have this line on every form/module
    Dim iNumItemsUsed As Integer  'to keep track of how many times the listbox has been clicked
    
    
    Private Sub Form_Load()
        List1.AddItem ("AAAAA")  'self explanatory
        List1.AddItem ("BBBBB")
        List1.AddItem ("CCCCC")
        List1.AddItem ("DDDDD")
        List1.AddItem ("EEEEE")
        List1.AddItem ("FFFFF")
        List1.AddItem ("GGGGG")
        Grid1.Rows = 3  'arbitrary number of rows (as you had 3 in your multiline textbox, I chose 3 for this example
        Grid1.Cols = 1  'setup of grid
        Grid1.FixedCols = 0 'setup of grid
        Grid1.FixedRows = 0 'setup of grid
    End Sub
    
    
    Private Sub List1_Click()
        iNumItemsUsed = iNumItemsUsed + 1  'keep track of how many times the listbox has been clicked on
        Select Case iNumItemsUsed
            Case 1
                Grid1.Cols = 1  'set number of columns
                Grid1.TextMatrix(0, 0) = List1.Text
            Case 2
                Grid1.TextMatrix(1, 0) = List1.Text
            Case 3
                Grid1.TextMatrix(2, 0) = List1.Text
            Case 4
                Grid1.Cols = 2   'reset number of columns
                Grid1.TextMatrix(0, 1) = List1.Text
            Case 5
                Grid1.TextMatrix(1, 1) = List1.Text
            Case 6
                Grid1.TextMatrix(2, 1) = List1.Text
            Case 7
                Grid1.Cols = 3  'reset number of columns
                Grid1.TextMatrix(0, 2) = List1.Text
            Case 8
                Grid1.TextMatrix(1, 2) = List1.Text
            Case 9
                Grid1.TextMatrix(2, 2) = List1.Text
            Case Else  'because all 9 cells (3 rows and 3 columns) have been now filled, tell the user.
                MsgBox "The grid is full!"
        End Select
    End Sub
    Sam I am (as well as Confused at times).

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: Export items from Listview to textbox

    thank you Sam
    Though I need to work with listiew and textbox, I start to get an idea.
    I hope I'll succeed to do that.
    thanks again

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    homework? (Just so I'll know how much to help)
    Sam I am (as well as Confused at times).

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: Export items from Listview to textbox

    I'm sorry this is what I did but it did not work as I was expecting

    Code:
    clicknum = clicknum + 1
    Select Case clicknum
    Case 1
          Text1.Text =  lvw.SelectedItem.Text
        Case 2
            Text1.Text = Text1.Text & vbNewLine & lvw.SelectedItem.Text
        Case 3
        clicknum = 0
            Text1.Text = Text1.Text & vbTab & vbTab  & _
            "                                       " & lvw.SelectedItem.Text
            End Select
    Last edited by Adebiyi24; Aug 28th, 2021 at 07:45 AM.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: Export items from Listview to textbox

    Quote Originally Posted by SamOscarBrown View Post
    homework? (Just so I'll know how much to help)
    No it is not a homework but it is part of a small application.

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    Then if not homework, why not use a listview (instead of my listbox) and a FlexGrid variant (instead of forcing something a textbox is not the best suited at)?
    Sam I am (as well as Confused at times).

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    Here is a START (first four items selected)...if you want to continue to use a control that is not the best one suited, here is an example (substitute your listview for my listbox):

    Code:
    Private Sub List1_Click()
        iNumItemsUsed = iNumItemsUsed + 1
        Dim i As Integer
        Dim myFirstLine As String
        Dim mySecondLine As String
        Dim myThirdLine As String
        Select Case iNumItemsUsed
            Case 1
                aLine(0) = List1.Text
                Text1.Text = List1.Text
            Case 2
                aLine(1) = List1.Text
                Text1.Text = Text1.Text & vbCrLf & List1.Text
            Case 3
                aLine(2) = List1.Text
                Text1.Text = Text1.Text & vbCrLf & List1.Text
            Case 4
                i = InStr(1, Text1.Text, vbCrLf)
                myFirstLine = Mid(Text1.Text, 1, i)
                Text1.Text = aLine(0) & vbTab & List1.Text & vbCrLf & aLine(1) & vbCrLf & aLine(2)
    Sam I am (as well as Confused at times).

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: Export items from Listview to textbox

    SamOscarBrown
    thank you very much
    I would be very grateful to you if you add case 5.
    I have been trying for long to get it work but I failed.

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    No, I probably will not....keep at it, by trial and error...think about what each "LINE" contains...save each line to a different variable, add them to your next iteration.

    BUT, it would be SO much more simple to use a grid like I did....why are you so insistent on using a ML textbox? Even a two column listbox would be better than what you are doing.
    Sam I am (as well as Confused at times).

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: Export items from Listview to textbox

    Code:
    Case 5
                i = InStr(1, Text1.Text, vbCrLf)
                Text1.Text = aLine(1) & vbTab & lvw.SelectedItem.Text & vbCrLf & aLine(0) & vbCrLf & aLine(1)
    Impossible to get it work as I wish.
    Anyway thank you sir very much

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    A slightly different approach...which might help you figure out how to do the remaining case #6: (MyFirstLine, MySecondLine and MyThirdLine are dimmed Globally--that way you can 're-use' the ones you want.)

    Code:
            Case 5
                Dim myText As String
                myText = Replace(Text1.Text, vbCrLf, "x")
                i = InStr(1, myText, "x")
                myFirstLine = Mid(myText, 1, i - 1)
                mySecondLine = Mid(myText, i + 1)
                i = InStr(1, mySecondLine, "x")
                mySecondLine = Mid(mySecondLine, 1, i - 1) & vbTab & List1.Text
                i = InStrRev(myText, "x")
                myThirdLine = Mid(myText, i + 1)
                Text1.Text = myFirstLine & vbCrLf & mySecondLine & vbCrLf & myThirdLine
    Sam I am (as well as Confused at times).

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: Export items from Listview to textbox

    Quote Originally Posted by SamOscarBrown View Post
    A slightly different approach...which might help you figure out how to do the remaining case #6: (MyFirstLine, MySecondLine and MyThirdLine are dimmed Globally--that way you can 're-use' the ones you want.)

    Code:
            Case 5
                Dim myText As String
                myText = Replace(Text1.Text, vbCrLf, "x")
                i = InStr(1, myText, "x")
                myFirstLine = Mid(myText, 1, i - 1)
                mySecondLine = Mid(myText, i + 1)
                i = InStr(1, mySecondLine, "x")
                mySecondLine = Mid(mySecondLine, 1, i - 1) & vbTab & List1.Text
                i = InStrRev(myText, "x")
                myThirdLine = Mid(myText, i + 1)
                Text1.Text = myFirstLine & vbCrLf & mySecondLine & vbCrLf & myThirdLine
    I thank you very much Sir
    but case 5 didn't work

  17. #17
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    Of course it does. Let’s see all your code and an image of what you see in the text box after case 5
    Sam I am (as well as Confused at times).

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: Export items from Listview to textbox

    Quote Originally Posted by SamOscarBrown View Post
    Of course it does. Let’s see all your code and an image of what you see in the text box after case 5
    Code:
    On Error Resume Next
     clicknum = clicknum + 1
     Dim aLine() As String
       
        aLine = Split(Text1.Text, vbCrLf)
        Dim i As Integer
        Dim myFirstLine As String
        Dim mySecondLine As String
        Dim myThirdLine As String
        Select Case clicknum
            Case 1
                aLine(0) = lvw_consult.SelectedItem.Text
                Text1.Text = lvw_consult.SelectedItem.Text
            Case 2
                aLine(1) = lvw_consult.SelectedItem.Text
                Text1.Text = Text1.Text & vbCrLf & lvw_consult.SelectedItem.Text
            Case 3
                aLine(2) = lvw_consult.SelectedItem.Text
                Text1.Text = Text1.Text & vbCrLf & lvw_consult.SelectedItem.Text
            Case 4
                i = InStr(1, Text1.Text, vbCrLf)
                myFirstLine = Mid(Text1.Text, 1, i)
                Text1.Text = aLine(0) & vbTab & lvw_consult.SelectedItem.Text & vbCrLf & aLine(1) & vbCrLf & aLine(2)
          Case 5
               Dim myText As String
                myText = Replace(Text1.Text, vbCrLf, "x")
                i = InStr(1, myText, "x")
                myFirstLine = Mid(myText, 1, i - 1)
                mySecondLine = Mid(myText, i + 1)
                i = InStr(1, mySecondLine, "x")
                mySecondLine = Mid(mySecondLine, 1, i - 1) & vbTab & List1.Text
                i = InStrRev(myText, "x")
                myThirdLine = Mid(myText, i + 1)
                Text1.Text = myFirstLine & vbCrLf & mySecondLine & vbCrLf & myThirdLine
            End Select
    I'm using on error resume next at the beginning of the code.
    When reaching case 5, line two is deleted and replaced by the fifth item just added.
    if I drop on error resume next , I get subscript out of range in this line:
    Code:
    aLine(0) = lvw_consult.SelectedItem.Text
    thanks

  19. #19
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    REMEMBER, I am using a LISTBOX (List1), while you are using a listVIEW (lvw)....the code you COPIED from me has "List1.Text" in Case 5.

    As far as your error in aLine(0), you will have to figure that one out.

    PS---I avoid ListViews whenever I can...I am not in favor of that control at all.
    Sam I am (as well as Confused at times).

  20. #20
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    You don't need the aLine array...delete it. I was using it as an EXAMPLE, but you never figured out what I was doing, so I never used it in my further iterations either.

    EDIT: No, I'm sorry, I used it in case 4...you will need to dim it with a value (I had initially dimmed to hold 9 values (Dim aLine(8) as String). You dimmed as zero and never redimmed it, that is your issue with that error. I am surprised you got a proper display at case 4.
    Last edited by SamOscarBrown; Aug 30th, 2021 at 02:38 PM.
    Sam I am (as well as Confused at times).

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: Export items from Listview to textbox

    REMEMBER, I am using a LISTBOX (List1), while you are using a listVIEW (lvw)....the code you COPIED from me has "List1.Text" in Case 5.
    I'm sorry for my mistake.
    Now it isworking
    However I still can't figure out why I'm having the error in aLine(0)

  22. #22
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    Redone FOR YOU....

    Code:
    Option Explicit
        Dim iNumItemsUsed As Integer
        Dim myFirstLine As String
        Dim mySecondLine As String
        Dim myThirdLine As String
    
    
    Private Sub Form_Load()
        List1.AddItem ("AAAAA")
        List1.AddItem ("BBBBB")
        List1.AddItem ("CCCCC")
        List1.AddItem ("DDDDD")
        List1.AddItem ("EEEEE")
        List1.AddItem ("FFFFF")
        List1.AddItem ("GGGGG")
    End Sub
    
    
    Private Sub List1_Click()
        iNumItemsUsed = iNumItemsUsed + 1
        Dim i As Integer
    
    
        Select Case iNumItemsUsed
            Case 1
                Text1.Text = List1.Text
                myFirstLine = List1.Text
             Case 2
                Text1.Text = Text1.Text & vbCrLf & List1.Text
                mySecondLine = List1.Text
            Case 3
    
    
                Text1.Text = Text1.Text & vbCrLf & List1.Text
                myThirdLine = List1.Text
            Case 4
                
                i = InStr(1, Text1.Text, vbCrLf)
                myFirstLine = Mid(Text1.Text, 1, i - 1)
                myFirstLine = myFirstLine & vbTab & List1.Text
                Text1.Text = myFirstLine & vbCrLf & mySecondLine & vbCrLf & myThirdLine
            Case 5
                Dim myText As String
                myText = Replace(Text1.Text, vbCrLf, "x")
                i = InStr(1, myText, "x")
                myFirstLine = Mid(myText, 1, i - 1)
                mySecondLine = Mid(myText, i + 1)
                i = InStr(1, mySecondLine, "x")
                mySecondLine = Mid(mySecondLine, 1, i - 1) & vbTab & List1.Text
                i = InStrRev(myText, "x")
                myThirdLine = Mid(myText, i + 1)
                Text1.Text = myFirstLine & vbCrLf & mySecondLine & vbCrLf & myThirdLine
        End Select
    End Sub
    Sam I am (as well as Confused at times).

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: Export items from Listview to textbox

    unfortunatel case 4 is collapsing the list in the textbox.
    I have to keep the code in #16.
    thank you very much.
    I'm so sorry for bothering you

  24. #24
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Export items from Listview to textbox

    Then you are doing something different than what I posted
    Sam I am (as well as Confused at times).

  25. #25

    Re: Export items from Listview to textbox

    thank you for your interest

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