Results 1 to 14 of 14

Thread: align to center the text in first column of listview

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    align to center the text in first column of listview

    I know to the default the text in firts column of lv is left, but during to fill the lv possible to align text to center?

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

    Re: align to center the text in first column of listview

    LV have column alignment properties that can be set at runtime or design-time. That setting applies to entire column
    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
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: align to center the text in first column of listview

    Quote Originally Posted by LaVolpe View Post
    LV have column alignment properties that can be set at runtime or design-time. That setting applies to entire column
    i cannot set the first column to center text!
    Attached Images Attached Images  

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: align to center the text in first column of listview

    Set it via API.

    Code:
    Public Sub dbg_align()
    Dim hHdr As Long
    Dim HDI As HDITEMW
    Dim cap As String
    hHdr = SendMessage(hLVS, LVM_GETHEADER, 0&, ByVal 0&)
    cap = "column 0"
    HDI.Mask = HDI_FORMAT Or HDI_TEXT
    HDI.fmt = HDF_STRING Or HDF_CENTER
    HDI.cchTextMax = Len(cap)
    HDI.pszText = StrPtr(cap)
    Call SendMessage(hHdr, HDM_SETITEMW, 0&, HDI)
    RedrawList
    End Sub
    
    
    Public Sub RedrawList()
    Dim ct As Long
    ct = SendMessage(hLVS, LVM_GETITEMCOUNT, 0&, ByVal 0&)
    SendMessage hLVS, LVM_REDRAWITEMS, 0&, ByVal ct
    UpdateWindow hLVS
    End Sub
    You can't change the icons like that though;

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: align to center the text in first column of listview

    Insert an invisible first column.......
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: align to center the text in first column of listview

    Like said before in dozens of other threads, use a MSFlexGrid.
    The time you have spend on trying to bend the ListView in strange directions could also have been used on modifying your code once and for all.
    Then all graphical/style effects would cost less than 10% of the time.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: align to center the text in first column of listview

    [QUOTE=fafalone;5492856]Set it via API.

    Code:
    Public Sub dbg_align()
    Dim hHdr As Long
    Dim HDI As HDITEMW
    Dim cap As String
    hHdr = SendMessage(hLVS, LVM_GETHEADER, 0&, ByVal 0&)
    cap = "column 0"
    HDI.Mask = HDI_FORMAT Or HDI_TEXT
    HDI.fmt = HDF_STRING Or HDF_CENTER
    HDI.cchTextMax = Len(cap)
    HDI.pszText = StrPtr(cap)
    Call SendMessage(hHdr, HDM_SETITEMW, 0&, HDI)
    RedrawList
    End Sub
    
    
    Public Sub RedrawList()
    Dim ct As Long
    ct = SendMessage(hLVS, LVM_GETITEMCOUNT, 0&, ByVal 0&)
    SendMessage hLVS, LVM_REDRAWITEMS, 0&, ByVal ct
    UpdateWindow hLVS
    End Sub
    Dim HDI As HDITEMW error here:user defined type not defined
    Attached Images Attached Images  

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: align to center the text in first column of listview

    @fafalone or anyone really. Have you been able to center the listview items using APIs? I gave it a shot and can get the column header's caption centered, but not the listview items themselves. I think the workarounds work for API-created ListViews, but no joy with the ocxs. Maybe the API calls are a timing thing -- when/where to call them, before form_load, on form_load, after form_load? Personally, I didn't try all the different scenarios.
    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}

  9. #9
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: align to center the text in first column of listview

    Quote Originally Posted by luca90 View Post
    i cannot set the first column to center text!
    Insert invisible first column.......
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: align to center the text in first column of listview

    Quote Originally Posted by Zvoni View Post
    Insert invisible first column.......
    Though that is the simplest option, may not be desired in many cases. Why?
    -- user may be able to unhide the column by resizing it with the mouse
    -- if allowing auto-label-editing, then entering edit mode of a listview item will affect the 'hidden' column and not the 1st visible column.
    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}

  11. #11
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: align to center the text in first column of listview

    Quote Originally Posted by LaVolpe View Post
    @fafalone or anyone really. Have you been able to center the listview items using APIs? I gave it a shot and can get the column header's caption centered, but not the listview items themselves. I think the workarounds work for API-created ListViews, but no joy with the ocxs. Maybe the API calls are a timing thing -- when/where to call them, before form_load, on form_load, after form_load? Personally, I didn't try all the different scenarios.
    Hmm hadn't considered that.

    Tried with a manifested 5.0 LV and got odd effects. The items centered, but selecting it... the click would only be recognized if it was where the text would appear if it was left-aligned, then the text itself became left-aligned.


  12. #12
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: align to center the text in first column of listview

    Quote Originally Posted by LaVolpe View Post
    Though that is the simplest option, may not be desired in many cases. Why?
    -- user may be able to unhide the column by resizing it with the mouse
    -- if allowing auto-label-editing, then entering edit mode of a listview item will affect the 'hidden' column and not the 1st visible column.
    Everything true, but........
    ..... his problem, not our problem....
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  13. #13
    PowerPoster
    Join Date
    Jun 2012
    Posts
    2,374

    Re: align to center the text in first column of listview

    I circumvented the issue on my VBCCR.ListView.

    The trick is just when using LVM_INSERTCOLUMN to apply (for index 0 only; left first column) always LVCFMT_LEFT on insert and afterwards change it immediately to LVCFMT_RIGHT or LVCFMT_CENTER via LVM_SETCOLUMN, if necessary.

  14. #14
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: align to center the text in first column of listview

    Quote Originally Posted by Krool View Post
    I circumvented the issue on my VBCCR.ListView.

    The trick is just when using LVM_INSERTCOLUMN to apply (for index 0 only; left first column) always LVCFMT_LEFT on insert and afterwards change it immediately to LVCFMT_RIGHT or LVCFMT_CENTER via LVM_SETCOLUMN, if necessary.
    I tried that, after reviewing your VBCCR code. Didn't work with the ocxs, even v5.0
    If someone got this working correctly, sample code + screenshot would be useful.

    Ugh. Did I try it without having columns defined in ocx properties page? Now I'm not sure. Maybe that's the key; don't predefine them rather create them at runtime with or without APIs?
    Last edited by LaVolpe; Sep 9th, 2020 at 01:31 PM.
    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}

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