Events in Windows Forms AI AssistView (SfAIAssistView)

15 Jul 20263 minutes to read

The following events are supported by the SfAIAssistView control:

Event Description
PromptRequest Notifies when a prompt is submitted in the control.

The following using directives are included in your file:

using System;
using System.Windows.Forms;
using Syncfusion.WinForms.AIAssistView;

NOTE

A ViewModel class with a Chats collection and CurrentUser properties exists. Refer to the Getting Started page for setup details.

PromptRequest event

PromptRequest event notifies users when a prompt is submitted in the control. It can be used to validate user input before processing or trigger custom actions based on the prompt content. The input message and its details are passed through the PromptRequestEventArgs. This argument provides the following details:

  • Message: Represents the input message value of the AIAssistView (typically a TextMessage).
  • Handled: Boolean value indicating whether the input message in the Messages collection has been handled by the event. Set this to true to prevent the default handling of the message.
public partial class Form1 : Form
{
    ViewModel viewModel;

    public Form1()
    {
        InitializeComponent();
        viewModel = new ViewModel();

        SfAIAssistView sfAIAssistView1 = new SfAIAssistView();
        sfAIAssistView1.Location = new System.Drawing.Point(41, 40);
        sfAIAssistView1.Size = new System.Drawing.Size(818, 457);  
        sfAIAssistView1.Dock = DockStyle.Fill;
        this.Controls.Add(sfAIAssistView1);

        sfAIAssistView1.PromptRequest += AIAssistView_PromptRequest;
        sfAIAssistView1.DataBindings.Add("Messages", viewModel, "Chats", true, DataSourceUpdateMode.OnPropertyChanged);
        sfAIAssistView1.DataBindings.Add("ShowTypingIndicator", viewModel, "ShowTypingIndicator", true, DataSourceUpdateMode.OnPropertyChanged);
        sfAIAssistView1.DataBindings.Add("Suggestions", viewModel, "Suggestion", true, DataSourceUpdateMode.OnPropertyChanged);
        viewModel.CurrentUser = sfAIAssistView1.User;

        sfAIAssistView1.TypingIndicator.Author = new Author() { Name = "Bot", AvatarImage = Image.FromFile(@"Asset\AI_Assist.png") };
        sfAIAssistView1.TypingIndicator.DisplayText = "Typing";
    }

    private void AIAssistView_PromptRequest(object sender, PromptRequestEventArgs e)
    {
        var message = e.Message as TextMessage;
        if (message == null) return;

        // Example: Mark the prompt as handled so the default processing is skipped
        e.Handled = true;
    }
}