Posts: 39
Threads: 17
Joined: Oct 2012
Reputation:
0
Ok, I have I have a list box, kryptonListBox1, and a text box textBoxX1. I want to get the selected item in the listbox's text into the text box. My code is:
textBoxX1.Text = kryptonListBox1.SelectedItem.ToString
I've tried using .Text.ToString, but with both I get the error 'Object reference not set to instance of an object'
What am I doing wrong and what's the easiest way to fix it?
Posts: 39
Threads: 17
Joined: Oct 2012
Reputation:
0
Also, to clear something up, when it's using .Text.ToString, it says 'Object variable or With block variable not set'
Posts: 31
Threads: 2
Joined: Oct 2012
Reputation:
0
The code you are using should work fine but just in case try with the code below:
[code2=vbnet]TextBox1.Text = KryptonListBox1.SelectedItems.Item(0).ToString[/code2]
Try using break points and trace the error because the problem is not with the code you're using. Also try to use a try/catch block and see if that corects the problem by ignoring it.
For further help please provide more info like what kind off error it is , where exactly are you using the code and so on..
Sssssssssoftware developer...
Posts: 1,006
Threads: 111
Joined: Jul 2010
Reputation:
1
As Snake_eyes suggested (Thanks!), this code should definitely work. The error may be appearing due to the fact that a valid item is not actually being selected. You can try to make sure an item is being selected by checking first, like so:
[code2=vbnet]If Not (kryptonListBox1.SelectedItem Is Nothing) Then
textBoxX1.Text = kryptonListBox1.SelectedItem.ToString
End If[/code2]