C# ListBox displays wrong format when used with ListViewItem - c#

This is my code to populate a ListBox named delBooks with a ListViewItem object and items .Text and .Tag properties.
ListViewItem item = new ListViewItem();
item.Text = "ss"; // Or whatever display text you need
item.Tag = "dd";
delBooks.Items.Add(item);
The output I see in the ListBox looks like this:
ListViewItem: {ss}
How can I correct this so it will display ss in the ListBox?

Set the DisplayMember on the ListBox to Text (as a string).

An object like ListViewItem does not exist for ListBox. This is one of the reasons that the ListBox control was superseded by the ListView control. In order to get ListViewItem like functionality out of a ListBox control you must implement your own object
class ListBoxItem
{
public string Text { get; set; }
public string Tag { get; set; }
public ListBoxItem(string text, string tag)
{
this.Text = text;
this.Tag = tag;
}
}
To populate the ListBox with your custom object simply do:
listbox.DisplayMember = "Text";
listbox.Items.Add(new ListBoxItem("ss", "dd"));
Where the .DisplayMember property of ListBox is the name of the property of your custom object that is to be displayed in the ListBox to the user.
If you need to access your custom objects values based on your ListBox item collection you can do a simple cast to retrieve the these values:
MessageBox.Show( ((ListBoxItem)listbox.Items[0]).Tag) );
Where the .Tag property is the value "dd" that we set earlier
PS: If you're a stickler for design like I am this method will also work with a struct
EDIT: If you are truly dead set on using ListViewItem you technically can just by setting the .DisplayMember to (in your case) the .Text property of the ListViewItem object

you need to change your last line like "delBooks.Items.Add(item.Text.toString());"

Related

C# - How do I set the selected item in a combobox by comparing my int value?

I'm using a ComboBox with items having text and value. Now, I want to simply make an item selected by comparing its value with the provided value. I'm iterating through the items and comparing as follow. Below code works fine, but is there a better or more simpler way to do this? I found a possible duplicate here but it works with the string value not integer.
foreach (ComboboxItem item in this.CampaignList.Items)
{
if (Convert.ToInt16(item.Value) == objAACampaign.CompanyId)
{
this.CampaignList.SelectedIndex = this.CampaignList.Items.IndexOf(item);
break;
}
}
Use display and value memeber
Create custom class like this:
class MyCustomClass
{
//important to have get set part
public _int { get; set; }
public _string { get; set; }
}
now load data you want to display inside List<MyCustomClass>() and then bind that list to combobox and set it's display and value member like this:
myComboBox.DisplayMember = "_string";
myComboBox.ValueMember = "_int";
myComboBox.DataSource = myList; //this is List<MyCustomClass>
Now simply use myComboBox.SelectedValue = valueYouWant
IMPORTANT!!!
Declare displayMember and valueMember before binding datasource to combobox because of perfomance. Search internet for more info.

Using C#, how can I create invisible property on listbox item and access it?

I'm using C# Windows Forms, and I have created a listbox and I want to create a value within each individual item in this listbox that is not visible. So to do this I have choosen to use the Display Member. There is only one problem. The name of the item I create that is visible to the user, in the listBox, is not the DisplayMember in the code which is "Item Name". Instead the name of the Item I create appears as "Doc_Engine.PersonalMessageSystemForm+SomeData".
Here is my first code:
//Create an item in the listBox1.
List<SomeData> data = new List<SomeData>();
data.Add(new SomeData() { Value = 1, Text = "Hidden Text" });
listBox1.DataSource = data;
listBox1.DisplayMember = "Item Name";
And here is my second code:
//A class that contains two required strings.
public class SomeData
{
public int Value { get; set; }
public string Text { get; set; }
}
Click here to see a picture of my ListBox
I want the name of my Item in the listbox to be "Item Name". Not "Doc_Engine.PersonalMessageSystemForm+SomeData".
Update: I might use the Tag property instead. But when I do this, the same thing happens. The name of the item does not turn out as desired.
Your SomeData class doesn't have a member called Item Name. It has two members: Value and Text. So, per the docs you get the result of calling ToString instead:
If the specified property does not exist on the object or the value of DisplayMember is an empty string (""), the results of the object's ToString method are displayed instead.
Change it to Value or Text or add some other property to hold the value you want to display.
In winforms controls there is a Tag property you can set to anything you want and later retrieve. Try using that instead.

How do I compare the selected item in a combobox to match my settings class in the properties

My combobox contains 'Control', 'Alt' & 'Shift'.
My predefined string in the settings class is 'Control'.
How do i compare those 2 Strings :
= SelectedItem in the Combobox
= predefined string in the settings class
Because i want to save the changed selectedItem in my settings class, so whenever i start the application again it should load the new SelectedItem in the Combobox.
Edit: Code looking actually like this, but it wont work.
if (cmbModifier.SelectedItem.ToString() != ClipboardPro.Properties.Settings.Default.SavedModifier.ToString())
{
modkey = cmbModifier.SelectedItem.ToString();
ClipboardPro.Properties.Settings.Default.SavedModifier = modkey;
ClipboardPro.Properties.Settings.Default.Save();
}
the SelectedItem property returns the full listitem object you used to fill your combobox.
If you are looking for the value, you can use SelectedValue.ToString()
if (cmbModifier.SelectedValue.ToString() != ClipboardPro.Properties.Settings.Default.SavedModifier.ToString())
{
modkey = cmbModifier.SelectedValue.ToString();
ClipboardPro.Properties.Settings.Default.SavedModifier = modkey;
ClipboardPro.Properties.Settings.Default.Save();
}

How to Get id key from List View - C# Windows application

I am listing set of table items(only names) in List View. I enabled checkbox property of ListView. I displayed all items as Large icons. I want to attach a key(id) for that items for further processing on that items.. If any idea , please reply
Use the ListViewItem.Name property. Poorly named, it is actually the key. The one you can pass to ListView.Items.IndexOfKey() or ListView.Items["key"].
Description
You should use the Tag property for things like that.
Gets or sets the object that contains data about the control.
You can set your id or any other object to the ListItem you want.
Sample
Add the Item
ListViewItem myItem = new ListViewItem();
myItem.Tag = 1; // or any other object
listView1.Items.Add(myItem);
Use the index
private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
ListViewItem myItem = e.Item;
int index = (int) myItem.Tag; // cast to your object type, int in this case
// use the index
}
More Information
MSDN - Control.Tag Property

Setting the selected value of a ListBoxView as a string (C#)

When a value is selected in a ListBoxView, How could the selection be stored as a string / variable, so I can then call another method that will use this string to perform an action?
If the Items in your listbox are all strings, you could use
System.Windows.Forms.ListBox lb= new ListBox();
lb.SelectedItem.ToString();
Otherwise, we will need to know the types of items you have added to the listbox.
If you using ListBox get selected item like this:
string item = listBox1.SelectedItem.ToString();
If you using ListView get selected item(s) depending on MultiSelect property:
Use this code if MultiSelect property = false
string itemName = listView.SelectedItems[0].Name;
Use this code if MultiSelect property = true
foreach (ListViewItem item in ltvMainMenu.SelectedItems)
{
string itemName = item.Name;
}

Categories