i have a class which looks like this
public class Process_Items
{
String Process_Name;
int Process_ID;
//String Process_Title;
public string ProcessName
{
get { return Process_Name; }
set { this.Process_Name = value; }
}
public int ProcessID
{
get { return Process_ID; }
set { this.Process_ID = value; }
}
}
now i want to create a Process_Items[] Array and display all the elements in a multi column listbox. Such that first column must have the processName and 2nd must have the processID. How can i achieve this in C# 2.0?
You should use a ListView control and add two columns (ListBox only has one column)
Process_Items[] items = new Process_Items[] // Initialize array
foreach(Process_Items p in items) {
listView.Items.Add(p.ProcessName).Subitems.Add(p.ProcessID.ToString());
}
A list box has a single ListItem (string) displayed to the user.
So you could override ToString() as
public override string ToString()
{
return string.Format("{0} [ProcID: {1}]", this.Process_Name , this.ProcessID);
}
If this is for a winforms app, have a look at the ListView Control or a DataGridView
What kind of control do you use for the list? If you use a ListView, then you can do like this (assuming that instance is a Process_Items - which btw is a strange name for a class IMO - instance):
listView1.Items.Add(instance.ProcessName).SubItems.Add(instance.ProcessID.ToString());
Related
I'm trying to get the following to work:
A class called Caption, where I populate a List - adding Items to the Class list
I then want to reference the list of class values, in a lookup method
public class Caption
{
readonly CaptionKey _CaptionKey; //Enum list
readonly string _Description;
public Caption(CaptionKey captionKey, string description)
{
_CaptionKey = captionKey;
_Description = description;
}
public CaptionKey CaptionKey { get { return _CaptionKey; } }
public string Description { get { return _Description; } }
}
Here is the class that creates the class list
public class InitCaptions
public static List<Caption> _Captions = new List<Caption>();
// the class access I need
public static string LookupCaption( CaptionKey )
{
//? How to return the description for
}
The problem is with referencing the list of classes from another class and process.
I can see the values in the debugger are there-
System.Collections.Generic.List<MyNamespace.Controllers.Captions>
I'm just not sure how to reference it properly.
I should add - this is a MVC solution - so the List is created in the application startup, but the reference call is done from a report - using ReportViewer. the List shows in the code using Intellisense, but when run - the List is not there.
Assuming a Simple Dictionary<> won't suit your needs (for reasons you have not specified),
then to find and return an item from a List you can use the List.Find method, which takes a delegate and is used like this:
Caption foundItem = _Captions.Find(delegate (Caption obj) { return obj.Description.equals("Find this Description"); });
If there is more than one, then it will return the first it finds.
There is also the FindAll(....) version of this which will return a new list of all matches items.
Is it possible to do this:
public class ParameterWrapper
{
public string Name { get; set; }
public Definition Definition { get; set; }
public StorageType StorageType { get; set; }
}
and then use it in a BindingList<ParameterWrapper> to bind it to a ComboBox control like this:
private void PopulateDropdownBinding(ComboBox control, BindingList<ParameterWrapper> parameters)
{
control.DataSource = parameters;
control.DisplayMember = "Name";
control.ValueMember = ???;
}
Where I have the "???" is my question. I want to return the actual Wrapper object. So the Display Name is the Wrapper.Name but Display Value is Wrapper. Can that be done?
Of course I could take the BindingList<ParameterWrapper> and stick into a Dictionary<string, ParameterWrapper> but I was hoping for a more streamlined solution. Ideas?
If you leave ValueMember unspecified, Value will return the item itself -- in this case, your ParameterWrapper instance.
Much less here than meets the eye!
You could add another property to your wrapper:
...
public ParameterWrapper Self { get { return this; } }
...
And set your ValueMember="Self";
Another (IMHO, better) option is to leave ValueMember unset (null by default) and use SelectedItem instead of SelectedValue
I'm sure there should be an easier way to do this. I have a class based on Collection, and that class is a collection of another class. Currently, whenever I have a new item in my class, I assign that item to the listbox. I can't seem to figure out a way to assign all of the values in the collection class, because it is a collection, to the collection of the listbox. Any ideas? Thanks
Ok, what I've done so far is I have a tostring override in the Class used in the collection. This is what I want the listbox to show.
public override string ToString()
{
return string.Format("{0} {1}: {2}", tTypev.ToString(),
Datev.ToString("MM/dd/yyyy"), Amountv.ToString("C"));
}
Which is what I want each item in the listbox to show.
class Transactions : System.Collections.CollectionBase
{
...
}
Is my collections class, containing a collection of the other class, Tansaction. Curently, I use the lstTransactions.Items.Add(), .Remove, .RemovAt, etc to add items to the list box, and the .Add(), .Remove, etc to add items to the Collection Class, Transactions. But I'm trying to decrease reliance on outside controls, and only use them in a few lines of code. I was trying to use something like:
lstTransactions.DataSource = (Transaction)myTrans;
but that didn't seem to work. Mainly because I couldn't figure out what property DataSource took.
I also tried:
lstTransactions.Items =
but it told me that items was read only.
In Windows Form:
You could used DataSource property to bind a collection of object.
DisplayMember property to the name of a property in the data source object.
Sample Code:
In the below sample the output list box would display 2 items : Apple and Ball.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
EmployeeCollection data = new EmployeeCollection();
data.AddEmployee(new Employee("Apple", 25));
data.AddEmployee(new Employee("Ball", 50));
listBox1.DataSource = data;
listBox1.DisplayMember = "Name";
}
}
public class Employee
{
public Employee(string name, int age)
{
this.Name = name;
this.Age = age;
}
public int Age
{
get; private set;
}
public string Name
{
get; private set;
}
}
public class EmployeeCollection : System.Collections.CollectionBase
{
public void AddEmployee(Employee employee)
{
this.List.Add(employee);
}
}
}
In WPF:
You could use ItemSource property of ListBox to bind a collection (which may be a List, Enumerable, Collections.._ do the job
Sample snippet:
IList<string> data = new List<string>() {"A", "B"};
ListBox listBox = new ListBox();
listBox.ItemsSource = data;
I have a list view with two columns- name and number. I want to read all these items and assign name to a combo box display member and number to value member. I have tried thinking the approach to follow but couldn't help myself. This is what I have tried. How should I proceed?
public class numbers
{
public string name;
public string number;
}
public class names : List<numbers>
{
}
names cname = new names();
public void addcontacts()
{
foreach(ListView lv in bufferedListView1)
{
//No idea how to proceed
First you set your own type:
public class myContact
{
public string Name { get; set; }
public string Number { get; set; }
public myContact(string name, string number)
{
this.Name = name;
this.Number = number;
}
public override string ToString()
{
return Name;
}
}
Then you transfer all items from the listview to the combobox like this:
foreach (ListViewItem item in listView1.Items)
{
comboBox1.Items.Add(new myContact(item.Text, item.SubItems[0].Text));
}
This example assumes, that each listviewitem holds the name and that its first child holds the number.
When you add objects to the combobox, C# will use the objects' ToString() method to create something that you can actually see when the program is running. You override the default ToString() method with your own and only return the name. If you want to use the selection from the combobox you just cast the selectedItem back to myContact and can access the number there. Welcome to OOP :)
Let's say I have a ListBox called animalList. As DataSource I use following class:
class Animal
{
private int id;
private string name;
private string description;
public Animal(int id, string name, string description)
{
// implementation
}
public int Id
{
// implementation
}
public string Name
{
// implementation
}
public string Description
{
// implementation
}
}
I'd like to have 2 columns in ListBox: Name and Description. Is it possible to do so?
I managed to add one column like this:
List<Animal> animals = // LINQ sucking data from XML
animalList.MultiColumn = true;
animalList.DataSource = animals;
animalList.DisplayMember = "Name";
animalList.ValueMember = "Id";
but I cannot really figure out how to actually implement next column.
The name of the MultiColumn property can be slightly misleading. The ListBox control does not support the type of columns you're looking for.
What MultiColumn actually does is "overflow" items into a new column instead of showing a vertical scrollbar.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.multicolumn.aspx