Set ListView height to X items?

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Set ListView height to X items?

Post by Dude »

Is there a way I can set a ListViewGadget's height to be exactly 10 items high, no matter which font is in use? Basically I just need to know how to get a single item's height so I can multiply it. Thanks. I think it's to do with #LVM_GETITEMRECT but I'm not sure how to apply it.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Set ListView height to X items?

Post by netmaestro »

LVM_xxx messages are incompatible with PB's ListvewGadget. That's because, despite their name, they aren't MS Listview controls at all. They are actually ListBox controls that respond to LB_xxx messages. See if this slight modification of the PB sample code for the ListviewGadget is of some help:

Code: Select all

If OpenWindow(0, 0, 0, 270, 480, "ListViewGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   ListViewGadget(0, 10, 10, 250, 120)
   Debug SendMessage_(GadgetID(0), #LB_GETITEMHEIGHT,0,0)
   SetGadgetFont(0, LoadFont(0, "arial", 24))
   a = SendMessage_(GadgetID(0), #LB_GETITEMHEIGHT,0,0)
   Debug a 
   FreeGadget(0)
   ListViewGadget(0, 10, 10, 250, 10*a)
   
   SetGadgetFont(0, LoadFont(0, "arial", 24))
    For i = 1 To 10
      AddGadgetItem (0, -1, "Item " + Str(i) + " of the Listview") ; define listview content
    Next
    SetGadgetState(0, 0) ; set (beginning with 0) the tenth item as the active one
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
  
BERESHEIT
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Set ListView height to X items?

Post by Dude »

Thanks netmaestro, it's always so nice to get your quick and helpful replies. :)

For my project, however, I had to amend it like the following to work perfectly. Not sure where the 0.4 calculation comes from, but it's needed.

Code: Select all

If OpenWindow(0, 0, 0, 270, 400, "ListViewGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ListViewGadget(0, 10, 10, 250, 0)
  num = 10
  For i = 1 To num
    AddGadgetItem (0, -1, "Item " + Str(i) + " of the Listview")
  Next
  h = SendMessage_(GadgetID(0), #LB_GETITEMHEIGHT, 0, 0)
  ResizeGadget(0, #PB_Ignore, #PB_Ignore, #PB_Ignore, h * (num + 0.4) )
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: Set ListView height to X items?

Post by Lord »

Hello!

Is there also a way to get for a ListIconGadget the heights of an item?
Image
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Set ListView height to X items?

Post by Shardik »

Lord wrote:Is there also a way to get for a ListIconGadget the heights of an item?
Please be aware that your question is offtopic because this thread deals only with the ListViewGadget. In Linux and MacOS ListIconGadget and ListViewGadget have the same API basis but not on Windows as netmaestro already explained:
netmaestro wrote:LVM_xxx messages are incompatible with PB's ListvewGadget. That's because, despite their name, they aren't MS Listview controls at all. They are actually ListBox controls that respond to LB_xxx messages.
For ListIconGadgets I posted already a cross-platform example that demonstrates how to get and set the height of a row.
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

Re: Set ListView height to X items?

Post by Xanos »

If anybody is interested. The Linux (GTK) version of

Code: Select all

h = SendMessage_(GadgetID(0), #LB_GETITEMHEIGHT, 0, 0)
ist the following:

Code: Select all

Procedure getRowHeight(gadget)
  Protected height, yOffset
  Protected *tree, *tree_column
  *tree = GadgetID(gadget)
  
  ; https://developer.gnome.org/gtk3/stable/GtkTreeView.html#gtk-tree-view-get-column
  *tree_column = gtk_tree_view_get_column_(*tree, 0)
  
  ; https://developer.gnome.org/gtk3/stable/GtkTreeViewColumn.html#gtk-tree-view-column-cell-get-size
  gtk_tree_view_column_cell_get_size_(*tree_column, #Null, #Null, @yOffset, #Null, @height)
  
  ProcedureReturn height
EndProcedure
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
Post Reply