+ Reply to Thread
Results 1 to 2 of 2

Thread: Error when parsing the Listbox item to textbox

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    7

    Error when parsing the Listbox item to textbox

    I have a listbox and a Textbox and what i am trying to do is setting the textbox text equal to the selected item of listbox
    So i wrote that code :
    Code:
    textBox1.Text = listBox1.SelectedItem.ToString();
    
    AND ALSO
    
    textBox31.Text = listBox4.SelectedItem;
    Fiest one crash the application and gives me an error [NullReference Exception]
    Code:
    Object reference not set to an instance of an object.
    And can`t solve it yet.

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    34
    Hi , i have tested your code and yes it gives an error but i analyzed the error and you could be getting that because
    the listbox didn`t take it`s time to highlight it`s selected item , so i have came up with a piece of code that worked for me
    without any issue.

    Here is the code :
    Code:
            private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                string slected_item = listBox1.SelectedItem.ToString();
    
                while (true)
                {
                    try
                    {
                        textBox1.Text = slected_item;
                        break;
                    }
                    catch{}
                }
            }
    That should give a good time for the listbox to highlight it`s selected item.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts