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
KylePosted Apr 30, 2012, 7:39 AM
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
Jaish MathewsPosted Apr 30, 2012, 5:58 AM
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
KylePosted Apr 30, 2012, 5:45 AM
Jaish MathewsPosted Apr 30, 2012, 5:36 AM
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
Keyur NarkhiPosted Apr 30, 2012, 5:29 AM
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