Hi,
Id like to know why this isnt working.
Ive created a class:
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
}
Id like to add objects to a listbox, in the following way;
Class1 myclass = new Class1();
Listbox1.Item.Add(myclass);
Which is not working.
I am aware that Listbox1.Item.Add accepts only Items and strings. But I am following a tutorial which does exactly this, instantiates a class and adds the objects to a Listbox and works fine.
ThanksListbox1.Item.Add accepts only Items and strings.Not quite true. The Add() function looks for an object type argument, so the code you posted is correct and should work.
What error is it showing? Please post the whole code you are using.
If you are not getting what you want displayed, override the ToString() method in your class and have it return what you want displayed, this may be some sort of a property in your class for example.
Hi Menhak,
I`ve modified the class to:
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public string text;
public override string ToString()
{
return text;
}
}
And I am able to add ListItems as per;
Class1 tt = new Class1();
// ListItem mm = (Object)tt;
tt.text = "Raul";
this.ListBox1.Items.Add(tt.ToString());
I'd like to now be able to check of what type the selected listbox item is and compare to type of Class1;
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedItem.GetType() == typeof(Class1 ))
{
Response.Write("It does enter");
// this.Image1 = ((Class1 )ListBox1.SelectedItem).downloadedimage;
}
}
But this is evaluating to false. As,
ListBox1.SelectedItem.GetType()
returns ,
System.Web.UI.WebControls.ListItem
Any suggestions?
That may work in windows forms, but in the world of web programming, your type cannot be maintained as such. Instead, your listitem will hold a value you can use to query your array of class objects and compare to.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment