473,796 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

listbox object different from listbox item name

I added an object to a listbox. This object is a complex number and not a
string so it shows "Consolapplicat ion.Complex" as a list item. If I convert
the complex number to a string then it shows a string which is correct but
also store's it in the listbox as a string. I wan't to store it as a complex
number but view it as a string. How do I go about doing that?
Nov 17 '05 #1
6 1978
Hi,

"orahm" <or***@discussi ons.microsoft.c om> wrote in message
news:14******** *************** ***********@mic rosoft.com...
I added an object to a listbox. This object is a complex number and not a
string so it shows "Consolapplicat ion.Complex" as a list item. If I
convert
the complex number to a string then it shows a string which is correct but
also store's it in the listbox as a string. I wan't to store it as a
complex
number but view it as a string. How do I go about doing that?


Instead of converting to a string, override the ToString method in your
class:

public class Complex
{
// ...
public override string ToString()
{
return "string representation" ;
}
}
HTH,
Greetings
Nov 17 '05 #2
orahm,

You can override your ToString method on your Complex class type to
return the string representation that you wish to see. The ListBox calls
ToString on the items that are added to it for the display value of the
item.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"orahm" <or***@discussi ons.microsoft.c om> wrote in message
news:14******** *************** ***********@mic rosoft.com...
I added an object to a listbox. This object is a complex number and not a
string so it shows "Consolapplicat ion.Complex" as a list item. If I
convert
the complex number to a string then it shows a string which is correct but
also store's it in the listbox as a string. I wan't to store it as a
complex
number but view it as a string. How do I go about doing that?

Nov 17 '05 #3
Thanks for the reply. That worked fine. I did exactly what you both said:
Is the list item really a complexNumber object or a string object now?

If its a string object I need to parse it back to a complex number if I want
to reuse the number. If its a complexnumber object I should be able to just
use select the item and use it again to do calculations. e. g. like this

complexNumber = this.memoryList Box.SelectedIte m;

this line gives me an error cannot implicitly convert type object. How would
I get that item back to a variable of my complex type?

public override string ToString()
{
if(Imag > 0)
return string.Format( "{0:F2} + j{1:F2}", Real, Imag);
else
{
double Imag2;
Imag2 = Math.Abs(Imag);
return string.Format(" {0:F2} - j{1:F2}", Real, Imag2);
}
}

Is the list item really a complex number object or a string object now?

"Nicholas Paldino [.NET/C# MVP]" wrote:
orahm,

You can override your ToString method on your Complex class type to
return the string representation that you wish to see. The ListBox calls
ToString on the items that are added to it for the display value of the
item.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"orahm" <or***@discussi ons.microsoft.c om> wrote in message
news:14******** *************** ***********@mic rosoft.com...
I added an object to a listbox. This object is a complex number and not a
string so it shows "Consolapplicat ion.Complex" as a list item. If I
convert
the complex number to a string then it shows a string which is correct but
also store's it in the listbox as a string. I wan't to store it as a
complex
number but view it as a string. How do I go about doing that?


Nov 17 '05 #4
orahm,

The item is a complex number, it just uses the ToString method to get
the display string.

You can cast the result from the SelectedItem property back to a
ComplexNumber instance, and then use that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"orahm" <or***@discussi ons.microsoft.c om> wrote in message
news:2B******** *************** ***********@mic rosoft.com...
Thanks for the reply. That worked fine. I did exactly what you both said:
Is the list item really a complexNumber object or a string object now?

If its a string object I need to parse it back to a complex number if I
want
to reuse the number. If its a complexnumber object I should be able to
just
use select the item and use it again to do calculations. e. g. like this

complexNumber = this.memoryList Box.SelectedIte m;

this line gives me an error cannot implicitly convert type object. How
would
I get that item back to a variable of my complex type?

public override string ToString()
{
if(Imag > 0)
return string.Format( "{0:F2} + j{1:F2}", Real, Imag);
else
{
double Imag2;
Imag2 = Math.Abs(Imag);
return string.Format(" {0:F2} - j{1:F2}", Real, Imag2);
}
}

Is the list item really a complex number object or a string object now?

"Nicholas Paldino [.NET/C# MVP]" wrote:
orahm,

You can override your ToString method on your Complex class type to
return the string representation that you wish to see. The ListBox calls
ToString on the items that are added to it for the display value of the
item.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"orahm" <or***@discussi ons.microsoft.c om> wrote in message
news:14******** *************** ***********@mic rosoft.com...
>I added an object to a listbox. This object is a complex number and not
>a
> string so it shows "Consolapplicat ion.Complex" as a list item. If I
> convert
> the complex number to a string then it shows a string which is correct
> but
> also store's it in the listbox as a string. I wan't to store it as a
> complex
> number but view it as a string. How do I go about doing that?


Nov 17 '05 #5
Nicholas,
Thanks a lot for your info. Now when I cast to a string it works:
string temp = (string)this.Li stBox.SelectedI tem;

but when I cast to a complexNumber it doesn't work.
result = (Complex)this.m emoryListBox.Se lectedItem; //result is a complexNumber
the message is: specified cast is not valid

what am I doing wrong?

"Nicholas Paldino [.NET/C# MVP]" wrote:
orahm,

The item is a complex number, it just uses the ToString method to get
the display string.

You can cast the result from the SelectedItem property back to a
ComplexNumber instance, and then use that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"orahm" <or***@discussi ons.microsoft.c om> wrote in message
news:2B******** *************** ***********@mic rosoft.com...
Thanks for the reply. That worked fine. I did exactly what you both said:
Is the list item really a complexNumber object or a string object now?

If its a string object I need to parse it back to a complex number if I
want
to reuse the number. If its a complexnumber object I should be able to
just
use select the item and use it again to do calculations. e. g. like this

complexNumber = this.memoryList Box.SelectedIte m;

this line gives me an error cannot implicitly convert type object. How
would
I get that item back to a variable of my complex type?

public override string ToString()
{
if(Imag > 0)
return string.Format( "{0:F2} + j{1:F2}", Real, Imag);
else
{
double Imag2;
Imag2 = Math.Abs(Imag);
return string.Format(" {0:F2} - j{1:F2}", Real, Imag2);
}
}

Is the list item really a complex number object or a string object now?

"Nicholas Paldino [.NET/C# MVP]" wrote:
orahm,

You can override your ToString method on your Complex class type to
return the string representation that you wish to see. The ListBox calls
ToString on the items that are added to it for the display value of the
item.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"orahm" <or***@discussi ons.microsoft.c om> wrote in message
news:14******** *************** ***********@mic rosoft.com...
>I added an object to a listbox. This object is a complex number and not
>a
> string so it shows "Consolapplicat ion.Complex" as a list item. If I
> convert
> the complex number to a string then it shows a string which is correct
> but
> also store's it in the listbox as a string. I wan't to store it as a
> complex
> number but view it as a string. How do I go about doing that?


Nov 17 '05 #6
Hi Orahm,

Perhaps check that you are adding the complex number to the list, not
adding complex.ToStrin g(), eg,

this would work, and would be able to be cast back to Complex
memoryListBox.A dd(myCalcObject );

this wouldn't, because the listbox now contains a string, not a Complex
memoryListBox.A dd(myCalcObject .ToString());

If you are still having problems, posting the code where you populate
the memoryListBox will help make it clearer where the problem lies.

Cheers -Mike

Nov 17 '05 #7

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

Similar topics

1
2895
by: Patty O'Dors | last post by:
Hi I have some code to create an ownerdrawn listbox (derived), and when I add an item to it, the bold text of the first item (the title, 'Collections and Maturities') mysteriously seems to get bunched up at the right, i.e. squashed up! any idea why? The main bit of the code is as such // (in progressReporter.cs) protected struct LBRow //a row of the listbox, whether it be the title or a
7
2510
by: Grant Schenck | last post by:
Hello, I have a ListBox control on a form. I add members of a class to the Items collection. They show up and I can select them. The text shown is from my classes ToString override. Now, later on I need to update this item. I find it in the Items collection and update a member used by the ToString member function. However, the text shown for the item in the ListBox does not change.
10
2255
by: yop | last post by:
All When I try to get the text from my listbox I am get an error which is listed below. Any ideas? Thanks Object reference not set to an instance of an object.
1
8424
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...
6
2883
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset a denormalized mirror of the database, but I'm not having much luck getting the selection logic down (I haven't found a 'hook' where I can access the listbox object as an object to set the listitem's selected property before it gets rendered).. ...
4
2461
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I can't access the integer value of the selected item, what do I need to do in order to return the "option value" of the control? Moe !--- returned value of the control
0
2661
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=
2
1799
by: rn5a | last post by:
A ListBox lists all the files & directories existing in a directory on the server. If an item in the ListBox happens to be a directory, then the name of the directory is appended with the text . Similarly, if an item in the ListBox happens to be a file, then the name of the file is appended with the text . Assuming that the ListBox lists 2 directories & 3 files, this is how the ListBox would look like: MyDir1 MyDir2 File1.aspx
5
2855
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that is what the ListBox uses for its display. Correct?
0
9673
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
9525
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
10452
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...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
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...
1
7546
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
5440
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...
1
4115
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
3
2924
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.