How to use the completion Class

Post

Posted
Rating:
#1 (In Topic #1128)
Regular
Andreas_K is in the usergroup ‘Regular’
 I search a example how to use the completion class, can't find until now, thanks.
Online now: No Back to the top

Post

Posted
Rating:
#2
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
This example is developed by an AI in response to your question.

Code (gambas)

  1. PUBLIC SUB Main()
  2.  
  3.   DIM Calculator AS NEW CalculatorApp
  4.  
  5.   PRINT "Bienvenido a la calculadora básica"
  6.   PRINT "Operaciones válidas: suma, resta, multiplicacion, division"
  7.  
  8.   DO
  9.     Input = INPUT("Ingrese una operación (o 'salir' para finalizar): ")
  10.    
  11.     IF Input = "salir" THEN
  12.       EXIT
  13.     END IF
  14.    
  15.     Result = Calculator.Calculate(Input)
  16.    
  17.     PRINT "Resultado: " & Result
  18.    
  19.   LOOP
  20.  
  21.  
  22. ' Clase para realizar operaciones de cálculo
  23. CLASS CalculatorApp
  24.  
  25.   PUBLIC FUNCTION Calculate(Operation AS String) AS String
  26.     SELECT Operation
  27.       CASE "suma"
  28.         RETURN "Operación de suma seleccionada."
  29.       CASE "resta"
  30.         RETURN "Operación de resta seleccionada."
  31.       CASE "multiplicacion"
  32.         RETURN "Operación de multiplicación seleccionada."
  33.       CASE "division"
  34.         RETURN "Operación de división seleccionada."
  35.       OTHERWISE
  36.         RETURN "Operación no válida. Intente nuevamente."
  37.     END SELECT
  38.   END
  39.  
  40.   ' Método para proporcionar opciones de finalización
  41.   PUBLIC FUNCTION Complete(Opt AS String) AS String
  42.     SELECT Opt
  43.       CASE "s"
  44.         RETURN "suma"
  45.       CASE "r"
  46.         RETURN "resta"
  47.       CASE "m"
  48.         RETURN "multiplicacion"
  49.       CASE "d"
  50.         RETURN "division"
  51.       CASE "sal"
  52.         RETURN "salir"
  53.       OTHERWISE
  54.         RETURN ""
  55.     END SELECT
  56.   END
  57.  
  58.   ' Método para proporcionar finalización automática
  59.   PUBLIC SUB AutoComplete(Text AS String)
  60.     DIM LastSpace AS Integer
  61.     LastSpace = INSTR(Text, " ", -1)
  62.     IF LastSpace > 0 THEN
  63.       DIM Command AS String
  64.       Command = MID(Text, LastSpace + 1)
  65.       DIM Completion AS String
  66.       Completion = Complete(LEFT(Command, 1))
  67.       IF Completion <> "" THEN
  68.         PRINT Completion
  69.       END IF
  70.     END IF
  71.   END
  72.  
  73.  
  74.  

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
Other sample:

Code (gambas)

  1. ' Gambas Form file
  2. $Form1 = New Form
  3. $Form1.Text = "Autocompletion Example"
  4. $Form1.Resize(300, 100)
  5.  
  6. 'Text input field
  7. $TextBox1 = New TextBox
  8. $TextBox1.Text = ""
  9. $TextBox1.Move(10, 10, 280, 30)
  10. $TextBox1.Parent = $Form1
  11.  
  12. ' Create a Completion control
  13. $Completion1 = New Completion
  14. $Completion1.Popup = $TextBox1
  15.  
  16. ' Define autocompletion items
  17. $Completion1.AddItem("Apple")
  18. $Completion1.AddItem("Banana")
  19. $Completion1.AddItem("Cherry")
  20. $Completion1.AddItem("Grape")
  21. $Completion1.AddItem("Lemon")
  22. $Completion1.AddItem("Orange")
  23.  
  24. ' Handle the Selected event
  25. $Completion1.Selected = Sub()
  26.   ' Get the selected item from the autocompletion list
  27.   $TextBox1.Text = $Completion1.SelectedText
  28.  
  29. ' Show the form
  30. $Form1.Show
  31.  
  32.  
  33.  

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
Andreas_K is in the usergroup ‘Regular’
Thanks for the answer, but on my Gambas this is not working, o i make things wrong.

Image

(Click to enlarge)


I would fill a textbox (Datacontroltextbox) with a Database Field (Select Distinct ….), and the Datacontrolcombo is not working in this way….
I use Gambas 3.18.4 on Mint.
Online now: No Back to the top

Post

Posted
Rating:
#5
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
Me, say:
All the explanation and example code is developed by an AI for you, expressly. I think this is what you are looking for, if not, tell me.

AI, say:

Sure, here's the previous explanation and example in English:

In Gambas, the `Completion` property on a `Form` object is used to configure automatic completion of entries in a text field. This is useful when you want to provide suggestions or automatically complete user entries. Here's a simple example of how to use `Form1.Completion` to add suggestions to a text field on a form:

Code (gambas)

  1. ' This example assumes you have a form named "Form1" with a text field named "txtInput" in it.
  2.  
  3. PUBLIC SUB Form_Open()
  4.     ' Set the Completion property for the "txtInput" text field.
  5.     Form1.txtInput.Completion = NEW String[3]
  6.     Form1.txtInput.Completion[0] = "Apple"
  7.     Form1.txtInput.Completion[1] = "Banana"
  8.     Form1.txtInput.Completion[2] = "Cherry"
  9.  
  10. PUBLIC SUB txtInput_KeyUp()
  11.     ' Handle the KeyUp event of the text field to provide suggestions as the user types.
  12.     DIM Text AS String = Form1.txtInput.Text
  13.     DIM Suggestion AS String
  14.  
  15.     ' Find suggestions that start with the entered text.
  16.     FOR EACH Suggestion IN Form1.txtInput.Completion
  17.         IF Suggestion LIKE Text & "*" THEN
  18.             Form1.txtInput.Suggest = Suggestion
  19.             EXIT
  20.         END IF
  21.     NEXT
  22.  
  23.  

In this example, we first configure the `Completion` property of the text field `txtInput` on the `Form1` form. Three suggestions, "Apple," "Banana," and "Cherry," are added.

Then, we handle the `KeyUp` event of the `txtInput` text field. Each time the user presses a key, we search through the suggestions to see if any of them start with the text entered so far. If a match is found, that suggestion is set as the current suggestion in the text field. This provides auto-completion functionality for the user as they type.

Please note that this is a simple example. You can customize it to fit your needs and add more suggestions to the `Completion` property as required in your application.

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
 Does the AI produced code work?

Any other time I have seen ai used it produced a load of garbage code that looked a bit like gambas but didn't work because it was full of errors.
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
What are you trying to do?

Completion.class is kinda tricky but FileCompletion.class is easy

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Public hCompletion As FileCompletion
  4.  
  5. Public Sub Form_Open()
  6.  
  7.    hCompletion = New FileCompletion(TextArea1)
  8.  
  9.  

That's all you need to enable file completion for TextArea1


To use Completion.Class you have to make a class file and make it Inherit Completion, then give it a Fill() method.

I have attached a demo that uses Completion to make my own version of file completion.

In the main form i simply attach the MyCompletion.class to Textarea1 control.

Code (gambas)

  1. Public hCompletion As MyCompletion
  2.  
  3. Public Sub Form_Open()
  4.  
  5.    hCompletion = New MyCompletion(TextArea1) As "CMP"
  6.  
  7.  

Then I have created a MyCompletion.class that inherits Completion like this..

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Inherits Completion
  4.  
  5. Public Sub Fill(Text As String, Explicit As Boolean)
  6.  
  7.   If Not Trim(Text) Then Return
  8.   Dim aFiles As String[]
  9.   Dim sText, sDir, sStart As String
  10.   Dim iPos As Integer
  11.  
  12.   sText = Split(Text, "\n ", "\"", True).Last  ' get the last bit of the text not all of it
  13.  
  14.   ' Now find the last / to get the path part from the text.
  15.   iPos = RInStr(sText, "/")
  16.   sStart = Mid$(sText, iPos + 1)
  17.   sDir = Left(sText, iPos)
  18.  
  19.  ' list the dir using the file part of the text to pattern match then sort it. or return if no results
  20.    aFiles = Dir(sDir, File.Name(sText) & "*")
  21.   If aFiles.Count Then Me.List = aFiles.Sort(gb.Natural + gb.IgnoreCase) Else Return
  22.  
  23.   ' set start position as the last / char in the text
  24.   Me.Start = RInStr(Text, "/")
  25.  
  26.   ' load a set of icons (code copied from FileCompletion.class)
  27.   Me.Icons = New String[Me.List.Count]
  28.   For I As Integer = 0 To Me.List.Max
  29.     If IsDir(sDir &/ Me.List[I]) Then
  30.       Me.Icons[I] = "icon:/16/directory"
  31.     Else
  32.       Me.Icons[I] = "icon:/16/file"
  33.     Endif
  34.   Next
  35.  
  36.  
  37.  

I wrote comments in the source so you can see what is happening.

THE IMPORTANT BITS…
You MUST make a class that inherits Completion and use that.
You MUST add the Public Sub Fill() method to your class
You MUST add your items to Me.List in the Fill method
you MUST set the Me.Start property to let completion know where to insert the text

Note: completion only works on TextBox, TextArea and objects that have an "Editor" property
Like a TableView for example you should assign like this…

Code (gambas)

  1.    hCompletion = New MyCompletion(TableView1.Editor)
  2.  

Hope that helps.

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
And just a note to gambafeliz

As i suspected all that AI code was incorrect in a big way.

Please test anything you find via AI before posting or you are just wasting peoples time with incorrect code.
I know you meant well but none of that AI code made sense and had syntax errors all over the place.
Clearly your AI source does not really know gambas very well.
it starts off talking about Form.Completion and Completion is NOT even a property of Form.class (you should have spotted that)

I Say:
Andreas_K my advice would be to ignore anything the AI produced and read my post instead :)

AI Say:
blah blah guess guess garbage code

No offense intended gambafeliz but i hope you see all the problems with that AI code.
It is not only nonsense it is very wrong compared to the correct answer.

Respects
Online now: No Back to the top

Post

Posted
Rating:
#9
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
:lol:  :lol:  :lol:

With all due respect, you already know me a little. The AI said that it only works on versions higher than my version, which is 3.15.2

Therefore I couldn't prove it.

On the other hand, it seems to me that the problem was that the person who asked did not know how to ask. Before asking questions you must give information that others understand and at the beginning it was not clear at all.

I knew that the first two batches of AI code were for nothing because it was not what the person asking asked for, but since he didn't ask properly, I gave him something similar but with an AI.

And since he also responds to me as if to give me more information, well then, I gave him the latest AI code that, in particular, seemed to me to be worth it but that I in particular could not validate because my version does not support Completion of Form.class

In short, there was always a good intention to help but I wanted the applicant to explain the question request more clearly.

Greetings to all.

Note: I don't doubt that your explanation is perfect or almost perfect, you already know that I have you as a super ninja of the Gambas code.

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#10
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
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.

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’

gambafeliz said

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) @ /comp/gb.form/completion - Gambas Documentation

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?
Online now: No Back to the top

Post

Posted
Rating:
#12
Guru
BruceSteers is in the usergroup ‘Guru’
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…

Code (gambas)

  1.   If Left(sText) <> "/" Then  ' It's not a file path
  2.     Dim hCtrl As Object = Me.Editor
  3.     Dim aText As String[] = Split(hCtrl.Text, "\n\r\t:;\"'()\{}[] ", Null, True) ' get a word list
  4.    
  5.     Dim aList As New String[]
  6.     ' search document words for matching pattern
  7.     For Each sWord As String In aText
  8.       If sWord Like sText & "*" And If sWord Not Like sText Then
  9.         If Not aList.Exist(sWord) Then aList.Add(sWord)
  10.       Endif
  11.     Next
  12.     If Not aList.Count Then Return
  13.  
  14.     Me.List = aList.Sort(gb.Natural + gb.IgnoreCase)
  15.     Me.Start = Text.Len - sText.Len
  16.     ' normal completion done so exit function
  17.     Return
  18.  
  19.  

attached is the test project.
(plus i replaced the attachment in the original post to be this version)

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#13
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
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. :)
Online now: No Back to the top

Post

Posted
Rating:
#14
Guru
BruceSteers is in the usergroup ‘Guru’
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…

Code (gambas)

  1.   If Left(sText) <> "/" Then  ' It's not a file path
  2.   Me.Icons.Clear
  3.  
  4.  
Online now: No Back to the top

Post

Posted
Rating:
#15
Regular
Andreas_K is in the usergroup ‘Regular’
 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
Online now: No Back to the top

Post

Posted
Rating:
#16
Guru
BruceSteers is in the usergroup ‘Guru’

Andreas_K said

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…

Code (gambas)

  1.  
  2. If $hCompletion then
  3.   $hCompletion.Close
  4.   $hCompletion = Null
  5.  
  6.  
Online now: No Back to the top

Post

Posted
Rating:
#17
Guru
BruceSteers is in the usergroup ‘Guru’
There is also an _Activate() event that fires when the user selects a word from the list
you could possibly kill it there?

Code (gambas)

  1.  
  2. Private hCompletion as MyCompletion
  3.  
  4. Public Sub Form_Open()
  5.  
  6.   hCompletion = New MyCompletion(TextArea1) As "CMP"  ' give the object CMP as the handler name example
  7.  
  8.  
  9. ' This event fires when an item is selected from the list.
  10.  
  11. Public Sub CMP_Activate()
  12.  
  13.   Print "A Completion item has been activated"
  14.   hCompletion.Close
  15.   hCompletion = Null
  16.  
  17.  
  18.  
  19.  
  20.  
Online now: No Back to the top

Post

Posted
Rating:
#18
Regular
Andreas_K is in the usergroup ‘Regular’

Code (gambas)

  1. Inherits Completion
  2. Private sDBField As String
  3. Private sDBTable As String
  4.  
  5. Public Sub SqlQuery(sTAble As String, sField As String, Optional boLike As Boolean = True)
  6.  
  7.   If stable = 0 Or sfield = 0 Then Return
  8.  
  9.   ssql = "SELECT DISTINCT " & sfield & " FROM " & stable & ""
  10.  
  11.   sdbfield = sfield
  12.   sDBTable = sTAble
  13.   bLike = boLike
  14.  
  15.  
  16. Public Sub Fill(Text As String, Explicit As Boolean)
  17.  
  18.   If Not Trim(Text) Then Return
  19.   Dim sResults As New String[]
  20.  
  21.   If Not Trim(sSql) Then Return
  22.  
  23.   If bLike Then
  24.     res = db.Exec(ssql & " where " & sDBField & " like  '" & text & "%'")
  25.   Else
  26.     res = db.Exec(ssql)
  27.  
  28.   While res.Available
  29.     sResults.Add(res[sDBField])
  30.     res.MoveNext
  31.   Wend
  32.  
  33.   If res.Count = 0 Then
  34.     If res Then
  35.       res = Null
  36.     End If
  37.    
  38.     Me.Close
  39.     Return
  40.   End If
  41.  
  42.   If res Then
  43.     res = Null
  44.  
  45.   Me.List = sResults
  46.   ' Catch
  47.   '   Message.Error("Completion Fill Error")
  48.  
  49.  
  50.  

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?

Code (gambas)

  1. Public Sub DataControlCulture_GotFocus()
  2.  
  3.   hCompletion = New MyCompletion(DataControlCulture) As "CMP"
  4.   hCompletion.SqlQuery("PesticideCulture", "Culture")
  5.  
  6.  
  7.  
  8. Public Sub CMP_Activate()
  9.  
  10.   Debug "Completion activated message"
  11.   If hCompletion Then
  12.     hCompletion.Close
  13.     hCompletion = Null
  14.   End If
  15.  
Online now: No Back to the top

Post

Posted
Rating:
#19
Guru
BruceSteers is in the usergroup ‘Guru’
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…

Code (gambas)

  1. Public Sub _free()
  2.   Res = Null
  3.  

_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)

Code (gambas)

  1.  
  2. Public Sub Form_Close()
  3.  
  4.   try hCompletion.Close
  5.   hCompletion = Null
  6.  
  7.  
  8.  
  9.  
Online now: No Back to the top

Post

Posted
Rating:
#20
Regular
Andreas_K is in the usergroup ‘Regular’
 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
Online now: No Back to the top
1 guest and 0 members have just viewed this.