Inserting Text in TextBox using Silverlight 3



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.

1.gif 

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.

2.gif

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.

3.gif

Hope this article is helpful to all.

Up Next
    Ebook Download
    View all
    Learn
    View all