Click here to Skip to main content
13,044,543 members (60,280 online)
Rate this:
Please Sign up or sign in to vote.
See more:
So I need help fixing this line of code, and I just can't figure it out..

private void btnCheck_Click(object sender, EventArgs e)
            if (lbAccounts.SelectedIndex < 0)
            if (!string.IsNullOrEmpty(txtUser.Text) && !string.IsNullOrEmpty(txtUser.Text))
                return; // stop processing if they have some data
            // otherwise get acccount:password, 
            // split it into acc and pass and write them to TextBoxes
            string value = lbAccounts.Items[lbAccounts.SelectedItem];
            // I am getting the error above
            string[] values = value.Split(':');
            if (values.Length != 2)
                return;  // wrong format        
            var acc = values[0];
            var pass = values[1];
            txtUser.Text = acc;
            txtPass.Text = pass;

If someone can help me do this that would be great... And also point me in the direction of how to make it keep reading and sending the next accounts in line in the listbox to the textbox. Thanks in advance :)
Posted 5-Jan-13 13:41pm
jibesh 5-Jan-13 19:59pm
what error you are getting? you need to mention the error otherwise we cannot able to give a proper solution.
jibesh 5-Jan-13 20:05pm
do not post duplicate questions.
purps 5-Jan-13 20:19pm
The solution below worked, and now I face another problem. After the SelectedItem is checked, I want it to move down the list and keep going until there is no more to check. All help is appreciated
jibesh 5-Jan-13 21:23pm
thats a different problem can you update your question with what you have done so far. you can use 'Improve Question' to update your question. see my comments to the earlier solution.
purps 5-Jan-13 21:58pm
Thank you so much :) I really appreciate it!
jibesh 6-Jan-13 0:19am
You welcome!

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you want to get the selected item from the list the below code is sufficient, hope SelectionMode is one and not multy.
string value = Convert.ToString(lbAccounts.SelectedItem);
purps 5-Jan-13 20:26pm
Here is a screenshot of what I am talking about..
I want it to be so that when the Check button is pressed, all the accounts the the lbAccounts listbox get checked one by one, and there is no need to select the index and click button manually..
jibesh 5-Jan-13 20:36pm
If you want to process all the items in the list dont go for Selected Item, instead iterate through the lbAccount.Items property something like this

foreach(object item in lbAccount.Items)
string listItem = Convert.ToString(item)
//add your logic

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100