473,785 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ListBox usage question

Hi to every one!!!!

For some time now I'm programing in .NET and I have a question in
using ListBox

I need to databind a collection to a ListBox. My code is something
like this:

using namespace System;
using namespace System::Windows ::Forms;

ref class MyClass
{
public:
MyClass(String^ strName, String^ strValue)
{
this->m_strName = strName;
this->m_strValue = strValue;
}

property String^ Name
{
String^ get()
{
return this->m_strName;
}

void set(String^ value)
{
this->m_strName = value;
}
}

property String^ Value
{
String^ get()
{
return this->m_strValue;
}

void set(String^ value)
{
this->m_strValue = value;
}
}

private:
String^ m_strName;
String^ m_strValue;
};

System::Collect ions::Generic:: List<MyClass^>^ oMyCollection = gcnew
System::Collect ions::Generic:: List<MyClass^>( );

oCollectioon->Add(gcnew MyClass(L"Name1 ", L"Value1"));
oCollectioon->Add(gcnew MyClass(L"Name2 ", L"Value2"));
oCollectioon->Add(gcnew MyClass(L"Name3 ", L"Value3"));
oCollectioon->Add(gcnew MyClass(L"Name4 ", L"Value4"));
oCollectioon->Add(gcnew MyClass(L"Name5 ", L"Value5"));

ListBox^ oListBox = gcnew ListBox();
oListBox->DataSource = oCollectioon;
oListBox-DisplayMember = L"Name";
oListBox->ValueMember = ???;
oListBox->SelectedValueM ember = ????;

My question is:
How can I bind the collection oMyCollection to the ListoxBox
displaying the Name property of my MyClass but the ValueMember and
SelectedValueMe mber are the collection item itself?

Thanks
NAlexVS

Sep 12 '07 #1
3 1664

<ca************ @gmail.comwrote in message
news:11******** **************@ o80g2000hse.goo glegroups.com.. .
Hi to every one!!!!

For some time now I'm programing in .NET and I have a question in
using ListBox

I need to databind a collection to a ListBox. My code is something
like this:

using namespace System;
using namespace System::Windows ::Forms;

ref class MyClass
{
public:
MyClass(String^ strName, String^ strValue)
{
this->m_strName = strName;
this->m_strValue = strValue;
}

property String^ Name
{
String^ get()
{
return this->m_strName;
}

void set(String^ value)
{
this->m_strName = value;
}
}

property String^ Value
{
String^ get()
{
return this->m_strValue;
}

void set(String^ value)
{
this->m_strValue = value;
}
}

private:
String^ m_strName;
String^ m_strValue;
};

System::Collect ions::Generic:: List<MyClass^>^ oMyCollection = gcnew
System::Collect ions::Generic:: List<MyClass^>( );

oCollectioon->Add(gcnew MyClass(L"Name1 ", L"Value1"));
oCollectioon->Add(gcnew MyClass(L"Name2 ", L"Value2"));
oCollectioon->Add(gcnew MyClass(L"Name3 ", L"Value3"));
oCollectioon->Add(gcnew MyClass(L"Name4 ", L"Value4"));
oCollectioon->Add(gcnew MyClass(L"Name5 ", L"Value5"));

ListBox^ oListBox = gcnew ListBox();
oListBox->DataSource = oCollectioon;
oListBox-DisplayMember = L"Name";
oListBox->ValueMember = ???;
oListBox->SelectedValueM ember = ????;

My question is:
How can I bind the collection oMyCollection to the ListoxBox
displaying the Name property of my MyClass but the ValueMember and
SelectedValueMe mber are the collection item itself?
Did you try not changing those properties at all?

Also, you probably want to use a BindingList<Tin stead of List<T>
Sep 12 '07 #2
On Sep 12, 4:42 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
<caracolito1... @gmail.comwrote in message

news:11******** **************@ o80g2000hse.goo glegroups.com.. .
Hi to every one!!!!
For some time now I'm programing in .NET and I have a question in
using ListBox
I need to databind a collection to a ListBox. My code is something
like this:
using namespace System;
using namespace System::Windows ::Forms;
ref class MyClass
{
public:
MyClass(String^ strName, String^ strValue)
{
this->m_strName = strName;
this->m_strValue = strValue;
}
property String^ Name
{
String^ get()
{
return this->m_strName;
}
void set(String^ value)
{
this->m_strName = value;
}
}
property String^ Value
{
String^ get()
{
return this->m_strValue;
}
void set(String^ value)
{
this->m_strValue = value;
}
}
private:
String^ m_strName;
String^ m_strValue;
};
System::Collect ions::Generic:: List<MyClass^>^ oMyCollection = gcnew
System::Collect ions::Generic:: List<MyClass^>( );
oCollectioon->Add(gcnew MyClass(L"Name1 ", L"Value1"));
oCollectioon->Add(gcnew MyClass(L"Name2 ", L"Value2"));
oCollectioon->Add(gcnew MyClass(L"Name3 ", L"Value3"));
oCollectioon->Add(gcnew MyClass(L"Name4 ", L"Value4"));
oCollectioon->Add(gcnew MyClass(L"Name5 ", L"Value5"));
ListBox^ oListBox = gcnew ListBox();
oListBox->DataSource = oCollectioon;
oListBox-DisplayMember = L"Name";
oListBox->ValueMember = ???;
oListBox->SelectedValueM ember = ????;
My question is:
How can I bind the collection oMyCollection to the ListoxBox
displaying the Name property of my MyClass but the ValueMember and
SelectedValueMe mber are the collection item itself?

Did you try not changing those properties at all?

Also, you probably want to use a BindingList<Tin stead of List<T>
Thanks!!!
I'll try your solution using a BindingList

Regards

Sep 12 '07 #3

<ca************ @gmail.comwrote in message
news:11******** **************@ d55g2000hsg.goo glegroups.com.. .
On Sep 12, 4:42 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
><caracolito1.. .@gmail.comwrot e in message

news:11******* *************** @o80g2000hse.go oglegroups.com. ..
Hi to every one!!!!
For some time now I'm programing in .NET and I have a question in
using ListBox
I need to databind a collection to a ListBox. My code is something
like this:
using namespace System;
using namespace System::Windows ::Forms;
ref class MyClass
{
public:
MyClass(String^ strName, String^ strValue)
{
this->m_strName = strName;
this->m_strValue = strValue;
}
property String^ Name
{
String^ get()
{
return this->m_strName;
}
void set(String^ value)
{
this->m_strName = value;
}
}
property String^ Value
{
String^ get()
{
return this->m_strValue;
}
void set(String^ value)
{
this->m_strValue = value;
}
}
private:
String^ m_strName;
String^ m_strValue;
};
System::Collect ions::Generic:: List<MyClass^>^ oMyCollection = gcnew
System::Collect ions::Generic:: List<MyClass^>( );
oCollectioon->Add(gcnew MyClass(L"Name1 ", L"Value1"));
oCollectioon->Add(gcnew MyClass(L"Name2 ", L"Value2"));
oCollectioon->Add(gcnew MyClass(L"Name3 ", L"Value3"));
oCollectioon->Add(gcnew MyClass(L"Name4 ", L"Value4"));
oCollectioon->Add(gcnew MyClass(L"Name5 ", L"Value5"));
ListBox^ oListBox = gcnew ListBox();
oListBox->DataSource = oCollectioon;
oListBox-DisplayMember = L"Name";
oListBox->ValueMember = ???;
oListBox->SelectedValueM ember = ????;
My question is:
How can I bind the collection oMyCollection to the ListoxBox
displaying the Name property of my MyClass but the ValueMember and
SelectedValueMe mber are the collection item itself?

Did you try not changing those properties at all?

Also, you probably want to use a BindingList<Tin stead of List<T>

Thanks!!!
I'll try your solution using a BindingList
BindingList will simply cause the ListBox to automatically reflect new
elements added or removed from the collection.

No matter what collection you use, the SelectedItem property should always
be the actual data object, and not just a single property as
DisplayMember/ValueMember/SelectedValueMe mber are concerned with. So use
SelectedItem instead of SelectedValue.
>
Regards

Sep 12 '07 #4

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

Similar topics

3
2861
by: mkiger | last post by:
Hey Guys. I'm making an asp.net app and I have a question regarding listboxes. I have a list box and I would like some of the items to be bold based on certian criteria. Is this possible? matt
1
1646
by: Danny van Kasteel | last post by:
Hi, I'm trying to make an ownerdrawn ListBox that can display a custom graphic and 2 lines of text per item. I have tried to set the following properties on a standard ListBox control: DrawMode = OwnerDrawFixed ItemHeight = 32
9
1810
by: DFS | last post by:
You might also find other uses ========================================================= Public Function getSelections(selType As String, selSeparator As Boolean, selList As ListBox, selCol As Byte) As String 'BUILD IN CLAUSE FROM ITEMS CHOSEN IN MULTI-SELECT LIST BOX 'ARGUMENTS 'selType = text or number
5
1583
by: Sunny | last post by:
Hi, How I can move to the first value in Listbox. I have a listbox that has some values. When I select any value from it. It opens a div with the info about that value. But when I click on close Button of that div & div goes away. and I select the same value again. The div dont appear. Is there anyway, I can make the listbox value to show the first entry,
0
9480
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,...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10087
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
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
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
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.