Getting Started with WinForms Inline AI Assistant
The Telerik UI for WinForms RadInlineAIAssistant component adds a prompt popup to a text-editing control. This article creates a working text-box example, adds a predefined command, and handles PromptRequest to display a response.
The component does not connect to an AI service automatically. The example uses a local uppercase conversion to demonstrate the request lifecycle without credentials or network access.
Adding Telerik Assemblies Using NuGet
Install the Telerik.UI.for.WinForms.AllControls package in your Windows Forms application. The package target framework version may vary. Use a Telerik UI for WinForms version that includes RadInlineAIAssistant.
For installation instructions, see Install Using NuGet Packages.
Configure your Telerik license key before building the application.
Adding Assembly References Manually
If you do not use NuGet, reference these assemblies:
Telerik.WinControlsTelerik.WinControls.UITelerikCommon
The assemblies are available through the supported installation approaches. Additional host controls, such as RadRichTextEditor, require their own assembly references.
Attaching the Assistant to a Text Box
- Create a text control and a
RadInlineAIAssistantinstance. Add the assistant to a component container so it is disposed with the form. Do not add it to the form'sControlscollection. - Call
AttachTowith aTextBox,RichTextBox,RadTextBox, orRadTextBoxControl. The assistant creates the corresponding interaction provider. - Optionally add predefined prompts to
Commands. - Handle
PromptRequestand supply a response with the event data'sSetResponsemethod. - Open the assistant by calling
Showwhile the host is visible. This example wiresCtrl+Spacein the host'sKeyDownevent; the shortcut is application code, not a component default.
The following standalone form can be opened from your application's startup code. It does not require designer-created controls.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.WinControls.UI;
public class InlineAssistantForm : Form
{
private readonly IContainer components = new Container();
private readonly TextBox editor;
private readonly RadInlineAIAssistant assistant;
public InlineAssistantForm()
{
this.Text = "Inline AI Assistant";
this.ClientSize = new Size(800, 500);
this.editor = new TextBox
{
Multiline = true,
Dock = DockStyle.Fill,
Text = "Select this text and press Ctrl+Space."
};
this.Controls.Add(this.editor);
this.assistant = new RadInlineAIAssistant(this.components);
this.assistant.NullText = "Enter a prompt...";
this.assistant.AttachTo(this.editor);
this.assistant.Commands.Add(new InlineAIAssistantCommand
{
Text = "Uppercase",
Prompt = "Convert the selected text to uppercase."
});
this.assistant.PromptRequest += this.OnPromptRequest;
this.editor.KeyDown += this.OnEditorKeyDown;
}
private void OnEditorKeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Space)
{
e.SuppressKeyPress = true;
this.assistant.Show();
}
}
private async void OnPromptRequest(
object sender, InlineAIAssistantPromptRequestEventArgs e)
{
string text = string.IsNullOrEmpty(e.SelectedText)
? e.FullText : e.SelectedText;
try
{
await Task.Delay(300, e.CancellationToken);
e.SetResponse(text.ToUpperInvariant());
}
catch (OperationCanceledException) when (e.CancellationToken.IsCancellationRequested)
{
return;
}
catch (Exception error)
{
e.FailResponse(error);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
this.components.Dispose();
}
base.Dispose(disposing);
}
}Run the form, select text, and press Ctrl+Space. Choose Uppercase from the commands dropdown, or enter a prompt and select Send. This sample always converts the selected text, or the full text when there is no selection, to uppercase. It does not interpret the prompt.
When the response appears, select Replace to replace the selection, Insert to insert the response, or Discard to clear it and continue prompting. The built-in text-box provider inserts after the selection without deleting the selected text. Replace requires a selection, and both editing actions require an editable host.
Connecting an AI Service
Replace the delay and uppercase conversion in OnPromptRequest with an asynchronous call to your service. Use these members of InlineAIAssistantPromptRequestEventArgs:
| Member | Usage |
|---|---|
Prompt | The submitted prompt, including prompts supplied by commands. |
SelectedText | The selected text captured when the request starts. |
FullText | The full host text, retrieved on first access. Read it on the UI thread before passing it to background work. |
Command | The selected command, or null for a typed prompt. |
CancellationToken | Pass this token to your service call so it can stop when the user cancels or closes the assistant. |
SetResponse(response) | Displays the complete response and ends processing. |
FailResponse(error) | Ends processing and displays the localized generic error message. |
Send only the context required for the operation, and keep service credentials out of client-side sample code. Catch exceptions inside an asynchronous event handler, as in the example.
For streaming, call e.AppendResponse(chunk) for each chunk and e.CompleteResponse() after the last chunk. These calls belong inside the request handler or a callback that retains that request's event data. For example, the following replaces the SetResponse call with two sample chunks:
e.AppendResponse("First part of the response. ");
e.AppendResponse("Remaining response text.");
e.CompleteResponse();Prefer the event data's response methods for asynchronous work. They retain the request identity and ignore late updates after that request is completed, failed, canceled, or replaced by another request. The First Look demo follows this pattern when appending response tokens.
Using the Rich Text Editor
For an existing RadRichTextEditor named radRichTextEditor1, assign the assistant to the editor's InlineAIAssistant property instead of calling AttachTo directly. This attaches the component and creates the editor's provider if no custom provider is assigned.
this.radRichTextEditor1.InlineAIAssistant = this.assistant;The editor adds an AI button to its compact selection mini toolbar. Its IsInlineAIAssistantEnabled property controls these entry points and defaults to true. You can also call Show from an application button, as the First Look demo does. The editor does not dispose the assistant; retain the container-based lifetime management shown above.