473,394 Members | 1,866 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,394 software developers and data experts.

Bizarre Exception

I am having a hard time figuring out exactly why this code is throwing a
System.NullReferenceException when I try to access the SelectedValue
property of the ComboBox. It happens on the line marked with a star (*). It
says "object reference not set to an instance of an object," but I do have
the DisplayMember and ValueMember properties set to a valid property
(actually the same one, which is working on the DisplayMember side of
things). Am I missing something here?

Private Sub PaperColorComboBox_SelectedValueChanged_
(ByVal sender As Object, ByVal e As System.EventArgs)_
Handles PaperColorComboBox.SelectedValueChanged

Dim Selector As ComboBox
Selector = CType(sender, ComboBox)
Try
*MsgBox("SelectedValue Type: " &
Selector.SelectedValue.GetType.ToString)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim xColor As New HexColorConverter()
xColor.HexColor = "E41244"
SwatchBox.BackColor = xColor.Color

End Sub

Apr 5 '07 #1
9 1053
Peter wrote:
I am having a hard time figuring out exactly why this code is throwing a
System.NullReferenceException when I try to access the SelectedValue
property of the ComboBox. It happens on the line marked with a star (*).
It says "object reference not set to an instance of an object," but I do
have the DisplayMember and ValueMember properties set to a valid
property (actually the same one, which is working on the DisplayMember
side of things). Am I missing something here?

Private Sub PaperColorComboBox_SelectedValueChanged_
(ByVal sender As Object, ByVal e As System.EventArgs)_
Handles PaperColorComboBox.SelectedValueChanged

Dim Selector As ComboBox
Selector = CType(sender, ComboBox)
Try
*MsgBox("SelectedValue Type: " &
Selector.SelectedValue.GetType.ToString)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim xColor As New HexColorConverter()
xColor.HexColor = "E41244"
SwatchBox.BackColor = xColor.Color

End Sub
I'm not sure what you're trying to achieve here, but if you're just
after the Selected Item then -

MsgBox("SelectedValue Type: " &
PaperColorComboBox.Items(PaperColorComboBox.Select edIndex))

You can then also do-away with -

Dim Selector As ComboBox
Selector = CType(sender, ComboBox)

Hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Apr 5 '07 #2
"ShaneO" <sp****@optusnet.com.auwrote in message
news:46**********************@news.optusnet.com.au ...
Peter wrote:
>I am having a hard time figuring out exactly why this code is throwing a
System.NullReferenceException when I try to access the SelectedValue
property of the ComboBox. It happens on the line marked with a star (*).
It says "object reference not set to an instance of an object," but I do
have the DisplayMember and ValueMember properties set to a valid property
(actually the same one, which is working on the DisplayMember side of
things). Am I missing something here?

Private Sub PaperColorComboBox_SelectedValueChanged_
(ByVal sender As Object, ByVal e As System.EventArgs)_
Handles PaperColorComboBox.SelectedValueChanged

Dim Selector As ComboBox
Selector = CType(sender, ComboBox)
Try
*MsgBox("SelectedValue Type: " &
Selector.SelectedValue.GetType.ToString)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim xColor As New HexColorConverter()
xColor.HexColor = "E41244"
SwatchBox.BackColor = xColor.Color

End Sub

I'm not sure what you're trying to achieve here, but if you're just after
the Selected Item then -

MsgBox("SelectedValue Type: " &
PaperColorComboBox.Items(PaperColorComboBox.Select edIndex))

You can then also do-away with -

Dim Selector As ComboBox
Selector = CType(sender, ComboBox)
Yes, that works, although here's why I'm doing it the other way: I have the
box displaying the names of a bunch of colors, but I want the SelectedValue
to actually be the hex value of the color, which is stored in another
property of the custom object I created to hold the list items.

Interesting side note: None of the other selection-based properties of this
combobox contain any data either; just SelectedIndex. Could it be because I
have the DropDownStyle set to DropDownList?

Apr 5 '07 #3
Peter wrote:
>
Yes, that works, although here's why I'm doing it the other way: I have
the box displaying the names of a bunch of colors, but I want the
SelectedValue to actually be the hex value of the color, which is stored
in another property of the custom object I created to hold the list items.

Interesting side note: None of the other selection-based properties of
this combobox contain any data either; just SelectedIndex. Could it be
because I have the DropDownStyle set to DropDownList?
Whoa... If I understand correctly, you simply want a way for your User
to click on a List of Named Colors, and for that to return the Hex Value
of the Color - am I right?

Is so, then there's probably a million ways this could be done. I've
just knocked-up the following Class for you, tested it, and it works
fine. (Watch for wrapping) -
Public Class ListBoxColorClass

Private m_sColorName As String
Private m_sColorHexValue As String

Public Sub New(ByVal sColorName As String, ByVal sColorHexValue As
String)
m_sColorName = sColorName
m_sColorHexValue = sColorHexValue
End Sub

Public Overrides Function ToString() As String
Return m_sColorName
End Function

Public Function ColorName() As String
Return m_sColorName
End Function

Public Function HexValue() As String
Return m_sColorHexValue
End Function

End Class
Add a ListBox to your Form, then to add items, all you need to do is -

ListBox1.Items.Add(New ListBoxColorClass("Red", Hex(Color.Red.ToArgb)))
ListBox1.Items.Add(New ListBoxColorClass("Green", Hex(Color.Green.ToArgb)))
ListBox1.Items.Add(New ListBoxColorClass("Blue", Hex(Color.Blue.ToArgb)))
The following will provide the Hex Value when the User clicks on the
Color Name shown in the ListBox -

MsgBox(String.Format("You just Selected: {0}. This has a Hex Value
of {1}" _
, DirectCast(ListBox1.Items.Item(ListBox1.SelectedIn dex),
ListBoxColorClass).ColorName _
, DirectCast(ListBox1.Items.Item(ListBox1.SelectedIn dex),
ListBoxColorClass).HexValue))
I hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Apr 5 '07 #4
Peter,

The combobox has in my idea no bizare exceptions at the selection changed
(and related ones). It has no normal exceptions.

Be aware that as long as the combobox is not set completely it will throw
those, there are lot of methods handled in this newsgroup to prevent those,
one of those is to set a boolean (switch)

Something as

Private Sub PaperColorComboBox_SelectedValueChanged_
(ByVal sender As Object, ByVal e As System.EventArgs)_
Handles PaperColorComboBox.SelectedValueChanged
if BooleanEveryThingIsInitialized then
Dim Selector As ComboBox
Selector = CType(sender, ComboBox)
Try
*MsgBox("SelectedValue Type: " &
Selector.SelectedValue.GetType.ToString)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim xColor As New HexColorConverter()
xColor.HexColor = "E41244"
SwatchBox.BackColor = xColor.Color
End if
End Sub



Apr 5 '07 #5
"ShaneO" <sp****@optusnet.com.auwrote in message
news:46**********************@news.optusnet.com.au ...
Peter wrote:
>>
Yes, that works, although here's why I'm doing it the other way: I have
the box displaying the names of a bunch of colors, but I want the
SelectedValue to actually be the hex value of the color, which is stored
in another property of the custom object I created to hold the list
items.

Interesting side note: None of the other selection-based properties of
this combobox contain any data either; just SelectedIndex. Could it be
because I have the DropDownStyle set to DropDownList?

Whoa... If I understand correctly, you simply want a way for your User to
click on a List of Named Colors, and for that to return the Hex Value of
the Color - am I right?

Is so, then there's probably a million ways this could be done. I've just
knocked-up the following Class for you, tested it, and it works fine.
(Watch for wrapping) -
Public Class ListBoxColorClass

Private m_sColorName As String
Private m_sColorHexValue As String

End Class
Add a ListBox to your Form, then to add items, all you need to do is -

ListBox1.Items.Add(New ListBoxColorClass("Red", Hex(Color.Red.ToArgb)))
ListBox1.Items.Add(New ListBoxColorClass("Green",
Hex(Color.Green.ToArgb)))
ListBox1.Items.Add(New ListBoxColorClass("Blue", Hex(Color.Blue.ToArgb)))
Thank you for your work, Shane. I appreciate it. This is pretty similar to
what I've done so far, except that the names and hex values are coming out
of a DataSet. The problem I was having, though, was that, in the
SelectedValueChanged event, I can get the sender to give me a value for
SelectedIndex, but at the same time it will not return an object for
SelectedValue or even SelectedItem... they are both set to Nothing. So, even
though I may go the route of referencing the properties through
SelectedIndex, as you suggest, I'd still like to know why those other
properties aren't returning the entire object itself. Ideas?

Apr 5 '07 #6
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Peter,

Be aware that as long as the combobox is not set completely it will throw
those, there are lot of methods handled in this newsgroup to prevent
those, one of those is to set a boolean (switch)

Something as
>Private Sub PaperColorComboBox_SelectedValueChanged_
(ByVal sender As Object, ByVal e As System.EventArgs)_
Handles PaperColorComboBox.SelectedValueChanged

if BooleanEveryThingIsInitialized then
> Dim Selector As ComboBox
Selector = CType(sender, ComboBox)
Try
*MsgBox("SelectedValue Type: " &
Selector.SelectedValue.GetType.ToString)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim xColor As New HexColorConverter()
xColor.HexColor = "E41244"
SwatchBox.BackColor = xColor.Color

End if
All right... my next question to you is, what event can I use to tell when
the control is all initialized and ready to go? The Load event happens
before painting, but is the data set at that point? I didn't think it was...

Apr 5 '07 #7
No event. Just set the boolean to true.
"Peter" <st*****@hotmail.comschreef in bericht
news:hC*****************@newsread4.news.pas.earthl ink.net...
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>Peter,

Be aware that as long as the combobox is not set completely it will throw
those, there are lot of methods handled in this newsgroup to prevent
those, one of those is to set a boolean (switch)

Something as
>>Private Sub PaperColorComboBox_SelectedValueChanged_
(ByVal sender As Object, ByVal e As System.EventArgs)_
Handles PaperColorComboBox.SelectedValueChanged

if BooleanEveryThingIsInitialized then
>> Dim Selector As ComboBox
Selector = CType(sender, ComboBox)
Try
*MsgBox("SelectedValue Type: " &
Selector.SelectedValue.GetType.ToString)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim xColor As New HexColorConverter()
xColor.HexColor = "E41244"
SwatchBox.BackColor = xColor.Color

End if

All right... my next question to you is, what event can I use to tell when
the control is all initialized and ready to go? The Load event happens
before painting, but is the data set at that point? I didn't think it
was...

Apr 6 '07 #8
No event. Just set the boolean to true.

Right, but how do I know when it's ok to do so? I mean, how can I tell that
the combobox is initialized and that it is now safe to set the boolean to
true?

Apr 6 '07 #9
Some pseudo

private MyInitializingIsReady as boolean
Load Sub
mycombobox.datasource = "MyTable"
mycombobox.displaymember = .............................
etc...................................
MyInitiializingIsReady = true
End Sub
Private Selected-change
If MyInitializing is Ready then
................
End if
End sub

"Peter" <st*****@hotmail.comschreef in bericht
news:Y0*****************@newsread3.news.pas.earthl ink.net...
>No event. Just set the boolean to true.

Right, but how do I know when it's ok to do so? I mean, how can I tell
that the combobox is initialized and that it is now safe to set the
boolean to true?

Apr 6 '07 #10

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

Similar topics

4
by: Alan Little | last post by:
This is very bizarre. Could someone else have a look at this? Maybe you can see something I'm overlooking. Go here: http://www.newsletters.forbes.com/enews/admin/deliver.php4 U: bugtest P:...
3
by: omission9 | last post by:
Any advice on the following would be much appreciated. I have thrown everything I have at it and am completely stumped. I apologize for the length, I have tried to be as succint as possible. I...
1
by: smith | last post by:
I was recently set to release an app that used very common single instance code and hit the oddest issue. After many hours of full build tests I believe that it is duplicatable. Environment: ...
11
by: Frances Del Rio | last post by:
this is so bizarre, and don't even know if this is right place to ask, but don't know where else: about two days I changed webhosting, changed DNS for my domain, francesdelrio.com, now when I...
8
by: Snis Pilbor | last post by:
First, let me announce that this is very possibly off-topic because malloc is a specific third party accessory to c, etc. I spent about an hour trying to find a more appropriate newsgroup and...
4
by: Stan | last post by:
This code has been working for a long time: try { Server.Transfer ("Order.aspx"); } catch (Exception ex) { /// }
1
by: zb | last post by:
I am writing a C# program that uses MySQL as database and MySQL ODBC 3.51 driver. It works fine all the way except this scenario that needs to be addressed. 1. Get a set of records to process...
7
by: BA | last post by:
Hello, I have a very strange code behavior that I cannot make heads or tails of: I have c# code being executed in BizTalk assemblies which is acting very strangely. In my BizTalk process I...
8
by: =?Utf-8?B?TWFyaw==?= | last post by:
We've got a wierd failure happening on just one machine. One part of our product uses a 3rd party search implementation (dtSearch). DtSearch has a native core (dten600.dll), late-bound, and a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
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...

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.