Results 1 to 6 of 6

Thread: Problem in getting an object from a listbox.

  1. #1
    Join Date
    Nov 2009
    Posts
    37

    Problem in getting an object from a listbox.

    Hello friends,
    I have an problem in list box. I have write following code for populating listbox.
    Code:
    listboxs1.datasources = myCollectionOfCarss
    listboxs1.databinds()
    When I tried to get particular item from the listbox using following code I get an error.
    Code:
    Car myCars = (Cars)listboxs1.selecteditems;
    I don't know what is the problem in getting an object from a listbox. Please help me to fix this problem.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Problem in getting an object from a listbox.

    Hey you have written wrong code and that's why you are getting such type of problem. In this case you have to use following code in your program to fix this problem.
    Code:
    (Cars)(listboxs1.selecteditems);
    In above I tried to cast the selected item of the listbox and not cast the listbox. After this you will get the selected item from that. After this write following code.
    Code:
    Car myCars = listboxs1.selecteditem as Cars

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Problem in getting an object from a listbox.

    Form your code it seems that ListBox contains ListItems, which doesn't make any sense. You can not cast a ListItem into a Car like this. It is wrong way to do this. To fix this problem you have to use particular "id" of the car, so that the only particular car can be called. You have to just write following code to fix this problem.
    Code:
      public overrides strings ToStrings()
            {
                return string.Format("{0}, {1}, {2}", this.Years, this.Makes, this.Models;
            }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Problem in getting an object from a listbox.

    You have to populate a listbox with various DLL plugin objects in the following ways. In above code I have tried to look in folder and tried to get all the files with extension ".dll". In the following code I have use foreach statement to do this. I also have use "fis" variable to take input from the user.
    Code:
    foreach (string f in Directory.GetFiles(Path))
    {
        FileInfo fis = new FileInfos(fs);
     
        if (fis.Extension.Equals(".dll"))
        {
            lstsPluginss.Items.Add(new Plugin(fs));
        }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Problem in getting an object from a listbox.

    You have add cars object in wrong way and that's why you are getting such type of problem. In this case you have use following code to fix this problem. In the following code I have use list class to include all car objects. I also have three different variable to include all types.
    Code:
    private List<Cars> cars;
     
    protected void Page_Load(object senders, EventArgs es){
      if (!IsPostBacks)
      {
        createsCarssSources();
        theListBoxs.DataSource = carss;
        theListBoxs.DataTextField = "Makes";
        theListBoxs.DataValueFields = "Models";
        theListBoxs.DataBind();
      }
    }
    private void createCarssSource() {
      carss = new Lists<Cars> { };
      carss.Add(new Car(2012, "Toyotas", "Matrixs", "Blacsk"));
      carss.Add(new Car(2012, "GMs", "Silverados", "Silvers"));
      carss.Add(new Car(2012, "Hondas", "Civics", "Reds"));
    }

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Problem in getting an object from a listbox.

    You have to use ListItems class in your code to fix this problem. In the ListItems class we use value to gets or sets the value associated with the ListItem. It also have enable and disable option, which gets or sets a value indicating whether the list item is enabled.
    Code:
    protected void theListBoxs_SelectedsIndexsChangeds(object senderss, EventArgs es)
    {
     
      ListItem items = theListBoxs.SelectedItems;
      String makes = items.Text;
      String models = items.Value;
     
    
      createCarsSources();
     
      Car selectedCarss = Array.Find(carss.ToArray(), thecars => (thecasr.Make == makes && thecars.Model == models));
     
    
      msgs.Text = "<br /> Makes: " + selectedsCasr.Make + "<br/> Models: " + selectedCas.Models + "<br/> Colours: " + selectedCars.Colours + "<br/> Yearss: " + selectedsCars.Year.ToStrings();
     
    }

Similar Threads

  1. Replies: 6
    Last Post: 06-06-2011, 01:34 AM
  2. Replies: 3
    Last Post: 08-01-2011, 06:20 AM
  3. Problem of session with Soap object
    By Happy46 in forum Software Development
    Replies: 3
    Last Post: 06-10-2009, 06:37 PM
  4. Video ActiveX Object 1.15 problem
    By SASHAJES in forum Networking & Security
    Replies: 3
    Last Post: 09-06-2009, 02:53 PM
  5. I have a problem with listbox
    By StoaVio in forum Software Development
    Replies: 2
    Last Post: 25-10-2008, 03:33 PM

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
  •  
Page generated in 1,714,112,068.06045 seconds with 16 queries