Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 02-03-2010
Member
 
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.
Reply With Quote
  #2  
Old 02-03-2010
Member
 
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
Reply With Quote
  #3  
Old 02-03-2010
Member
 
Join Date: May 2008
Posts: 2,008
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;
        }
Reply With Quote
  #4  
Old 02-03-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
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));
    }
}
Reply With Quote
  #5  
Old 02-03-2010
Member
 
Join Date: May 2008
Posts: 2,293
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"));
}
Reply With Quote
  #6  
Old 02-03-2010
Member
 
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();
 
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem in getting an object from a listbox."
Thread Thread Starter Forum Replies Last Post
Getting HP AIO Device object server register class object failed message on my system Donoho Hardware Peripherals 6 06-06-2011 01:34 AM
Scope of Object or variable in Object oriented Programming Languages Dėfrim Software Development 3 08-01-2011 06:20 AM
Problem of session with Soap object Happy46 Software Development 3 06-10-2009 06:37 PM
Video ActiveX Object 1.15 problem SASHAJES Networking & Security 3 09-06-2009 02:53 PM
I have a problem with listbox StoaVio Software Development 2 25-10-2008 03:33 PM


All times are GMT +5.5. The time now is 03:21 AM.