|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Response Toolbar in Windows Forms AI AssistView control | Syncfusion |
| 4 | +description: Learn about the Response Toolbar in AI AssistView, letting users interact with bot replies using copy, regenerate, like, and custom action buttons. |
| 5 | +platform: windowsforms |
| 6 | +control: SfAIAssistView |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Response ToolBar in WinForms AI AssistView |
| 11 | + |
| 12 | +The **SfAIAssistView** control includes a **Response Toolbar** feature that allows users to perform actions on bot responses by clicking action buttons. This feature provides an interactive way for users to engage with AI responses through copy, regenerate, like, and other custom actions. |
| 13 | + |
| 14 | +By default, the **Response Toolbar** is not displayed. To enable it, set the **IsResponseToolBarVisible** property to `true`. |
| 15 | + |
| 16 | +{% tabs %} |
| 17 | + |
| 18 | +{% highlight c# %} |
| 19 | + |
| 20 | + SfAIAssistView sfAIAssistView1 = new SfAIAssistView(); |
| 21 | + sfAIAssistView1.IsResponseToolBarVisible = true; |
| 22 | + |
| 23 | +{% endhighlight %} |
| 24 | + |
| 25 | +{% endtabs %} |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +## Response Toolbar Items |
| 30 | + |
| 31 | +The **Response Toolbar** supports the following action buttons: |
| 32 | + |
| 33 | +- **Copy** - Copies the bot response text to clipboard |
| 34 | +- **Regenerate** - Regenerates the response for the same prompt |
| 35 | +- **Like** - Marks the response as helpful/liked |
| 36 | +- **Dislike** - Marks the response as not helpful |
| 37 | +- **Custom** - User-defined custom actions |
| 38 | + |
| 39 | +## Response Toolbar Item Click Event |
| 40 | + |
| 41 | +The **SfAIAssistView** control provides the **ResponseToolBarItemClicked** event. This is triggered when a user clicks any toolbar action button. You can handle these actions to perform specific operations based on the toolbar item clicked. |
| 42 | + |
| 43 | +### Event Handler Code Example |
| 44 | + |
| 45 | +{% tabs %} |
| 46 | + |
| 47 | +{% highlight c# %} |
| 48 | + |
| 49 | + sfAIAssistView1.ResponseToolBarItemClicked += sfAIAssistView1_ResponseToolBarItemClicked; |
| 50 | + |
| 51 | + private void sfAIAssistView1_ResponseToolBarItemClicked( |
| 52 | + object sender, |
| 53 | + ResponseToolBarItemClickedEventArgs e) |
| 54 | + { |
| 55 | + // Handle the toolbar item click |
| 56 | + if (e.ToolBarItem.ItemType == ResponseToolBarItemType.Copy) |
| 57 | + { |
| 58 | + Clipboard.SetText(e.ChatItem.Text); |
| 59 | + MessageBox.Show("Message copied to clipboard!"); |
| 60 | + } |
| 61 | + else if (e.ToolBarItem.ItemType == ResponseToolBarItemType.Regenerate) |
| 62 | + { |
| 63 | + MessageBox.Show("Regenerating response..."); |
| 64 | + // Handle regeneration logic |
| 65 | + } |
| 66 | + else if (e.ToolBarItem.ItemType == ResponseToolBarItemType.Like) |
| 67 | + { |
| 68 | + MessageBox.Show("Response marked as helpful!"); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | +{% endhighlight %} |
| 73 | + |
| 74 | +{% endtabs %} |
| 75 | + |
| 76 | +The **ResponseToolBarItemClickedEventArgs** provides access to the **ChatItem** (the message being acted upon) and the **ToolBarItem** (the action button clicked). |
| 77 | + |
| 78 | +## Customization |
| 79 | + |
| 80 | +### Controlling Toolbar Visibility |
| 81 | + |
| 82 | +You can control the visibility of the entire toolbar or individual toolbar items: |
| 83 | + |
| 84 | +{% tabs %} |
| 85 | + |
| 86 | +{% highlight c# %} |
| 87 | + |
| 88 | + var messagesList = sfAIAssistView1.Messages as IList; |
| 89 | + if(messagesList != null && messagesList.Count>0 ) |
| 90 | + { |
| 91 | + var message = messagesList[1] as TextMessage; |
| 92 | + // Hide the entire toolbar for a specific message |
| 93 | + sfAIAssistView1.SetToolBarVisibility(message, false); |
| 94 | + |
| 95 | + // Show the toolbar for a specific message |
| 96 | + sfAIAssistView1.SetToolBarVisibility(message, true); |
| 97 | + |
| 98 | + // Hide a specific toolbar item for a message |
| 99 | + sfAIAssistView1.SetToolBarItemVisibility( |
| 100 | + message, |
| 101 | + ResponseToolBarItemType.Regenerate.ToString(), |
| 102 | + false); |
| 103 | + |
| 104 | + // Hide toolbar item by name |
| 105 | + sfAIAssistView1.SetToolBarItemVisibility(message, "Copy", false); |
| 106 | + } |
| 107 | + |
| 108 | +{% endhighlight %} |
| 109 | + |
| 110 | +{% endtabs %} |
| 111 | + |
| 112 | +### Getting Toolbar Items |
| 113 | + |
| 114 | +Retrieve toolbar items from a message: |
| 115 | + |
| 116 | +{% tabs %} |
| 117 | + |
| 118 | +{% highlight c# %} |
| 119 | + |
| 120 | + // Get a specific toolbar item |
| 121 | + ResponseToolBarItem copyButton = sfAIAssistView1.GetToolBarItem( |
| 122 | + message, |
| 123 | + ResponseToolBarItemType.Copy.ToString()); |
| 124 | + |
| 125 | + if (copyButton != null) |
| 126 | + { |
| 127 | + // Use toolbar item properties |
| 128 | + string itemName = copyButton.Name; |
| 129 | + } |
| 130 | + |
| 131 | +{% endhighlight %} |
| 132 | + |
| 133 | +{% endtabs %} |
| 134 | + |
| 135 | +### Configuring Toolbar Items |
| 136 | + |
| 137 | +Set custom toolbar items in the control: |
| 138 | + |
| 139 | +{% tabs %} |
| 140 | + |
| 141 | +{% highlight c# %} |
| 142 | + |
| 143 | + // Configure toolbar items |
| 144 | + sfAIAssistView1.ResponseToolBarItems = new ObservableCollection<ResponseToolBarItem> |
| 145 | + { |
| 146 | + new ResponseToolBarItem |
| 147 | + { |
| 148 | + ItemType = ResponseToolBarItemType.Copy, |
| 149 | + Name = "Copy" |
| 150 | + }, |
| 151 | + new ResponseToolBarItem |
| 152 | + { |
| 153 | + ItemType = ResponseToolBarItemType.Regenerate, |
| 154 | + Name = "Regenerate" |
| 155 | + }, |
| 156 | + new ResponseToolBarItem |
| 157 | + { |
| 158 | + ItemType = ResponseToolBarItemType.Like, |
| 159 | + Name = "Like" |
| 160 | + } |
| 161 | + }; |
| 162 | + |
| 163 | +{% endhighlight %} |
| 164 | + |
| 165 | +{% endtabs %} |
| 166 | + |
| 167 | + |
| 168 | +### How to hide Regenerate Button for Old Messages. |
| 169 | + |
| 170 | +{% tabs %} |
| 171 | + |
| 172 | +{% highlight c# %} |
| 173 | + |
| 174 | + private void UpdateToolbarForLatestMessage() |
| 175 | + { |
| 176 | + var messages = (sfAIAssistView1.Messages as IList) |
| 177 | + ?.Cast<TextMessage>().ToList(); |
| 178 | + |
| 179 | + if (messages == null) return; |
| 180 | + |
| 181 | + var botMessages = messages |
| 182 | + .Where(m => m.Author.Name == "Bot") |
| 183 | + .ToList(); |
| 184 | + |
| 185 | + var latestMessage = botMessages.LastOrDefault(); |
| 186 | + |
| 187 | + // Hide Regenerate button on all old bot messages |
| 188 | + foreach (var oldMessage in botMessages |
| 189 | + .Where(m => m != latestMessage)) |
| 190 | + { |
| 191 | + sfAIAssistView1.SetToolBarItemVisibility( |
| 192 | + oldMessage, |
| 193 | + ResponseToolBarItemType.Regenerate.ToString(), |
| 194 | + false); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | +{% endhighlight %} |
| 199 | + |
| 200 | +{% endtabs %} |
0 commit comments