dcsimg
Results 1 to 12 of 12

Thread: Problem when sending data from Listview to textbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    427

    Problem when sending data from Listview to textbox

    Hello VbForums
    I'm sending data from listview to multiline textbox
    Code:
    Dim intI As Integer
    For intI = 1 To ListView1.ListItems.Count
      txt_Desig.Text = text_Desig.Text & vbNewLine & ListView1.ListItems(intI).SubItems(1) & " - " & ListView1.ListItems(intI).SubItems(2) & " - " & _
      ListView1.ListItems(intI).SubItems(3)
        
    txt_price.Text = txt_price.Text & vbNewLine & ListView1.ListItems(intI).SubItems(4)
    Next intI
    This is the output
    Name:  02-08-2019 22-59-30.png
Views: 141
Size:  1.4 KB
    As you can notice, When a line is long it is creating a problem.
    Any suggestion please?
    thank you

  2. #2
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    773

    Re: Problem when sending data from Listview to textbox

    Making txt_Desig wider seems like the obvious suggestion.

    Another option is to set the ScrollBars property of txt_Desig to "1 - Horizontal" while in design mode, which should prevent the wrapping you show in your screenshot, but will require scrolling left/right to see the entire contents of lengthy lines.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    427

    Re: Problem when sending data from Listview to textbox

    Thank you OptionBase1 for the suggestions
    but I don't want to use scrollbars

  4. #4
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    773

    Re: Problem when sending data from Listview to textbox

    Well, then maybe it would be best to either explain how you would like the result to look, or post an image of what you would like the end result to look like. As it stands, I'm not keen to make any more suggestions and risk playing more guessing games of what you do or don't want to use.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    427

    Re: Problem when sending data from Listview to textbox

    thanks again OptionBase1
    this is the output i wish to have
    Name:  02-08-2019 22-59-30.png
Views: 62
Size:  1.4 KB

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,430

    Re: Problem when sending data from Listview to textbox

    Sounds like a job for a FlexGrid to me.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,430

    Re: Problem when sending data from Listview to textbox

    Example:

    Code:
    Option Explicit
    
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    
    Private Enum DrawTextFormats
        DT_BOTTOM = &H8&
        DT_CALCRECT = &H400&
        DT_CENTER = &H1&
        DT_EDITCONTROL = &H2000&
        DT_END_ELLIPSIS = &H8000&
        DT_EXPANDTABS = &H40&
        DT_EXTERNALLEADING = &H200&
        DT_HIDEPREFIX = &H100000
        DT_INTERNAL = &H1000&
        DT_LEFT = &H0&
        DT_MODIFYSTRING = &H10000
        DT_NOCLIP = &H100&
        DT_NOFULLWIDTHCHARBREAK = &H80000
        DT_NOPREFIX = &H800&
        DT_PATH_ELLIPSIS = &H4000&
        DT_PREFIXONLY = &H200000
        DT_RIGHT = &H2&
        DT_SINGLELINE = &H20&
        DT_TABSTOP = &H80&
        DT_TOP = &H0&
        DT_VCENTER = &H4&
        DT_WORDBREAK = &H10&
        DT_WORD_ELLIPSIS = &H40000
    End Enum
    
    Private Declare Function DrawText Lib "user32" Alias "DrawTextW" ( _
        ByVal hDC As Long, _
        ByVal lpStr As Long, _
        ByVal nCount As Long, _
        ByRef RECT As RECT, _
        ByVal wFormat As DrawTextFormats) As Long
    
    Private Function GetWrappedTextHeight(ByVal Width As Single, ByVal Text As String) As Single
        'Uses the invisible Picture1, assume its font the same as the font to measure.
        'Dimensions are in ScaleMode units, best if Twips when working with MSHFlexGrids.
        Dim RECT As RECT
    
        RECT.Right = ScaleX(Width, ScaleMode, vbPixels)
        GetWrappedTextHeight = ScaleY(DrawText(Picture1.hDC, _
                                               StrPtr(Text), _
                                               Len(Text), _
                                               RECT, _
                                               DT_CALCRECT _
                                            Or DT_EDITCONTROL _
                                            Or DT_LEFT _
                                            Or DT_NOPREFIX _
                                            Or DT_WORDBREAK), _
                                      vbPixels, _
                                      ScaleMode)
    End Function
    
    Private Sub Form_Load()
        With MSHFlexGrid1
            .Move 0, 0, ScaleWidth, ScaleHeight
            .GridLines = flexGridNone
            .BackColorBkg = .BackColor
            .FixedCols = 0
            .ColWidth(0) = .Width - 1500 - ScaleX(5, vbPixels, vbTwips)
            .ColAlignment(0) = flexAlignLeftTop
            .ColWordWrapOption(0) = flexWordBreak
            .TextMatrix(0, 0) = "Designation"
            .ColWidth(1) = 1500
            .ColAlignment(1) = flexAlignLeftTop
            .TextMatrix(0, 1) = "Price"
    
            Set Picture1.Font = .Font
    
            .TextMatrix(.Rows - 1, 0) = "1. xxxxx xxxxx xxxxxxx xxxxxxx xxxxxxxxxx " _
                                      & "xxx xxxxxxxxxxxxxxx xxxxxxxxxxxxx"
            .RowHeight(.Rows - 1) = GetWrappedTextHeight(.ColWidth(0), .TextMatrix(.Rows - 1, 0)) _
                                  + ScaleY(5, vbPixels, vbTwips)
            .TextMatrix(.Rows - 1, 1) = "1000"
    
            .Rows = .Rows + 1
            .TextMatrix(.Rows - 1, 0) = "2. yy yyyy yyyyy"
            .RowHeight(.Rows - 1) = GetWrappedTextHeight(.ColWidth(0), .TextMatrix(.Rows - 1, 0)) _
                                  + ScaleY(5, vbPixels, vbTwips)
            .TextMatrix(.Rows - 1, 1) = "3000"
    
            .Rows = .Rows + 1
            .TextMatrix(.Rows - 1, 0) = "3. wowow wow wowowowow"
            .RowHeight(.Rows - 1) = GetWrappedTextHeight(.ColWidth(0), .TextMatrix(.Rows - 1, 0)) _
                                  + ScaleY(5, vbPixels, vbTwips)
            .TextMatrix(.Rows - 1, 1) = "3750"
    
            .Rows = .Rows + 1
            'Half-baked synthetic hanging indent:
            .TextMatrix(.Rows - 1, 0) = "4. zzzzz zzzzz zzzzz zz z zzzzzzzz" & vbNewLine _
                                      & "   zzzzzzzzz"
            .RowHeight(.Rows - 1) = GetWrappedTextHeight(.ColWidth(0), .TextMatrix(.Rows - 1, 0)) _
                                  + ScaleY(5, vbPixels, vbTwips)
            .TextMatrix(.Rows - 1, 1) = "4000"
    
            .Rows = .Rows + 1
            .TextMatrix(.Rows - 1, 0) = "5. aaa a"
            .RowHeight(.Rows - 1) = GetWrappedTextHeight(.ColWidth(0), .TextMatrix(.Rows - 1, 0)) _
                                  + ScaleY(5, vbPixels, vbTwips)
            .TextMatrix(.Rows - 1, 1) = "7000"
        End With
    End Sub

    Name:  sshot.png
Views: 61
Size:  2.5 KB

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    427

    Re: Problem when sending data from Listview to textbox

    dilettante thank you very much
    A very professional work
    however I'm getting a vriable not defined in this line
    Code:
      .ColWordWrapOption(0) = flexWordBreak
    Sorry I have never worked with a MSHFlexGrid
    When I removed the line I get this
    Name:  02-08-2019 22-59-30.png
Views: 43
Size:  1.8 KB

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,430

    Re: Problem when sending data from Listview to textbox

    Are you trying to use the creaky old ANSI-only MSFlexGrid instead of the MSHFlexGrid which replaced it?

    Also if your Form is wider than the one I was using you might want to be careful not to resize the control to the full width.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    427

    Re: Problem when sending data from Listview to textbox

    Sorry sir
    I was using MsFlexGrid instead of MsHFlexGrid
    Now I need to add other columns but I get subscript out of range
    Code:
    .ColWidth(2) = 1500
            .ColAlignment(2) = flexAlignLeftTop
            .TextMatrix(0, 2) = "Quantity"

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2015
    Posts
    427

    Re: Problem when sending data from Listview to textbox

    sorry again sir
    I think I should use properties to add new colums

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,430

    Re: Problem when sending data from Listview to textbox

    A default instance of this control has 2 columns. You can change that at design-time or in code by assigning another value to its Cols property.

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