Solved

Detect if the user has selected an item

Posted on 2004-09-30
2
242 Views
Last Modified: 2010-04-15
Hi team, I am looking for a solution that will detect if the user has <u>not</u> choosen an item from a list box. The code below is what I have come up with and does not seem to provide me with the answer I am looking for.

public void GetSelections(ListBox _LBox) //Comments are same as above
            {
                  StringBuilder sb1 = new StringBuilder("");
                  StringBuilder sb2 = new StringBuilder("");
                  for(int i=0; i < _LBox.Items.Count; i++)
                  {
                        if(_LBox.Items[i].Selected)
                        {
                              sb1.Append(_LBox.Items[i].Text); //Return more than one Location, if selected
                              sb1.Append(","); //split muliple selections with ','.
                              sb2.Append(_LBox.Items[i].Value); //Return more than one Location id.
                              sb2.Append(","); //split multiple selections with ','.
                              if(_LBox.Items[i].Selected)
                              {
                                    _sbLocationIDs = null;
                                    _sbLocationsText = null;
                                    lblLocations.Text = "None Selected";
                              }
                              else
                              {
                                    _sbLocationIDs = sb2;
                                    _sbLocationsText = sb1;
                                    lblLocations.Text = "Selected Locations: " + sb1.ToString(); //Set the label equal to selections.
                              }
                        }
                        
                  }
Cheers

Wayne Atherton
0
Comment
Question by:watherton
2 Comments
 
LVL 5

Accepted Solution

by:
tomasX2 earned 50 total points
ID: 12189907
if( (_LBox.SelectedIndex == -1)
{
  // Then nothing is selected
}
0
 

Author Comment

by:watherton
ID: 12190932
Why do you always miss the simple ones?

Cheers tomasX2.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 29
What namespace do I need to import? 2 26
Graphics 2 27
Add different cell to otherwise similiar row 4 38
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question