473,804 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Progres sChanged(object sender, ProgressChanged EventArgs e)

{

this.Text = string.Format(" Find Duplicates: {0} - {1}%",mDriveDat a.Drive,
e.ProgressPerce ntage);

toolStripProgre ssBar1.Value = e.ProgressPerce ntage;

List<DuplicateF ileNamefilelist = e.UserState as List<DuplicateF ileName>;

DuplicateFileNa me[] files = filelist.ToArra y();

listBox1.Items. AddRange(files) ;

listBox1.Displa yMember = "FileName";

listBox1.ValueM ember = "ID";

}
Oct 28 '07 #1
4 2823
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_Progres sChanged(object sender, ProgressChanged EventArgs
e)

{

this.Text = string.Format(" Find Duplicates: {0} -
{1}%",mDriveDat a.Drive, e.ProgressPerce ntage);

toolStripProgre ssBar1.Value = e.ProgressPerce ntage;

List<DuplicateF ileNamefilelist = e.UserState as
List<DuplicateF ileName>;

DuplicateFileNa me[] files = filelist.ToArra y();

listBox1.Items. AddRange(files) ;

listBox1.Displa yMember = "FileName";

listBox1.ValueM ember = "ID";

}

Oct 28 '07 #2
Hi,

Make sure DuplicateFilesN ame 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.ToArra y and listbox.AddRang e, you can simply do

listBox1.DataSo urce = filelist;
On Sun, 28 Oct 2007 12:59:04 +0100, Rotsey <ma***********@ RemoveThis.optu snet.com.auwrot e:
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_Progres sChanged(object sender, ProgressChanged EventArgs e)

{

this.Text = string.Format(" Find Duplicates: {0} - {1}%",mDriveDat a.Drive,
e.ProgressPerce ntage);

toolStripProgre ssBar1.Value = e.ProgressPerce ntage;

List<DuplicateF ileNamefilelist = e.UserState as List<DuplicateF ileName>;

DuplicateFileNa me[] files = filelist.ToArra y();

listBox1.Items. AddRange(files) ;

listBox1.Displa yMember = "FileName";

listBox1.ValueM ember = "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.comwro te in message
news:op.t0wvt3f qdj93y5@ubuan.. .
Hi,

Make sure DuplicateFilesN ame 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.ToArra y and listbox.AddRang e, you can simply do

listBox1.DataSo urce = filelist;
On Sun, 28 Oct 2007 12:59:04 +0100, Rotsey
<ma***********@ RemoveThis.optu snet.com.auwrot e:
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_Progres sChanged(object sender, ProgressChanged EventArgs e)

{

this.Text = string.Format(" Find Duplicates: {0} - {1}%",mDriveDat a.Drive,
e.ProgressPerce ntage);

toolStripProgre ssBar1.Value = e.ProgressPerce ntage;

List<DuplicateF ileNamefilelist = e.UserState as List<DuplicateF ileName>;

DuplicateFileNa me[] files = filelist.ToArra y();

listBox1.Items. AddRange(files) ;

listBox1.Displa yMember = "FileName";

listBox1.ValueM ember = "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.Collecti ons.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()
{
InitializeCompo nent();
}

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

listBox1.Displa yMember = "Jabberwock y";
}

private ListBox listBox1;

/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.IContain er 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.Disp ose();
}
base.Dispose(di sposing);
}

#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 InitializeCompo nent()
{
this.listBox1 = new System.Windows. Forms.ListBox() ;
this.SuspendLay out();
//
// listBox1
//
this.listBox1.F ormattingEnable d = true;
this.listBox1.L ocation = new System.Drawing. Point(12, 12);
this.listBox1.N ame = "listBox1";
this.listBox1.S ize = new System.Drawing. Size(120, 95);
this.listBox1.T abIndex = 0;
//
// Form1
//
this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
this.ClientSize = new System.Drawing. Size(251, 238);
this.Controls.A dd(this.listBox 1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);
this.ResumeLayo ut(false);

}

#endregion

}
}
Oct 30 '07 #5

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

Similar topics

6
1582
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
8426
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 I want to do is to allow the user to click on the row that corresponds to the correct address, and have the code behind populate the form's Address1, Address2 etc. controls with the relevant data items. I put the code for this into the...
0
2662
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 I am having is that when I have a value in the propertygird and edit that, I want the listbox to have the selectvalue equal to the value that is being edited. Just to make it clearer: PropgridVal = Germany Datasource=
4
4851
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 options. EG OF GRIDVIEW: USUER AGE carlosg old rominat young gonzalor normal florenciat young
3
2301
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
2344
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 populating a listbox with an array of very simple "Country" objects via a WebService. The Country class contains CountryID,TLDs and Name properties each of which have a getter and setter (I found that properties without
3
5770
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 used to move item from/to the 1st and 2nd listbox My problem here is that after I bind the data to the 1st listbox (from a dataTable, using DisplayMember and ValueMember), and I try to move 1 of the item from this 1st listbox to the 2nd listbox...
1
3443
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 foreach (Product p in manager.Products) { ASP.MasterDetailDropDown productMasterDetail; productMasterDetail = (ASP.MasterDetailDropDown)LoadControl("~/controls/MasterDetailDropDown.ascx"); ...
5
2535
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 pointer to object of class Form1. I don't known how to use method add(), where can I find it. From Form1 I can add value like this this->listBox1- I cant find it. I have textBox1 on Form1 and I can change text in this control like this ...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.