dcsimg
Results 1 to 18 of 18

Thread: [RESOLVED] export listview to textbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    454

    Resolved [RESOLVED] export listview to textbox

    Hello VbForums community
    I'm exporting items from lisview to multiline textbox on item check event

    clicknum = clicknum + 1
    Code:
    Select Case clicknum
        
        Case 1
            Text1.Text = vbNewLine & "  1- " & Item.Text
        Case 2
            Text1.Text = Text1.Text & vbNewLine & vbNewLine & "  2- " & _
            Item.Text
        End select
    I'm looking for a way to remove the item on uncheck event.
    For example I checked two items then I figured out that I was mistaken.
    So instead of removing the item from the textbox, I want to uncheck it in the listview.
    I don't know if that possible

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,451

    Re: export listview to textbox

    Not sure your question is crystal clear. I don't understand the relationship between "remove item from the textbox" and "uncheck it in listivew"

    Do you know how to uncheck a listview item via code? Is that what you are asking?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    454

    Re: export listview to textbox

    LaVolpe
    thank you for your interest
    I have a listview and a multiline textbox.
    if I check an item in a lisview, it is exported to the textbox
    If I uncheck the same item in the listview, it is removed from the textbox
    that's what I'm hoping to do.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,947

    Re: export listview to textbox

    My first thought is to just rebuild the text in the text box. That would be a simple and foolproof way to do it and would be plenty fast so long as we are not talking a large number of items.
    You may also be able to locate the text in the text box using instr and then use replace to remove the desired text which may or may not be simple depending on what is actually being added and if there could be duplicates in there.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,451

    Re: export listview to textbox

    Oh, have fun with that...

    Then I am going to assume you know how to determine when a listitem becomes checked/unchecked. If it becomes unchecked, sounds like you have to find it in the textbox and remove that text line. Shouldn't be too hard since your textbox contents are carriage return delimited. Of course, if the numbering of other items written to the textbox need to change, then you'll have to consider that too, i.e., 3- now must be listed as 2-

    Of course, another option is simply to write a routine that fills the textbox, from scratch, on demand. If so, it would loop through the listview only writing to it the checked listview items.

    Edited: DataMiser types faster than me -- we pretty much said the same thing.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    454

    Re: export listview to textbox

    Quote Originally Posted by DataMiser View Post
    My first thought is to just rebuild the text in the text box. That would be a simple and foolproof way to do it and would be plenty fast so long as we are not talking a large number of items.
    Thank you DataMiser
    but I didn't understand rebuild the text in the text box
    Quote Originally Posted by DataMiser View Post
    You may also be able to locate the text in the text box using instr and then use replace to remove the desired text which may or may not be simple depending on what is actually being added and if there could be duplicates in there.


    this means I have to loop through the textbox lines and also loop through the checked items in the listview. Then use instr function
    If so I'm sure I'll spend a whole week before I get i work
    thank you

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    454

    Re: export listview to textbox

    Quote Originally Posted by LaVolpe View Post
    Oh, have fun with that...

    If it becomes unchecked, sounds like you have to find it in the textbox and remove that text line. Shouldn't be too hard since your textbox contents are carriage return delimited. Of course, if the numbering of other items written to the textbox need to change, then you'll have to consider that too, i.e., 3- now must be listed as 2-
    Thank you again LaVolpe but I could not grasp the point

    Quote Originally Posted by LaVolpe View Post
    another option is simply to write a routine that fills the textbox, from scratch, on demand. If so, it would loop through the listview only writing to it the checked listview items.
    thank you for this idea.

  8. #8
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    827

    Re: export listview to textbox

    This is as much code as I'm willing to provide to assist. It makes use of a checkbox array rather than a listview, but it demonstrates adding/removing the caption of each checkbox from the array to a multiline textbox as each checkbox is checked/unchecked.

    Code:
    Private Sub Check1_Click(Index As Integer)
    
      If Check1(Index).Value = vbChecked Then
        Text1.Text = Text1.Text & Check1(Index).Caption & vbCrLf
      Else
        Text1.Text = Replace(Text1.Text, Check1(Index).Caption & vbCrLf, "")
      End If
    
    End Sub

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    454

    Re: export listview to textbox

    thank you OptionBase1 but I don't think this code will serve for the listview

  10. #10
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    827

    Re: export listview to textbox

    Exactly. It wasn't intended to be "Copy and paste" ready for you to use with no thought or effort on your part. That being said, it does demonstrate one approach of accomplishing your goal. You would just need to put some work into it to make it work for your needs. Good luck.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    454

    Re: export listview to textbox

    Code:
    Private Sub lvw_ItemCheck(ByVal Item As MSComctlLib.ListItem)
    Text1.Text = ""
     clicknum = clicknum + 1
    Dim i As Long
    For i = 1 To lvw.ListItems.Count
    If lvw.ListItems.Item(i).Checked = True Then
        Select Case clicknum
       Case 1
            Text1.Text = vbNewLine & "  1- " & lvw.ListItems.Item(i).Text
        Case 2
            Text1.Text = Text1.Text & vbNewLine & vbNewLine & "  2- " & _
            lvw.ListItems.Item(i).Text
        Case 3
            Text1.Text = Text1.Text & vbNewLine & vbNewLine & "  3- " & _
            lvw.ListItems.Item(i).Text
        
        clicknum = 0
    
        End Select
    End If
    Next
        End Sub
    This is what I'm doing but I'm having a lot of trouble

  12. #12
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    827

    Re: export listview to textbox

    The first major issue I see is that you have massive code duplication for no reason. And what if it was possible to click 20 items in your listview? You are going to have 20 Case statements with almost identical code in each one?

    I'll allow for the fact that you want to treat the first item added to the TextBox differently than items 2 and beyond. Ok. So look at the code in Case 2 and Case 3. What is different? The difference is you are prepending a different number in front of the listview item that you add to the textbox. But, conveniently enough, that value you are prepending is the same value that you just confirmed is stored in the clicknum variable. So, you should be able to have a:

    Code:
    Case 1
      'Existing Case 1 code
    Case Else
      'Modify your existing case 2, 3, etc. code to remove the hardcoded numeric value and instead prepend the value stored in the clicknum variable
    All that being said, your clicknum = clicknum + 1 statement obviously needs to be inside of your loop for any of that subsequent logic to make sense, and also that clicknum = 0 statement can't be inside your for loop. Since you aren't defining clicknum inside of this Sub code you posted, it makes me concerned that it is defined at the form or module level and that you might be making changes to it elsewhere in code that you haven't posted, which would potentially cause problems in this code that you posted.

  13. #13
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,577

    Re: export listview to textbox

    Try this
    Code:
    Private Sub lvw_ItemCheck(ByVal Item As MSComctlLib.ListItem)
        Dim i As Long
        Dim n As Long
        Dim t As String
        
        For i = 1 To lvw.ListItems.Count
            If lvw.ListItems.Item(i).Checked = True Then
                n = n + 1
                t = t & CStr(n) & "- " & lvw.ListItems.Item(i).Text & vbNewLine
            End If
        Next
        Text1.Text = t
    End Sub



  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    454

    Re: export listview to textbox

    Case Else
    'Modify your existing case 2, 3, etc. code to remove the hardcoded numeric value and instead prepend the value stored in the clicknum variable
    Tthank you sir for your help but I couldn' understand this point.

  15. #15
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,947

    Re: export listview to textbox

    Quote Originally Posted by newbie2 View Post
    Thank you DataMiser
    but I didn't understand rebuild the text in the text box
    In more explicit terms, clear the text box, add the checked items to it. This of course should be done in a sub routine that gets called whenever you need to update the text in the text box.
    So long as there are not a huge number of items in the listview it should be fast and of course is probably the easiest way since you have to have code to put the items in the text box anyway.

    Now if you are like some who want to put tens of thousands of items in the listview that is another matter.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    454

    Re: export listview to textbox

    Case Else
    'Modify your existing case 2, 3, etc. code to remove the hardcoded numeric value and instead prepend the value stored in the clicknum variable
    Tthank you sir for your help but I couldn' understand this point.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    454

    Re: export listview to textbox

    Thank you all of you

    Special hanks to 4x2y
    this code worked great
    Code:
    Private Sub lvw_ItemCheck(ByVal Item As MSComctlLib.ListItem)
        Dim i As Long
        Dim n As Long
        Dim t As String
        
        For i = 1 To lvw.ListItems.Count
            If lvw.ListItems.Item(i).Checked = True Then
                n = n + 1
                t = t & CStr(n) & "- " & lvw.ListItems.Item(i).Text & vbNewLine
            End If
        Next
        Text1.Text = t
    End Sub

  18. #18
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    827

    Re: export listview to textbox

    Quote Originally Posted by newbie2 View Post
    Tthank you sir for your help but I couldn' understand this point.
    Fantastic.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width