Click here to Skip to main content
15,891,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two fields, text area and Listbox. When i select each item in listbox it has to display in text area with appending on the text in it. and if i move the cursor before some text and select another item from listbox it must append the item where the cursor is placed.
i am fresher to this field so please help how do i do it??

i tried with this txtTemplatebody.Text = txtTemplatebody.Text + lbTokenList.SelectedItem.ToString(); but when i tried to append on front it's not working.

Thanks in advance
Posted
Comments
gggustafson 12-Dec-14 16:42pm    
Is the problem in C# or JavaScript?

Here is your complete solution. just check it!If you have any more question feel free to ask.


C#
  private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  {  //code of listbox selectedIndexChanged event
      try
      {
int si = textBox1.SelectionStart;
textBox1.Text=textBox1.Text.Insert(si,listBox1.SelectedItem.ToString());
textBox1.SelectionStart = si + listBox1.SelectedItem.ToString().Length;
      }
      catch(Exception ex)
      {
          throw ex;
      }

  }
 
Share this answer
 
Comments
Member 11261355 15-Dec-14 0:05am    
Thanks for the help. i have did it using javascript. any freshers struggling like me. here is the solution
Anisuzzaman Sumon 15-Dec-14 0:47am    
You are most welcome!
C#
function insert(el, ins) {
    if (el.setSelectionRange) {
        el.value = el.value.substring(0, el.selectionStart) + ins + el.value.substring(el.selectionStart, el.selectionEnd) + el.value.substring(el.selectionEnd, el.value.length);
    }
    else if (document.selection && document.selection.createRange) {
        el.focus();
        var range = document.selection.createRange();
        range.text = ins + range.text;
    }
}

function Updatelist() {

    var sel = document.getElementById(listboxID);
    var listLength = sel.options.length;
    for (var i = 0; i < listLength; i++) {
        if (sel.options[i].selected) {
            return sel.options[i].value;
        }
    }
}


Finally add
listboxid.Attributes.Add("Onclick", "insert(" + txtid.ClientID + ",Updatelist())"); in .cs page
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900