listbox showing class name

R

Rotsey

Hi,

I have a listbox and I am adding objects to it with AddRange.

The problem is the listbox just shows the name of object
class in every row and not the DisplayMember see below.

Any ideas??

void mWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

this.Text = string.Format("Find Duplicates: {0} - {1}%",mDriveData.Drive,
e.ProgressPercentage);

toolStripProgressBar1.Value = e.ProgressPercentage;

List<DuplicateFileName> filelist = e.UserState as List<DuplicateFileName>;

DuplicateFileName[] files = filelist.ToArray();

listBox1.Items.AddRange(files);

listBox1.DisplayMember = "FileName";

listBox1.ValueMember = "ID";

}
 
M

Morten Wennevik [C# MVP]

Hi,

Make sure DuplicateFilesName has a "FileName" property, and not Filenameor any other spelling. If the listbox cannot find the property, it willrevert to default object.ToString(), and the default ToString implementation returns the class name.

Instead of doing filelist.ToArray and listbox.AddRange, you can simply do

listBox1.DataSource = filelist;


Hi,

I have a listbox and I am adding objects to it with AddRange.

The problem is the listbox just shows the name of object
class in every row and not the DisplayMember see below.

Any ideas??

void mWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

this.Text = string.Format("Find Duplicates: {0} - {1}%",mDriveData.Drive,
e.ProgressPercentage);

toolStripProgressBar1.Value = e.ProgressPercentage;

List<DuplicateFileName> filelist = e.UserState as List<DuplicateFileName>;

DuplicateFileName[] files = filelist.ToArray();

listBox1.Items.AddRange(files);

listBox1.DisplayMember = "FileName";

listBox1.ValueMember = "ID";

}
 
R

Rotsey

The FileName property is spelled/cased correctly

You will notice that my ocde is in ProgressChnaged event
so the listox is to be filled incremently.

Using the DataSource property correctly uses the displaymenber/valuemember
properties.

So why doesn't AddRange work the same????




Hi,

Make sure DuplicateFilesName has a "FileName" property, and not Filename or
any other spelling. If the listbox cannot find the property, it will revert
to default object.ToString(), and the default ToString implementation
returns the class name.

Instead of doing filelist.ToArray and listbox.AddRange, you can simply do

listBox1.DataSource = filelist;


Hi,

I have a listbox and I am adding objects to it with AddRange.

The problem is the listbox just shows the name of object
class in every row and not the DisplayMember see below.

Any ideas??

void mWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

this.Text = string.Format("Find Duplicates: {0} - {1}%",mDriveData.Drive,
e.ProgressPercentage);

toolStripProgressBar1.Value = e.ProgressPercentage;

List<DuplicateFileName> filelist = e.UserState as List<DuplicateFileName>;

DuplicateFileName[] files = filelist.ToArray();

listBox1.Items.AddRange(files);

listBox1.DisplayMember = "FileName";

listBox1.ValueMember = "ID";

}
 
C

Chris Shepherd

Rotsey said:
The FileName property is spelled/cased correctly

You will notice that my ocde is in ProgressChnaged event
so the listox is to be filled incremently.

Using the DataSource property correctly uses the displaymenber/valuemember
properties.

So why doesn't AddRange work the same????

It does. There must be some other bug. See below.

When I load that form, I get a listbox with the words One, Two, Three,
and Four as the items in it. In .NET 2.0 it seems to work fine anyways.

Chris.


using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProofOfConcept
{
public class Form1 : Form
{

protected class InnerTestObject
{
private string someValue;

public InnerTestObject() : this("") { }

public InnerTestObject(string val)
{
this.someValue = val;
}

public string Jabberwocky
{
get { return this.someValue; }
set { this.someValue = value; }
}
}

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.AddRange(new InnerTestObject[] {
new InnerTestObject("One"),
new InnerTestObject("Two"),
new InnerTestObject("Three"),
new InnerTestObject("Four")});

listBox1.DisplayMember = "Jabberwocky";
}

private ListBox listBox1;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(12, 12);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 95);
this.listBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(251, 238);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}

#endregion

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top