473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

listbox showing class name

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<DuplicateFileNamefilelist = e.UserState as List<DuplicateFileName>;

DuplicateFileName[] files = filelist.ToArray();

listBox1.Items.AddRange(files);

listBox1.DisplayMember = "FileName";

listBox1.ValueMember = "ID";

}
Oct 28 '07 #1
4 2801
Hi Rotsey,

An object should have overrided .ToString() method to show what you need
or you should implement custom drawing (see DrawItem or such event)

Regards, Alex
[TechBlog] http://devkids.blogspot.com
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<DuplicateFileNamefilelist = e.UserState as
List<DuplicateFileName>;

DuplicateFileName[] files = filelist.ToArray();

listBox1.Items.AddRange(files);

listBox1.DisplayMember = "FileName";

listBox1.ValueMember = "ID";

}

Oct 28 '07 #2
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;
On Sun, 28 Oct 2007 12:59:04 +0100, Rotsey <ma***********@RemoveThis.optusnet.com.auwrote:
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<DuplicateFileNamefilelist = e.UserState as List<DuplicateFileName>;

DuplicateFileName[] files = filelist.ToArray();

listBox1.Items.AddRange(files);

listBox1.DisplayMember = "FileName";

listBox1.ValueMember = "ID";

}


--
Happy coding!
Morten Wennevik [C# MVP]
Oct 28 '07 #3
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????


"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.t0wvt3fqdj93y5@ubuan...
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;
On Sun, 28 Oct 2007 12:59:04 +0100, Rotsey
<ma***********@RemoveThis.optusnet.com.auwrote:
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<DuplicateFileNamefilelist = e.UserState as List<DuplicateFileName>;

DuplicateFileName[] files = filelist.ToArray();

listBox1.Items.AddRange(files);

listBox1.DisplayMember = "FileName";

listBox1.ValueMember = "ID";

}


--
Happy coding!
Morten Wennevik [C# MVP]
Oct 29 '07 #4
Rotsey wrote:
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

}
}
Oct 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Grant Schenck | last post by:
Hello, This seems like it should be easy... I have a listbox on a .NET form. I add a new item to the list box. How can I associated a separate data value?
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
0
by: David J | last post by:
Hi, I am strugling with the propertygrid and a listbox. I am using the universaldropdowneditor from the codeproject (code below). However I am populating the listbox via a datasource. The problem...
4
by: Marcos Beccar Varela | GamaCom Argentina | last post by:
Hello Everyone. I need to use a gridview, that when I use the Update command one of the colums, instead of showing a textbox where the user can write, I need a combobox (listobx), where there are...
3
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) dtlist1.Items.Clear() etc..
6
by: Paul | last post by:
Hi All, Framework 1.1 listbox control unable to DataBind I've been googling for an answer to this query that appears quite a lot, but none, it seem, answers my problem directly. I am...
3
by: perrycheung221 | last post by:
Hi guys, I got 2 listboxes in a window form, one on left and one on right. The 1st listbox have some items while the 2nd listbox is empty. Also there are 2 buttons between the 2 listboxes which...
1
by: stimul8d | last post by:
okay; ASP. I have i listbox inside a user control which is dynamically created on page_init. I check for postback and only populate the datasource if it's false. regardless, i do this ...
5
by: lukasmazur | last post by:
Hi I have a problem with using listBox1. I have a two forms form1 and form2. In form1 are controls listBox1, textBox1 and button witch creating object of class Form2. In class Form2 I create a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.