BP Forums
Listbox items text and text boxes? - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+---- Thread: Listbox items text and text boxes? (/showthread.php?tid=702)



Listbox items text and text boxes? - Derek275 - 11-20-2012

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?


Re: Listbox items text and text boxes? - Derek275 - 11-20-2012

Also, to clear something up, when it's using .Text.ToString, it says 'Object variable or With block variable not set'


Re: Listbox items text and text boxes? - Snake_eyes - 11-21-2012

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


Re: Listbox items text and text boxes? - brandonio21 - 11-21-2012

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]