[Gambas-user] help with ListView code?

Tobias Boege taboege at ...626...
Fri Jul 28 15:47:56 CEST 2017


On Thu, 27 Jul 2017, mikeB wrote:
> Greetings all,
> I've got a Listview (set as Column view) with 2 columns that I'm using
> 
> for testing so I might understand how all this works so can add it to a
> project.
> 
> testCode..................................................
> With listview1
>     .Clear
>     .Resizable = True
>     .Columns.Count = 2
>     .Columns[0].Text = "Site Name:"
>     .Columns[1].Text = "Site URL:"
> End With
> ..................................................................
> I can add values to the 1st column:
> code..........................................................
> listview1.add("1", "First var entry")
> listview1.add("2", "Second var entry to test the resize ability")
> listview1.add("3", "Third var entry")
> ...................................................................
> Have spent a lot of research time trying to figure out how to add
> values to the 2nd column (Site URL:) - got me stumped.
> 
> Anyone got an example line of code to help me out?
> 
> I can't seem to find the answer in the help docs.
> 
> Would greatly appreciate any/ all help;-)
> mikeB
> 

You don't really have a ListView but a ColumnView, do you? A ColumnView is
a ColumnView and not "a Listview (set as Column view)". The ListView control
doesn't have a Columns property.

I have to say that when I tried to answer your question, I noticed that
I had no clue how ListView, its sibling ColumnView or their mutual parent
_TreeView work, so I read their source code (see gb.gui.base) -- and I
still had no clue. And let me say that this happens rarely to me these days.
I had to dig up an example (the FileView class in gb.form is very nice)
to see what they actually do with their columns. Since I get it now, I want
to give an explanation of these classes first, so bear with me.

To understand ColumnView you have to understand _TreeView, because
ColumnView is basically a _TreeView with some code added to manage columns,
but the columns are already available from _TreeView.

To add to the confusion, a _TreeView is internally basically a GridView,
i.e. a square grid of cells in which you can put strings and pictures,
but you can't access these cells /that/ freely from a _TreeView. The key
information is that each _TreeView item is an entire *row* in the GridView.

By calling ColumnView.Add("1", "First var entry") you create a new row
in the GridView, identified by the key "1", and set the text of the first
column of that row to "First var entry". The return value of the Add()
method is a _TreeView_Item object which allows you to set the texts of
all other columns:

  ColumnView.Add("1", "First var entry")[1] = "second column"

Wrap it in a With or store it in a variable if you want to set many other
columns:

  Dim hItem As _TreeView_Item

  hItem = ColumnView.Add("1", "First var entry")
  hItem[1] = "second column"
  hItem[2] = "third column"

The asymmetric treatment of the first vs. the other columns is most likely
because you always want to set at least the first column's text, and maybe
of technical nature as the Add() method has optional parameters which make
it uglier to add a variable argument list (Param in gb) to Add().

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list