I'm building an application that isn't web based and therefore doesn't use System.Web.UI.WebControls
My aim is to set the item value in a listbox when I populate it along with its text value (hence ListItem). But without System.Web.UI.WebControls I don't have access to it. Any suggestions? I'm new at this so please be gentle, I know it's probably a stupid question.
Loading

Keyur Narkhi62 reputationPosted Apr 30, 2012, 1:29 AM EDT
If you are using windows application following is the link which helps you
http://www.dotnetperls.com/listbox
Or please explain more your problem
Hope this helps.
thanks
Jaish MathewsRank #225 · 11K reputationPosted Apr 30, 2012, 1:36 AM EDT
If it's not web application, it might be a windows application, I am assuming. If so, you need to use ComboBox control. There are different set of built-in controls for web and windows applications and we need to choose them accordingly. Please read this link telling about built-in win form controls supported by .NET
http://msdn.microsoft.com/en-us/library/3xdhey7w.aspx
Kyle5 reputationPosted Apr 30, 2012, 1:45 AM EDT
Jaish MathewsRank #225 · 11K reputationPosted Apr 30, 2012, 1:58 AM EDT
The bolded part itself making sense. 1st one telling Web and 2nd one Windows. Ok, now you need to choose a control under System.Windows.Form
Normally it should be ComboBox, but it depend on you. See the link here to see how ComboBox control can use
http://msdn.microsoft.com/en-us/library/4ex92czb.aspx
Again don't try Web controls in WinForms and don't try windows controls to WebForm
Kyle5 reputationPosted Apr 30, 2012, 3:39 AM EDT
That's some sound advise although I'm surprised that such a useful control is confined so strictly to a using. I ended up keeping my Listbox and made my own ListItem class. Works great so far but if it bugs out I'll post the errors here.
Here's the code for those that are as rusty/new to this as I am:
public class ListItem
{
private string id = string.Empty;
private string name = string.Empty;
public ListItem(string sid, string sname)
{
id = sid;
name = sname;
}
public override string ToString()
{
return this.name;
}
public string ID
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
And later in my code I use it like so:
foreach (DataRow dr in dt.Rows)
{
string a = "This is a persons name";
string b = "This is an persons ID";
ListItem li = new ListItem(b, a);
lbPeople.Items.Insert(x, li);
x++;
}
EDIT: Here's the source I used: forums.asp.net/t/1232340.aspx/1