Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,105 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,695 people online right now. Registration is fast and FREE... Join Now!




ListBox

 
Reply to this topicStart new topic

ListBox

pietra
12 Nov, 2007 - 05:59 AM
Post #1

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
Hello to all smile.gif

I have one question I think it will be very simple to answer to most of you smile.gif

This code on a click of Button1 puts in Label1 what is selected in ListBox1 (sorry if I'm not ussing the correct verbs to describe actions, but I'm sure you can understand me or you can read it from the code).

CODE

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = ListBox1.SelectedItem.Text;
}


When I load the page and select something from the ListBox, everything works fine, but when nothing is selected error page is displayed. Is there another way to solve it or do I need an "if" statement? I tried with "if" but I'm not sure what to evaluate and with what exactly. I tried

CODE

        if (ListBox1.SelectedItem.Selected == false)

and similar variatinos (null), but it's not working.


Error I get in any case is this:
"Exception Details: System.NullReferenceException: Object reference not set to an instance of an object."


Can you please help me out?
Thank you!
User is offlineProfile CardPM
+Quote Post

Jayman
RE: ListBox
12 Nov, 2007 - 10:26 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,922



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Yes, you will need an IF statement. The easiest way is to check if the SelectedIndex is -1. If it is -1 then nothing is selected in the listbox, otherwise something is selected.
CODE

protected void Button1_Click(object sender, EventArgs e)
{
     if(ListBox1.SelectedIndex != -1)
     {
          Label1.Text = ListBox1.SelectedItem.Text;
      }
}

User is online!Profile CardPM
+Quote Post

pietra
RE: ListBox
13 Nov, 2007 - 04:38 AM
Post #3

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
I didn't think of "-1" hehe.gif but I didn't know of "SelectedItem" so, thank you! biggrin.gif

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: ListBox
13 Nov, 2007 - 04:56 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8600
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to ASP.Net Forum smile.gif
User is offlineProfile CardPM
+Quote Post

pietra
RE: ListBox
13 Nov, 2007 - 05:04 AM
Post #5

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
QUOTE(PsychoCoder @ 13 Nov, 2007 - 05:56 AM) *

Moved to ASP.Net Forum smile.gif

After I posted I relized that I posted on the wrong forum, sorry! smile.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 09:13PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month