DirView question

Post your Gambas programming questions here.
Post Reply
bill-lancaster
Posts: 215
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

DirView question

Post by bill-lancaster »

Have found DirView a handy way of displaying directories.
Is there a way to select (in code) a particular directory in the display?
User avatar
BruceSteers
Posts: 1877
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: DirView question

Post by BruceSteers »

bill-lancaster wrote: Monday 20th May 2024 9:01am Have found DirView a handy way of displaying directories.
Is there a way to select (in code) a particular directory in the display?
Yes by accessing the Dirview's internal TreeView object


Public Sub Form_Open()

  DirView1.ShowHidden = True
  SelectDir(User.home &/ ".config", DirView1)

End


Private Sub SelectDir(Path As String, DView As DirView)
  
  Dim tv As TreeView = DView.Children[0]
  tv[Path].Selected = True

End




If you look at the DirView source code...
https://gitlab.com/gambas/gambas/-/blob ... =heads#L93

You can see that when it is is created is first adds 1 TreeView object to itself, so the DirView.Children[0] is a TreeView.
So with the above code..
Dim tv As TreeView = DView.Children[0]
tv is the TreeView, you can do all you can with a normal TreeView.

  Dim tv As TreeView = DView.Children[0]
  Print tv.Keys.Join("\n") ' list all the item keys

  tv[Path].Background = Color.yellow  ' make one item have yellow background
  tv[Path].RichText = "<font color=green><b>" & tv[Path].Text & "</b></font>" ' change font color of one item and make it bold.
If at first you don't succeed , try doing something differently.
BruceS
bill-lancaster
Posts: 215
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: DirView question

Post by bill-lancaster »

Does the trick nicely,
Thanks Bruce
Post Reply