Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Listbox selected Item values being populated without the event being fired.

Posted on 2007-11-30
11
Medium Priority
?
171 Views
Last Modified: 2010-04-15
I have  a listbox whose list is  being populated through a datasource ...That list contains names of some products.when the list is being populated control goes to the first name on the list ....and without even selecting the first name its popping up the details of that name. What should I do so that the control does not directly go to the first name on the list  and select its details?
Only when the user selects the item it should pop up otherwise the information should not be displayed.

Thanks!

Here goes my sample code





//Populating List Box with productNames
        private List<UserData.ProductsRegistered> proreg;
 
        private void ProductsRegistered(string UserName, string productID,string Name)
        {
            proreg =userdata.GetRegisteredProducts(UserName, productID,Name);
 
            listBox1.DataSource = proreg;
            listBox1.DisplayMember = Name;
            listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
                                         
        }
 
        //selecting individual items in a list box
 
 
        private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            // Get the currently selected item in the ListBox.
 
            string Name = "";
            // Name= listBox1.SelectedItem.ToString();
            // string Name = listBox1.SelectedItem.ToString();
            string UserId = "";
            string SerialNumber = "";
            string UserName = "";
            string ProductId = "";
 
 
            //populate the product Name and load the listbox with all values
            if (null != listBox1.SelectedItem)
            {
 
                Name = listBox1.SelectedItem.ToString();
 
                UserData.Name info = userdata.GetInfoName(UserName, Name);
                ProductId = info.ProductId;
 
                UserData.UID ui = userdata.GetUserID(ProductId);
                UserId = ui.UserId;
 
                UserData.LicenseInfo LInfo = userdata.GetNameInfo(UserId, ProductId, Name);
                SerialNumber = LInfo.SerialNumber;
 
                int _count = listBox1.Items.Count;
                if (_count != 0)
                {
                    for (int i = 0; i < _count; i++)
                    {
                        if ((listBox1.GetSelected(i)))
                        {
                            MessageBox.Show("Serial No:= " + SerialNumber.ToString() + "UserID:= " + UserId.ToString(), "LicenseInfo");
 
                        }
                    }
                }
            }
            //If listbox values to be cleared.
            else
            {
                listBox1.Items.Clear();
            }
        }

Open in new window

0
Comment
Question by:onebite2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 20384322
listBox1.SelectedIndex = -1
0
 

Author Comment

by:onebite2
ID: 20384478
Where should I put this???????

Thanks!
0
 

Author Comment

by:onebite2
ID: 20384487
I mean where should this line of code be embedded in my code ?can you please tell me?

Thanks in advance!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:p_davis
ID: 20384491
if you want it to blank on the form load event then you would want to  place it at the end of the initialization of all of your controls and the binding of your datasource to the list
0
 
LVL 22

Expert Comment

by:p_davis
ID: 20384511
i don't know where your productsregistered function is called from but you should be able to put it


private void ProductsRegistered(string UserName, string productID,string Name)
        {
            proreg =userdata.GetRegisteredProducts(UserName, productID,Name);
 
            listBox1.DataSource = proreg;
            listBox1.DisplayMember = Name;
            listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
 
//HERE
listBox1.SelectedIndex = -1;
                                         
        }

Open in new window

0
 

Author Comment

by:onebite2
ID: 20384565
When I put the listBox1.SelectedIndex = -1; at the place where u pointed out it throws me an exception when I want to clear the values in the list box.........

Products Registered function is initialised with the list to poulate values and called down as another function.......
private List<UserData.ProductsRegistered> proreg;
 
        private void ProductsRegistered(string UserName, string productID,string Name)
        {
            proreg =userdata.GetRegisteredProducts(UserName, productID,Name);
 
            listBox1.DataSource = proreg;
            listBox1.DisplayMember = Name;
            listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
                                         
        }

Help to find a work around !

 
0
 

Author Comment

by:onebite2
ID: 20384602
I got the work around to do it ...thanks a lot!I appreciate!
0
 
LVL 22

Expert Comment

by:p_davis
ID: 20384627
i don't completely understand the clear list items if your selected item is null but this will account for the -1 index
 else
{
   if(listBox1.SelectedIndex > -1)
    listBox1.Items.Clear();
}

Open in new window

0
 
LVL 22

Expert Comment

by:p_davis
ID: 20384633
oh -- didn't see the refresh.--

good, and you're welcome
0
 

Author Comment

by:onebite2
ID: 20385078
I am sorry the problem is still existing................I thought it worked around but no....its still there...

See i have a datagridview when I select on one row listbox values are populated and this listbox1.selectedindec=-1 worked but if I select the next row....its not working again its getting higlighted...............

Please help!
0
 
LVL 22

Accepted Solution

by:
p_davis earned 1500 total points
ID: 20387686
there should be a row focused event for the datagridview that you can tie in to and reset the selected index = -1 each time a new row focused. i don't have vs in front of me right now so i cant tell you the exact event off the top of my head.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

604 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