I want a ListBox full of items. Although, each item should have a different value.
So when the user selects an item and presses a button, a method will be called which will use the value the select item has.
I don't want to reveal the item values to the user.
EDIT: This is not for ASP.NET, it's for a Windows Forms application. I just thought the HTML example would be easy to read.
I have the inspiration from HTML:
<form>
<input type="radio" name="sex" value="Value1" /> Male
<br />
<input type="radio" name="sex" value="Value2" /> Female
</form>
This also allows me to use different values than what the user sees.
You can choose what do display using the DisplayMember of the ListBox.
List<SomeData> data = new List<SomeData>();
data.Add(new SomeData() { Value = 1, Text= "Some Text"});
data.Add(new SomeData() { Value = 2, Text = "Some Other Text"});
listBox1.DisplayMember = "Text";
listBox1.DataSource = data;
When the user selects an item, you can read the value (or any other property) from the selected object:
int value = (listBox1.SelectedItem as SomeData).Value;
Update: note that DisplayMember works only with properties, not with fields, so you need to alter your class a bit:
public class SomeData
{
public string Value { get; set; };
public string Text { get; set; };
}
items have a property called 'Tag', which you can use to store any information you want (hidden from the user)
ListViewItem myItem = new ListViewItem();
myItem.Text = "Users see this";
myItem.Tag = "Users don't see this";
(or set the appropriate properties in the property explorer)
Very simple:
foreach(var item in *Your Source List*)
{
ListItem dataItem = new ListItem();
dataItem.Text = "value to show";
dataItem.Value = *another value you want*;
listBox.Items.Add(dataItem);
}
As stated by the 1st answer, the use of DisplayMember works whether you are using asp.net or winforms.
And to comment a bit more, it also works if you are using the rather old fashion Items.add way of adding items to a ListBox.
Just for fun, here is a simple demo of what you need (just create a new form and drop on it a ListBox and a Label):
public partial class Form1 : Form
{
class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return string.Format("{0} {1}", LastName, FirstName);
}
}
public Form1() { InitializeComponent(); }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
listBox1.DisplayMember = "LastName";
listBox1.DataSource = GetCustomers();
//listBox1.Items.AddRange(GetCustomers().ToArray());
}
private IEnumerable<Customer> GetCustomers()
{
return new List<Customer>()
{
new Customer() { FirstName = "Gustav", LastName = "MAHLER" },
new Customer() { FirstName = "Johann Sebastian", LastName = "BACH" }
};
}
private void lb_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = listBox1.SelectedItem.ToString();
}
}
Enjoy
PS: #2nd post Tag is not available to ListBox: because it accepts an array of object, not a specific item container like ListView... but you don't need any in your case. Tag is useful when you want to carry additional data along with a specific TreeViewItem or ListViewItem for example.
By the way, Tag is defined at the Control level and so exists for Button, Label, and so on... but for my part I think it is rather a bad idea to store business data in it (untyped, UI coupled...) apart from the ListView and TreeView cases for which it is rather convenient.
Easy!
protected void Page_Load(object sender, EventArgs e)
{
llenaListBox(ListBox1, 0, 10);
}
private void llenaListBox(ListBox PoListBox, int PiMinimo, int PiMaximo)
{
int Li;
for (Li = PiMinimo; Li <= PiMaximo; Li++)
{
ListItem obj = new ListItem();
obj.Text = Li.ToString();
obj.Value = Li.ToString();
PoListBox.Items.Add(obj);
}
}
Related
I have a C# WPF application that has a form with two fields. Every time the form is submitted, I want to get the values and use the Instructor class to add the new item to a list. Then, I want to loop through the list and display the items in the ListView element. I realize I can do this without the class, but having the class is a requirement for my school assignment.
Here is my Main Window class:
public partial class MainWindow : Window
{
private List<Instructor> instList;
public MainWindow()
{
InitializeComponent();
List<Instructor> instList = new List<Instructor> { };
}
private void btnCreateInstructor_Click(object sender, RoutedEventArgs e)
{
spCreateInstructor.Visibility = (spCreateInstructor.Visibility == Visibility.Hidden) ? Visibility.Visible : Visibility.Hidden;
}
private void btnInstructorSubmit_Click(object sender, RoutedEventArgs e)
{
instList.Add(new Instructor { firstName = txtInstructorFirstName.Text, lastName = txtInstructorLastName.Text });
foreach (var inst in instList)
{
lvInstructorList.Items.Add("{0} {1}", inst.firstName, inst.lastName);
//Error occurs on the line above.
}
}
}
This is the instructor class:
class Instructor
{
public string firstName { set; get; }
public string lastName { set; get; }
}
My problem is that I get an error saying No overload for method Add takes 3 arguments What am I doing wrong? I have indicated where the error occurs with a comment in the code.
Try replacing this line:
lvInstructorList.Items.Add("{0} {1}", inst.firstName, inst.lastName);
with this one
lvInstructorList.Items.Add(new Instructor { firstName = inst.firstName, lastName = inst.lastName });
It is similar to how you added to the instList.
Edit
After realizing lvInstructorList is a ListView xaml element, this should work:
var listViewItem = new ListViewItem(new Instructor { firstName = inst.firstName, lastName = inst.lastName });
lvInstructorList.Items.Add(listViewItem);
ah here is how to create a listview and add items to it in wpf
check this question Add Items to Columns in a WPF ListView
In short
list view takes an object so you need to add items like that
lvInstructorList.Items.Add(lvInstructorList.Items.Add(new Instructor { firstName = inst.firstName, lastName = inst.lastName });
and you should bind to it in xaml
<ListView x:Name="lvInstructorList">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding firstName }"/>
</GridView>
</ListView.View>
Not sure why you want to loop again and again to the lvInstructorList. But you have to clear every time.
instList.Add(new Instructor { firstName = txtInstructorFirstName.Text, lastName = txtInstructorLastName.Text });
lvInstructorList.Clear();
foreach (var inst in instList)
{
lvInstructorList.Items.Add(inst);
}
It looks to me like you are not intending on binding the list view to an object, but rather just insert string values into the list view. If that is the case:
lvInstructorList.Items.Add(string.Format("{0} {1}", inst.firstName, inst.lastName));
So I'm going back and cleaning up code I did years ago. In one part, (and I used this several times) I have a list box displaying employee names such as
Smith, John
Doe, Jane
When the user clicks the name, I do something like
String unBrokenName = ListBox1.SelectedItem.ToString();
String LastName = unBrokenName.Substring(...
You get the idea, I extract the first and last name based upon the ", "
Then I do this to get the employee from the sql database.
Employee SelectedEmployee = Employee.GetEmployeeByFirstLast(FirstName, LastName);
At the time, it was the best I knew. Now it feels wrong, because I KNOW I should be able to get the sql ID of the employee when they select it, like
int EmployeeId = SOMELISTBOXSELECTEDITEMPROPERTY;
Employee SelectedEmployee = Employee.GetEmployeeByID(EmployeeId);
Is there some property for a listbox item that will store this id while displaying the same name the users are used to seeing?
You can actually add anything you'd like to a listbox:
class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return this.Name;
}
}
And then:
listBox1.Items.Add( new Foo() { Id = 101, Name = "Foo Bar" } );
listBox1.Items.Add( new Foo() { Id = 102, Name = "Foo Bar Jr." } );
The SelectedItem property will now give you the selected Foo, while displaying the Name property in the list itself.
private void listBox1_SelectedIndexChanged( object sender, EventArgs e )
{
Foo item = ( listBox1.SelectedItem as Foo );
if( item != null )
{
// use item.Id here
}
}
Instead of overriding ToString, you can also use the DisplayMember property of the listbox to select which property the listbox will display.
You can do something like this:
listBox1.DataSource = employeesList;
listBox1.DisplayMember = "LastName";
listBox1.ValueMember = "EmployeeId";
When you run your application the listbox will have the list of employees that you are passing and it will show the LastName. But when you select an item, you can access the id by:
`listBox1.SelectedValue();`
And then in the listbox1_Click event something like:
if (listBox1.SelectedIndex != -1)
{
int employeeId = listBox1.SelectedValue();
//do something here;
}
Sorry for my bad English :(.
Hi, how do I add the items to a ListView that I've put in a List?
I've tried this:
listView1.Items.Add(pluginContainer);
But this doesn't seem to work :(.
I can't make a foreach loop because then it will take like 10 seconds for the ListView to be filled (I'm talking about 5000+ items).
This fixed it:
listView1.Items.AddRange(pluginContainer.ToArray());
If the items in your list are all of type ListViewItem, you can use AddRange. If they're not, you're going to have to either make ListViewItems out of them, or use a for loop.
In either case, you should strive to improve the performance of a ListView during addition of items, by first calling SuspendLayout on it. After you've added all its items, call ResumeLayout.
Try this:
public enum State
{
AL, GA, FL, SC, TN, MI
}
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public State State { get; set; }
// Converts properties to string array
public string[] ToListViewItem()
{
return new string[] {
ID.ToString("00000"),
Name,
State.ToString() };
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Setup list view column headings and widths
listView1.Columns.Add("ID", 48);
listView1.Columns.Add("Name", 300);
listView1.Columns.Add("State", 48);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Create a list
List<Person> list = new List<Person>();
// Fill in some data
list.Add(new Person() { ID=1001, Name="John", State=State.TN });
list.Add(new Person() { ID=1002, Name="Roger", State=State.AL });
list.Add(new Person() { ID=1003, Name="Samantha", State=State.FL});
list.Add(new Person() { ID=1004, Name="Kara", State=State.MI});
// Fill in ListView from list
PopulateListView(list);
}
void PopulateListView(List<Person> list)
{
listView1.SuspendLayout();
for(int i=0; i<list.Count; i++)
{
// create a list view item
var lvi = new ListViewItem(list[i].ToListViewItem());
// assign class reference to lvi Tag for later use
lvi.Tag = list[i];
// add to list view
listView1.Items.Add(lvi);
}
//This adjust the width of 1st column to fit data.
listView1.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
listView1.ResumeLayout();
}
}
I have a table that have these fields: ID , Name
I have bound a listbox to the table.
My question is, when the user has selected an item in listbox, how would I find out what the ID of the selected item is?
Note: The id is not equal to the selectedindex or id of items in items list
e.g.
Suppose you have a DataTable dt, with columns ID and Name in it.
then while binding include the following code,
this.listbox.DataSource = dt;
this.listbox.DisplayMember = "Name";
this.listbox.ValueMember = "ID";
while reading the selected values of listbox,
this.listbox.SelectedItem will give u the selected Name and
this.listbox.SelectedValue will give u the corresponding ID
test it
lst.SelectedItem.Value;
OR
lst.SelectedValue;
where lst is a ListBox Cotrol
What type of application is this? ASP.net, Windows Forms, WPF?
I have a feeling you are working with Windows Forms, as the other two are much clearer...
Here is some code for a Windows Forms App... Basically, you create your own class, and use that for the list items. The list box will display the results of the ToString() method, so override that to get the value you wanna display. When you access ListBox.SelectedItem, it will be an instance of the class you defined, and you can access whatever properties are necessary:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MyListItem item1 = new MyListItem("Java", 1);
MyListItem item2 = new MyListItem("C#", 221);
MyListItem item3 = new MyListItem("C++", 13);
listBox1.Items.Add(item1);
listBox1.Items.Add(item2);
listBox1.Items.Add(item3);
}
private class MyListItem
{
public string ItemName { get; set; }
public int ItemId { get; set; }
public MyListItem(string name, int id)
{
this.ItemName = name;
this.ItemId = id;
}
public override string ToString()
{
return this.ItemName;
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MyListItem selectedItem = (MyListItem)listBox1.SelectedItem;
MessageBox.Show(string.Format("Name is: {0}, Id is: {1}", selectedItem.ItemName, selectedItem.ItemId));
}
}
I have a situation that is pretty simple, and I'd like to know the ideal way to do it.
I have a combo box. Each line of the combo box corresponds to a particular strategy object.
What is the proper way to map the combo box lines to the strategy object.
The way I was doing it seems overly complicated, and I'm pretty much guaranteed there is a simple standard way to do this.
Thank you.
EDIT:
I had the data in a Dictionary, where the string was the text for the combobox, and the object was the strategy... But this isn't ordered... And I just know there is some extremely simple way to do it.
SOLUTION:
I used this solution, not feeling comfortable putting presentation logic in the data classes:
private partial class HtmlTransformState : AbstractHtmlEditFormState
{
private Dictionary<string, ITransformStrategy> strategies = new Dictionary<string, ITransformStrategy>()
{
{ "Simple URL", new TransformStrategy<SimpleUrlCodeExtractor>() },
{ "Overview", new TransformStrategy<OverviewCodeExtractor>() },
{ "Video List", new TransformStrategy<VideoListCodeExtractor>() },
{ "Video List No MbORKb", new TransformStrategy<VideoListNoMBOrKBAndNoLinksAllowedCodeExtractor>() },
{ "Blue Mountain 2007", new TransformStrategy<BlueMountain2007CodeExtractor>() },
{ "Four Gates", new TransformStrategy<FourGatesCodeExtractor>() },
{ "General", new TransformStrategy<GeneralCodeExtractor>() }
};
public override void DrawForm()
{
// ...
ParentForm.cmboTransformStrategy.DataSource = new BindingSource(strategies, null);
ParentForm.cmboTransformStrategy.DisplayMember = "Key";
ParentForm.cmboTransformStrategy.ValueMember = "Value";
}
public override IEnumerable<string> ProcessHtml(string urlPath)
{
ITransformStrategy transformStrategy = (ITransformStrategy)ParentForm.cmboTransformStrategy.SelectedValue;
// Do some stuff with 'transformStrategy'
}
}
Do you mean something like the following?
public class Strategy
{
private string _name = "default";
public string Name
{
get { return _name; }
set { _name = value; }
}
public Strategy(string name)
{
_name = name;
}
}
Then in form load (you need to have a combo box on that form):
private void Form1_Load(object sender, EventArgs e)
{
List<Strategy> ls = new List<Strategy>();
ls.Add(new Strategy("First"));
ls.Add(new Strategy("Second"));
ls.Add(new Strategy("Third"));
comboBox1.DataSource = ls;
comboBox1.DisplayMember = "Name";
}
Override ToString for your strategy object. After that you can insert your strategy objects directly in the combo box.
public class StrategyObject
{
public override string ToString()
{
return "return the text to display";
}
}
StrategyObject selectedStratObj = comboBox1.SelectedItem as StrategyObject;
I would use the SelectedIndexChanged event on the combobox and select the corresponding dictionary entry
found that, Bind a Dictionary to a ComboBox see below for a working example(at least on the original vb.net code that I wrote)
Vb.net converted into C#, you will have to manage the handle yourself
public class Form1
{
private Dictionary<int, myDic> dict = new Dictionary<int, myDic>();
private void // ERROR: Handles clauses are not supported in C#
ComboBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
KeyValuePair<int, myDic> curItem = (KeyValuePair<int, myDic>)ComboBox1.SelectedItem;
MessageBox.Show(curItem.Value.myvalue);
}
private void // ERROR: Handles clauses are not supported in C#
Form1_Load(object sender, System.EventArgs e)
{
myDic d = default(myDic);
for (int i = 0; i <= 10; i++) {
d = new myDic();
d.myKey = i.ToString;
d.myvalue = Strings.Chr(65 + i);
dict.Add(d.GetHashCode, d);
}
ComboBox1.DataSource = new BindingSource(dict, null);
ComboBox1.DisplayMember = "value";
ComboBox1.ValueMember = "Key";
}
}
class myDic
{
public string myKey;
public string myvalue;
public override string tostring()
{
return myvalue;
}
}
Here's one of my finest innovations. :) I'm really proud of this little one.
public class Stringable<T>
{
private T _obj;
private Func<T, string> _convertFn;
public Stringable(T obj, Func<T, string> convertFn)
{
_obj = obj;
_convertFn = convertFn;
}
public T GetObj() { return _obj; }
public override string ToString() { return _convertFn(_obj); }
}
This generic class adds ToString() to any class (even a black-box class) and you can define its behavior inside the lambda. Imagine you have a class Person with properties FirstName and LastName. Here's how you would use it to populate Combo box.
_cboPersons.Items.Add(new Stringable<Person>(person,o=>string.Format("{0}, {1}", o.LastName, o.FirstName)));
Then, when combo box item is selected just use this to get the original object from your combo
Person person=(_cboPersons.SelectedItem as Stringable<Person>).GetObj() // Get's person object.