Page 1 of 2

How to use the completion Class

Posted: Tuesday 7th November 2023 7:52pm
by Andreas_K
I search a example how to use the completion class, can't find until now, thanks.

Re: How to use the completion Class

Posted: Tuesday 7th November 2023 8:05pm
by gambafeliz
This example is developed by an AI in response to your question.

PUBLIC SUB Main()

  DIM Calculator AS NEW CalculatorApp
  
  PRINT "Bienvenido a la calculadora básica"
  PRINT "Operaciones válidas: suma, resta, multiplicacion, division"
  
  DO
    DIM Input AS String
    Input = INPUT("Ingrese una operación (o 'salir' para finalizar): ")
    
    IF Input = "salir" THEN
      EXIT
    END IF
    
    DIM Result AS String
    Result = Calculator.Calculate(Input)
    
    PRINT "Resultado: " & Result
    
  LOOP

END

' Clase para realizar operaciones de cálculo
CLASS CalculatorApp

  PUBLIC FUNCTION Calculate(Operation AS String) AS String
    SELECT Operation
      CASE "suma"
        RETURN "Operación de suma seleccionada."
      CASE "resta"
        RETURN "Operación de resta seleccionada."
      CASE "multiplicacion"
        RETURN "Operación de multiplicación seleccionada."
      CASE "division"
        RETURN "Operación de división seleccionada."
      OTHERWISE
        RETURN "Operación no válida. Intente nuevamente."
    END SELECT
  END

  ' Método para proporcionar opciones de finalización
  PUBLIC FUNCTION Complete(Opt AS String) AS String
    SELECT Opt
      CASE "s"
        RETURN "suma"
      CASE "r"
        RETURN "resta"
      CASE "m"
        RETURN "multiplicacion"
      CASE "d"
        RETURN "division"
      CASE "sal"
        RETURN "salir"
      OTHERWISE
        RETURN ""
    END SELECT
  END

  ' Método para proporcionar finalización automática
  PUBLIC SUB AutoComplete(Text AS String)
    DIM LastSpace AS Integer
    LastSpace = INSTR(Text, " ", -1)
    IF LastSpace > 0 THEN
      DIM Command AS String
      Command = MID(Text, LastSpace + 1)
      DIM Completion AS String
      Completion = Complete(LEFT(Command, 1))
      IF Completion <> "" THEN
        PRINT Completion
      END IF
    END IF
  END

END


Re: How to use the completion Class

Posted: Tuesday 7th November 2023 8:07pm
by gambafeliz
Other sample:

' Gambas Form file
$Form1 = New Form
$Form1.Text = "Autocompletion Example"
$Form1.Resize(300, 100)

'Text input field
$TextBox1 = New TextBox
$TextBox1.Text = ""
$TextBox1.Move(10, 10, 280, 30)
$TextBox1.Parent = $Form1

' Create a Completion control
$Completion1 = New Completion
$Completion1.Popup = $TextBox1

' Define autocompletion items
$Completion1.AddItem("Apple")
$Completion1.AddItem("Banana")
$Completion1.AddItem("Cherry")
$Completion1.AddItem("Grape")
$Completion1.AddItem("Lemon")
$Completion1.AddItem("Orange")

' Handle the Selected event
$Completion1.Selected = Sub()
  ' Get the selected item from the autocompletion list
  $TextBox1.Text = $Completion1.SelectedText
End

' Show the form
$Form1.Show

RETURN


Re: How to use the completion Class

Posted: Wednesday 8th November 2023 7:44pm
by Andreas_K
Thanks for the answer, but on my Gambas this is not working, o i make things wrong.
screen.png
screen.png (39.54 KiB) Viewed 5528 times
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.

Re: How to use the completion Class

Posted: Thursday 9th November 2023 7:47am
by gambafeliz
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:

' This example assumes you have a form named "Form1" with a text field named "txtInput" in it.

PUBLIC SUB Form_Open()
    ' Set the Completion property for the "txtInput" text field.
    Form1.txtInput.Completion = NEW String[3]
    Form1.txtInput.Completion[0] = "Apple"
    Form1.txtInput.Completion[1] = "Banana"
    Form1.txtInput.Completion[2] = "Cherry"
End

PUBLIC SUB txtInput_KeyUp()
    ' Handle the KeyUp event of the text field to provide suggestions as the user types.
    DIM Text AS String = Form1.txtInput.Text
    DIM Suggestion AS String

    ' Find suggestions that start with the entered text.
    FOR EACH Suggestion IN Form1.txtInput.Completion
        IF Suggestion LIKE Text & "*" THEN
            Form1.txtInput.Suggest = Suggestion
            EXIT
        END IF
    NEXT
END



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.

Re: How to use the completion Class

Posted: Thursday 9th November 2023 11:17am
by BruceSteers
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.

Re: How to use the completion Class

Posted: Thursday 9th November 2023 1:10pm
by BruceSteers
What are you trying to do?

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

' Gambas class file

Public hCompletion As FileCompletion

Public Sub Form_Open()

   hCompletion = New FileCompletion(TextArea1)

End


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.
Public hCompletion As MyCompletion

Public Sub Form_Open()

   hCompletion = New MyCompletion(TextArea1) As "CMP"

End


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

' Gambas class file

Export
Inherits Completion

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

  If Not Trim(Text) Then Return
  Dim aFiles As String[]
  Dim sText, sDir, sStart As String
  Dim iPos As Integer

  sText = Split(Text, "\n ", "\"", True).Last  ' get the last bit of the text not all of it

  ' Now find the last / to get the path part from the text.
  iPos = RInStr(sText, "/")
  sStart = Mid$(sText, iPos + 1)
  sDir = Left(sText, iPos)
  If Not IsDir(sDir) Then Return

 ' list the dir using the file part of the text to pattern match then sort it. or return if no results
   aFiles = Dir(sDir, File.Name(sText) & "*")
  If aFiles.Count Then Me.List = aFiles.Sort(gb.Natural + gb.IgnoreCase) Else Return

  ' set start position as the last / char in the text
  Me.Start = RInStr(Text, "/")

  ' load a set of icons (code copied from FileCompletion.class)
  Me.Icons = New String[Me.List.Count]
  For I As Integer = 0 To Me.List.Max
    If IsDir(sDir &/ Me.List[I]) Then
      Me.Icons[I] = "icon:/16/directory"
    Else
      Me.Icons[I] = "icon:/16/file"
    Endif
  Next

End



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...
   hCompletion = New MyCompletion(TableView1.Editor)


Hope that helps.

Re: How to use the completion Class

Posted: Thursday 9th November 2023 2:07pm
by BruceSteers
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

Re: How to use the completion Class

Posted: Thursday 9th November 2023 2:48pm
by gambafeliz
: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.

Re: How to use the completion Class

Posted: Thursday 9th November 2023 3:00pm
by gambafeliz
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.