How to use the completion Class
Posted: Tuesday 7th November 2023 7:52pm
I search a example how to use the completion class, can't find until now, thanks.
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
' 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
' 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
' Gambas class file
Public hCompletion As FileCompletion
Public Sub Form_Open()
hCompletion = New FileCompletion(TextArea1)
End
Public hCompletion As MyCompletion
Public Sub Form_Open()
hCompletion = New MyCompletion(TextArea1) As "CMP"
End
' 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
hCompletion = New MyCompletion(TableView1.Editor)