Bizarre Exception

P

Peter

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
 
S

ShaneO

Peter said:
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.SelectedIndex))

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

Peter

ShaneO said:
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.SelectedIndex))

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?
 
S

ShaneO

Peter said:
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.SelectedIndex),
ListBoxColorClass).ColorName _
, DirectCast(ListBox1.Items.Item(ListBox1.SelectedIndex),
ListBoxColorClass).HexValue))


I hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
C

Cor Ligthert [MVP]

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
 
P

Peter

ShaneO said:
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?
 
P

Peter

Cor Ligthert said:
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


if BooleanEveryThingIsInitialized then


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

Cor Ligthert [MVP]

No event. Just set the boolean to true.


Peter said:
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...
 
P

Peter

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?
 
C

Cor Ligthert [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top