Advanced Topics

Building Live AI Forms with WithLiveInputFrom

WithLiveInputFrom wires a GPALInput or GPALTextArea's TextChanged event (debounced) to automatically re-run the configured AI task and write the result to WithOutputTo, turning the button-driven pattern from Summarizing and Generating Text with GPAL.AI into a live, type-and-see tool with one extra call.

From a Button Click to a Live Update

The pattern in Summarizing and Generating Text with GPAL.AI wires a button's click handler to call WithInputFrom/WithOutputTo/Execute again each time the user wants a new result. WithLiveInputFrom replaces both WithInputFrom and that manual Execute call for one input control: it records the control as the input source, then registers a TextChanged callback that calls Execute() automatically. The button and click handler are no longer needed. Typing in the input control is what drives the AI task now.

// Before: WithInputFrom plus a button click handler

GPALTextArea inputArea = (GPALTextArea)GPAL.TextArea.WithName("input").WithText("Paste text here");

GPALTextArea outputArea = (GPALTextArea)GPAL.TextArea.WithName("output");

void SummarizeClickHandler(object sender, EventArgs e)

{

GPAL.AI

.WithProvider(AIProviderType.XAI)

.WithTask(AITask.Summarization)

.WithInputFrom(inputArea)

.WithOutputTo(outputArea)

.Execute();

}

GPALButton summarizeButton = (GPALButton)GPAL.Button

.WithName("summarize")

.WithText("Summarize")

.WithCallback(SummarizeClickHandler);

GPAL.Form

.WithTitle("Summarizer")

.WithFormControl(inputArea)

.WithFormControl(outputArea)

.WithFormControl(summarizeButton)

.Show();

WithLiveInputFrom

WithLiveInputFrom(input, debounceMilliseconds) takes the place of WithInputFrom in the chain. It records the control as the input source exactly like WithInputFrom, then attaches a TextChanged callback to that control. Each keystroke restarts a timer; once the user stops typing for debounceMilliseconds (500 by default), the timer fires and calls Execute() again, which reads the control's current text, runs the configured AI task, and writes the result to whatever WithOutputTo targets. WithStatusTo(statusBar) can be chained alongside WithOutputTo to update a GPALStatusStrip with a processing message while the AI call is in flight and reset it to Ready when done. There is no button and no click handler. The form becomes a live tool the moment it is shown.

GPALTextArea inputArea = GPAL.TextArea.WithName("input").WithText("").ToGPALObject();

GPALRichTextBox outputArea = (GPALRichTextBox)GPAL.RichTextBox.WithName("output").WithText("").ToGPALObject();

GPALStatusStrip statusBar = (GPALStatusStrip)GPAL.StatusStrip.WithName("status").WithText("Ready").ToGPALObject();

IGPALAI aiTask = GPAL.AI

.WithProvider(AIProviderType.Anthropic)

.WithCredentials(credentials)

.WithTask(AITask.Summarization)

.WithLiveInputFrom(inputArea, debounceMilliseconds: 800)

.WithOutputTo(outputArea)

.WithStatusTo(statusBar)

.ToGPALObject();

GPAL.Form

.WithTitle("Live Summarizer")

.WithFormControl(inputArea)

.WithFormControl(outputArea)

.WithFormControl(statusBar)

.ShowDialog();

TIP

A second overload, WithLiveInputFrom(GPALInput input, int debounceMilliseconds = 500), works the same way for single-line input controls.

Wiring Order: Before the Form is Shown

GPALInput and GPALTextArea controls wire their TextChanged event during form realization, the same step that happens when WithFormControl adds the control to the form and the form is shown. WithLiveInputFrom must be called before that happens, so it should be set up while building the AI configuration and form controls, not after .Show()/.ShowDialog() has already run. Calling it afterward leaves the AI task configured but the live callback never gets attached to the control.

WARNING

If WithLiveInputFrom is called after the form has already been shown, the debounced TextChanged callback is never wired up and the input will not trigger the AI task. Configure GPAL.AI, including WithLiveInputFrom, before calling WithFormControl, Show, or ShowDialog.