How to Get the value of selected Item in listbox(items loaded from DB using WCF service) to a Text box ?

Pertanyaan How to Get the value of selected Item in listbox(items loaded from DB using WCF service) to a Text box ?

  • Thursday, July 26, 2012 5:14 AM
     
      Has Code
    First, I consumed data from a database to listbox1. However, if I select the item, it does not go into textbox1! Instead, it shows a null reference error.
    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (null != listBox1.SelectedItem)
        {        
            string text = (listBox1.SelectedItem as ListBoxItem).Content.ToString();
            textBox1.Text = text.ToString();
        } 
    }
    

All Replies

  • Friday, July 27, 2012 3:43 AM
     
      Has Code

    Hi  shanmu995 ,

    You want to show the selected Item in  textbox , right ?

    Please try this , hope it will be helpful :

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (null != listBox1.SelectedItem)
        {
           //  string text = (listBox1.SelectedItem as ListBoxItem).Content.ToString();
             string s = listBox1.SelectedItem.ToString();
             textBox1.Text = s;   
         } 
    }
    Regards ,

    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

  • Friday, July 27, 2012 12:35 PM
     
     

    Hi Lisa Zhu ,

           thank you for your reply

          your code is not working because listbox1 values are consumed from database using WCF service in silverlight window phone. It show only name of the service

  • Thursday, August 02, 2012 7:56 AM
     
     

    Hi shanmu995,
    I am sorry for not  reading your post title carefully enought  , I thought you did a winform application ,so the code I gave is based on WinForm  application .Sorry.
    And this forum does not support WCF related question /discussiion .
    So , if you need help with the WCF ,please refer to : http://social.msdn.microsoft.com/Forums/en-US/wcf/ 

    Thanks for your support  understanding .

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us