Useful Links Code

Post your Gambas programming questions here.
Post Reply
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Useful Links Code

Post by Quincunxian »

I'm writing a Gambas Project Review Utility and I wanted to include some useful links as references for various Gambasy things such as database, html and graphics references.

I created a form with a single scroll view with the background set to Color.White.
It dynamically adds URLLabels with the Tooltip set to display the actual hyperlink and a short description.
This data is contained in a small txt file (Links.Txt) in a format of:
Displayed Name | HyperLink | Short Description.

Clicking on any URLLabel opens the default browser with the address coming from URLLabel.Link.
The order of display is the order in which the lines of the file is read.
Image

Form Code as below.
' Gambas class file

Private NewLink As URLLabel
Private TmpAry As String[]

Public Sub Form_Open()
  Dim Buffer As Integer = 3
  Dim FileName As String = Application.Path &/ "Links.Txt" '{Change this for the actual location of the file}
  Dim LinkFile As File
  Dim TmpLine As String = ""
  Dim TmpInt as Integer = 0  ' <----<< This code was missing from the Original Post. (Edit)
  Dim Ylocation As Integer = Buffer
  Dim SetOdd As Boolean = True

  Scv_Links.Width = Me.Width - 10
  Scv_Links.Height = Me.Height - 10

  If Exist(FileName) Then
    Scv_Links.Children.Clear
    LinkFile = Open FileName For Input
    While Not Eof(LinkFile)
      Line Input #LinkFile, TmpLine
      If ((Left(TmpLine, 1) <> "#") Or (Trim(TmpLine) <> "")) Then
        TmpAry = Split(TmpLine, "|")
        If TmpAry.Count = 3 Then
          NewLink = New URLLabel(Scv_Links) As "Link#" & Str(TmpInt)
          NewLink.Text = TmpAry[0]
          NewLink.Link = TmpAry[1]
          NewLink.Tooltip = TmpAry[1] & Gb.CrLf & TmpAry[2]
          NewLink.Height = 24
          NewLink.Width = Scv_Links.Width - 25
          NewLink.Left = 5
          NewLink.Top = YLocation
          YLocation += Buffer + NewLink.Height
          If SetOdd Then
            NewLink.Background = Color.RGB(195, 223, 223)
            SetOdd = False
          Else
            NewLink.Background = Color.RGB(255, 255, 223)
            SetOdd = True
          Endif
          NewLink.Foreground = Color.LinkForeground
        Endif
      Endif
    Wend
  Endif
End

Public Sub Form_KeyPress()
  If Key.Code = Key.Esc Then Me.Close
End
Attachments
Links Text File.zip
(833 Bytes) Downloaded 377 times
Last edited by Quincunxian on Friday 15th March 2019 11:50pm, edited 3 times in total.
Cheers - Quin.
I code therefore I am
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Useful Links Code

Post by cogier »

I get an error: -

Unknown identifier: Tmpint in FMain.class:25
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Useful Links Code

Post by Quincunxian »

I had some other code that was linked to my Program so must have removed it when I took that out.

For those who have already copied the code
Dim TmpInt as Integer = 0
just needs to be amended to variable declaration at the top of the sub

I'll do an edit and put it back in
Cheers - Quin.
I code therefore I am
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Useful Links Code

Post by cogier »

Hi Quin,

Matt and I have looked at your code and made a few changes. Does this help?
Test1.tar.gz
(12.61 KiB) Downloaded 393 times
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Useful Links Code

Post by Quincunxian »

I Like it !.

When did the array.Max turn up or has it always been there ?
Apparently it appears in other controls as well ie: ScrollView1.Children.Max
I assume another little gem from Benoît and that's going to be very useful indeed. !

If {test} Then Continue ?
Now that's a new one on me - Let me think about that one as I need to get my head around how that works
in an IF - Then - Else clause ?

Much cleaner code over all - Nice to lean some new things I must say.

The bit that has me a bit perplexed is :
NewLink = New URLLabel(ScrollView1) As "Link"

The 'As ("Link")' code is setting the Name element of the control and it 'should' be unique which is why I added the Str(Number) to it at each creation. Obviously different for created controls.
I'm wondering what would happen if you wanted to do more complex functions with it ?
I'll play with that one and see if there are any exceptions or perhaps the compiler creates a unique token code for each new control ?

Cheers,
A lot of food for thought.
Quin.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Useful Links Code

Post by cogier »

When did the array.Max turn up or has it always been there ?
It's been around for some time, it is just the same as .Count -1

Continue jumps to Next, try this: -
Public Sub Form_Open()

For iCount As Integer = 1 To 10
  If Odd(iCount) Then Continue
  Print iCount
Next

'Result = 2, 4, 6, 8, 10

End
The 'As ("Link")' code is setting the Name element ....
The program does not actually need the As "Link" to run but I have added a routine that will show you how you could use it. There is no need add separate links for each URLLabel as you can use Last to find out which item was Clicked, Entered or whatever.
Test1.tar.gz
(13.73 KiB) Downloaded 361 times
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: Useful Links Code

Post by Cedron »

I learned "File.Load", "array.Max", and "Odd()" from this. Thanks.
.... and carry a big stick!
Post Reply