Introduction
There are various requirements for the TextBox Text input. In this article we will see how can we insert text in any TextBox Caret position.
Creating Silverlight Project
Fire up Visual Studio 2008 and create a new Silverlight 3 Project. Name it as TextInsert.
Here is the idea, there will be a list of Text in a combo box and we need to add the selected Text from the ComboBox to the caret position of TextBox.
Open the solution and design the page as follows.
Now insert some text to the ComboBox in the application load.
public MainPage()
{
InitializeComponent();
List<string> myList = new List<string>()
{
"[UserName]","[Sender]", "[UserContact]", "[UserAddress]"
};
cmbTexts.ItemsSource = myList;
}
Now create an event handle of ComboBox SelectionChanged.
SelectionChanged="cmbTexts_SelectionChanged"
Add the following code into the above handler.
private void cmbTexts_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selected = txtMyTextBox.Text;
int positionNotificationTemplate = txtMyTextBox.SelectionStart;
string strTemp1 = cmbTexts.SelectedItem.ToString();
selected = selected.Insert(positionNotificationTemplate, strTemp1);
txtMyTextBox.Text = selected;
}
Now run the application and write your message in TextBox.
Hope this article is helpful to all.