|
| 1 | +--- |
| 2 | +title: Inserting a Message at a Specific Index in RadChat |
| 3 | +description: Learn how to insert a message at a specific index in the RadChat control in UI for WinForms. This guide explains how to utilize the `Insert` method with a `BaseChatDataItem`. |
| 4 | +type: how-to |
| 5 | +page_title: How to Insert a Message at a Specific Index in RadChat Control |
| 6 | +meta_title: How to Insert a Message at a Specific Index in RadChat Control |
| 7 | +slug: chat-insert-message-specific-index |
| 8 | +tags: chat, ui-for-winforms, insert-message, basechatdataitem, messagesviewelement |
| 9 | +res_type: kb |
| 10 | +ticketid: 1694976 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +|Product Version|Product|Author| |
| 16 | +|----|----|----| |
| 17 | +|2025.3.812|RadChat for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +In this tutorial, we will demonstrate how to insert a new message at a specific index in the RadChat control. |
| 22 | + |
| 23 | +## Solution |
| 24 | + |
| 25 | +To insert a message at a specific index in RadChat, use the `Insert()` method with a `BaseChatDataItem`. Follow these steps: |
| 26 | + |
| 27 | +1. Access the existing messages in RadChat using the `MessagesViewElement.Items` collection. |
| 28 | +2. Create a `ChatMediaMessage` or another appropriate message type. |
| 29 | +3. Convert the message into a `BaseChatDataItem` using the `ChatFactory.CreateDataItem()` method. |
| 30 | +4. Use the `Insert()` method to place the new message at a specific index. |
| 31 | + |
| 32 | +Here is an example: |
| 33 | + |
| 34 | +````C# |
| 35 | + |
| 36 | +private void InsertMediaMessageAtBeginning() |
| 37 | +{ |
| 38 | + ChatMediaMessage mediaMessage = new ChatMediaMessage(Properties.Resources.AndrewFuller, new Size(300, 200), null, this.radChat1.Author, DateTime.Now); |
| 39 | + BaseChatDataItem baseChatDataItem = this.radChat1.ChatElement.ChatFactory.CreateDataItem(mediaMessage); |
| 40 | + this.radChat1.ChatElement.MessagesViewElement.Items.Insert(0, baseChatDataItem); |
| 41 | +} |
| 42 | + |
| 43 | +```` |
| 44 | + |
| 45 | +## See Also |
| 46 | + |
| 47 | +* [RadChat Documentation](https://docs.telerik.com/devtools/winforms/controls/chat/overview) |
| 48 | + |
| 49 | +* [RadChat Messages](https://docs.telerik.com/devtools/winforms/controls/chat/chat-items/messages) |
0 commit comments