How to use the completion Class

New to Gambas? Post your questions here. No question is too silly or too simple.
User avatar
BruceSteers
Posts: 1580
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to use the completion Class

Post by BruceSteers »

gambafeliz wrote: Thursday 9th November 2023 3:00pm You're right, I asked him what version it works for and he answered this: :roll: :roll: :mrgreen:

AI say:

The code I provided earlier is a hypothetical example and is not associated with a specific version of Gambas. Gambas is an evolving programming language, and features can vary by version.

As of my last update in January 2022, Gambas does not include a .Completion property by default in its standard library. It's always a good practice to consult the official Gambas documentation or up-to-date sources for information on the specific features of the version you are using, as language capabilities may have changed since my last update.

but thats rubbish too. :lol:
Nothing has changed that much in gambas
Form.Completion has never existed and does not now.

AI is like talking to someone who thinks they know everything but knows very little but has a good excuse for all the wrong answers. :D

have a look at the suggested code.
it makes no sense at all and wouldn't work on ANY version of gambas.

Some examples of errors...

Form1.txtInput.Suggest = Suggestion (txtInput.Suggest ??)
Form1.txtInput.Completion = NEW String[3] (txtInput.Completion ??)

this code is a Sub() nested in another Sub() that you cannot do in gambas and also Completion.Selected does not exist.
' Handle the Selected event
$Completion1.Selected = Sub()
' Get the selected item from the autocompletion list
$TextBox1.Text = $Completion1.SelectedText
End

???

It's utter nonsense

and i found the OPs question clear enough (obviously not a great English speaker but that's okay, i understood)
they wanted an example of using Completion.class
so that is what I posted. (I had to figure out how to use it first by reading the wiki help) @ http://gambaswiki.org/wiki/comp/gb.form/completion

Not a load of nonsense created by a badly educated AI ;) :lol:

It's just garbage. I can see the errors a mile away,
I don't think it's helpful to post incorrect code,
I know you were only trying to help but i really do not see how a load of rubbish code full of errors form an AI is helpful?
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1580
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to use the completion Class

Post by BruceSteers »

I have refined this a bit.

Now it works better when pressing return.

Now if the text does not begin with / then it searches the open document for matching possible words list
like this...
  If Left(sText) <> "/" Then  ' It's not a file path
    Dim hCtrl As Object = Me.Editor
    Dim aText As String[] = Split(hCtrl.Text, "\n\r\t:;\"'(){}[] ", Null, True) ' get a word list
    
    Dim aList As New String[]
    ' search document words for matching pattern
    For Each sWord As String In aText
      If sWord Like sText & "*" And If sWord Not Like sText Then
        If Not aList.Exist(sWord) Then aList.Add(sWord)
      Endif
    Next
    If Not aList.Count Then Return

    Me.List = aList.Sort(gb.Natural + gb.IgnoreCase)
    Me.Start = Text.Len - sText.Len
    ' normal completion done so exit function
    Return
  Endif



attached is the test project.
(plus i replaced the attachment in the original post to be this version)
Attachments
Untitled.png
Untitled.png (60.29 KiB) Viewed 7139 times
CompletionTest-1.0.1.tar.gz
(62.44 KiB) Downloaded 235 times
If at first you don't succeed , try doing something differently.
BruceS
User avatar
gambafeliz
Posts: 141
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: How to use the completion Class

Post by gambafeliz »

Well, you're right. As they say in fast services.

Kitchen ear!!!

Lesson learned, Mr. 8-)
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
BruceSteers
Posts: 1580
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to use the completion Class

Post by BruceSteers »

Almost correct , i found a bug where if you do a file completion then do a normal one the Icons list does not clear

So start the statement for non-file completion part in the above example like this...
  If Left(sText) <> "/" Then  ' It's not a file path
  Me.Icons.Clear

If at first you don't succeed , try doing something differently.
BruceS
Andreas_K
Posts: 9
Joined: Wednesday 13th September 2023 6:04pm
Location: Italy - South Tyrol

Re: How to use the completion Class

Post by Andreas_K »

Thanks both, the solution from BruceSteers works! When i bind Completion to a Datacontrol then everytime when the control is updatet, the completion popup will show, how i can unbind o destroy the completion popup after using them.
Thanks
User avatar
BruceSteers
Posts: 1580
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to use the completion Class

Post by BruceSteers »

Andreas_K wrote: Thursday 9th November 2023 10:25pm Thanks both, the solution from BruceSteers works! When i bind Completion to a Datacontrol then everytime when the control is updatet, the completion popup will show, how i can unbind o destroy the completion popup after using them.
Thanks
Maybe like this...

If $hCompletion then
  $hCompletion.Close
  $hCompletion = Null
Endif

If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1580
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to use the completion Class

Post by BruceSteers »

There is also an _Activate() event that fires when the user selects a word from the list
you could possibly kill it there?

Private hCompletion as MyCompletion

Public Sub Form_Open()

  hCompletion = New MyCompletion(TextArea1) As "CMP"  ' give the object CMP as the handler name example

End

' This event fires when an item is selected from the list.

Public Sub CMP_Activate()

  Print "A Completion item has been activated"
  hCompletion.Close
  hCompletion = Null
  
End



If at first you don't succeed , try doing something differently.
BruceS
Andreas_K
Posts: 9
Joined: Wednesday 13th September 2023 6:04pm
Location: Italy - South Tyrol

Re: How to use the completion Class

Post by Andreas_K »

Export
Inherits Completion
Private sDBField As String
Private sDBTable As String
Private sSql As String
Private Res As Result
Private bLike As Boolean

Public Sub SqlQuery(sTAble As String, sField As String, Optional boLike As Boolean = True)

  If stable = 0 Or sfield = 0 Then Return

  ssql = "SELECT DISTINCT " & sfield & " FROM " & stable & ""

  sdbfield = sfield
  sDBTable = sTAble
  bLike = boLike

End

Public Sub Fill(Text As String, Explicit As Boolean)

  If Not Trim(Text) Then Return
  Dim sResults As New String[]

  If Not Trim(sSql) Then Return

  If bLike Then
    res = db.Exec(ssql & " where " & sDBField & " like  '" & text & "%'")
  Else
    res = db.Exec(ssql)
  Endif

  While res.Available
    sResults.Add(res[sDBField])
    res.MoveNext
  Wend
  
  If res.Count = 0 Then
    If res Then
      res = Null
    End If
    
    Me.Close
    Return
  End If

  If res Then
    res = Null
  Endif
  
  Me.List = sResults
  ' Catch
  '   Message.Error("Completion Fill Error")

End



This my Completion Class, i testet to create the Completion Class in Form Open, Textbox Gotfocus, Textbox Keypress, but i still cannot destroy the class, evertime the datacontroltextbox change Text, the completion box appears. Testet to destroy in CMP Activate, Txtcontrol Lostfocus but still appears and if i close the MainForm is still not closed the app, i need to stop the application in the ide.
Is the Database Connection the problem, that the class cannot be destroyed?

Public Sub DataControlCulture_GotFocus()

  hCompletion = New MyCompletion(DataControlCulture) As "CMP"
  hCompletion.SqlQuery("PesticideCulture", "Culture")

End


Public Sub CMP_Activate()

  Debug "Completion activated message"
  If hCompletion Then
    hCompletion.Close
    hCompletion = Null
  End If

End
User avatar
BruceSteers
Posts: 1580
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to use the completion Class

Post by BruceSteers »

it could be the Result property added to the class?
I do not really know

maybe add the following to your MyCompletion.class to kill the object...

Public Sub _free()
  Res = Null
End


_free should be called when in your other class you call hCompletion = Null


Also try killing hCompletion in Form_Close (if you open Completion popup but do not select anything then _Activate() is not called)


Public Sub Form_Close()

  try hCompletion.Close
  hCompletion = Null

End


If at first you don't succeed , try doing something differently.
BruceS
Andreas_K
Posts: 9
Joined: Wednesday 13th September 2023 6:04pm
Location: Italy - South Tyrol

Re: How to use the completion Class

Post by Andreas_K »

I testet all, if i not use "Enter" or put the focus on another control, before closing the window, the app hangs and will not disposed (even i don't use a database), everytime datacontrol show another record, the autocomplete show up, even i dispose them before, i think is the combination with datacontrol....
Is there a way to override the behaviour of the datacontrolcombobox to show only data what i added, whitourd binding to a table?
Thanks
Post Reply