473,473 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Create 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 "Consolapplication.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 1963
Hi,

"orahm" <or***@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
I added an object to a listbox. This object is a complex number and not a
string so it shows "Consolapplication.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.com

"orahm" <or***@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
I added an object to a listbox. This object is a complex number and not a
string so it shows "Consolapplication.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.memoryListBox.SelectedItem;

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.com

"orahm" <or***@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
I added an object to a listbox. This object is a complex number and not a
string so it shows "Consolapplication.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.com

"orahm" <or***@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.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.memoryListBox.SelectedItem;

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.com

"orahm" <or***@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
>I added an object to a listbox. This object is a complex number and not
>a
> string so it shows "Consolapplication.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.ListBox.SelectedItem;

but when I cast to a complexNumber it doesn't work.
result = (Complex)this.memoryListBox.SelectedItem; //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.com

"orahm" <or***@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.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.memoryListBox.SelectedItem;

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.com

"orahm" <or***@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
>I added an object to a listbox. This object is a complex number and not
>a
> string so it shows "Consolapplication.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.ToString(), eg,

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

this wouldn't, because the listbox now contains a string, not a Complex
memoryListBox.Add(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
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...
7
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,...
10
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
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...
6
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...
4
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,...
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...
2
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 ....
5
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...
0
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,...
1
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...
0
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...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
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...

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.