AIChatControl.MessageSending Event
Fires when a user submits a message to the chat.
Namespace: DevExpress.AIIntegration.WinForms.Chat
Assembly: DevExpress.AIIntegration.WinForms.Chat.v26.1.dll
NuGet Package: DevExpress.AIIntegration.WinForms.Chat
Declaration
[DXCategory("Events")]
public event EventHandler<AIChatControlMessageSendingEventArgs> MessageSending
Event Data
The MessageSending event's data class is DevExpress.AIIntegration.Blazor.Chat.WebView.AIChatControlMessageSendingEventArgs.
Remarks
Handle the MessageSending event to process a user message before the control adds it to chat history and sends it to the AI service. For example, you can log a message to a database, remove personally identifiable information (PII) from the message text, add system instructions, or apply custom logic.
Set the e.Cancel event parameter to true to block automatic message delivery. After cancellation, call the SendMessageAsync method to submit a message manually.
Example
The following example blocks the default delivery flow and sends a modified message to the AI service:
using DevExpress.AIIntegration.Blazor.Chat.WebView;
using Microsoft.Extensions.AI;
public Chat() {
InitializeComponent();
aiChatControl1.MessageSending += AiChatControl1_MessageSending;
}
async void AiChatControl1_MessageSending(object sender, AIChatControlMessageSendingEventArgs e) {
e.Cancel = true;
StringBuilder sb = new StringBuilder();
sb.AppendLine("Your message:");
sb.AppendLine(e.Text);
e.Text = sb.ToString();
await e.Chat.SendMessageAsync(sb.ToString());
}
Use the e.Files and e.Contexts event parameters to access files and AI resources attached to the message.
Refer to the following help topic for additional information: AI Chat Control