Click here to Skip to main content
15,886,872 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
So I need help fixing this line of code, and I just can't figure it out..

C#
private void btnCheck_Click(object sender, EventArgs e)
        {

            if (lbAccounts.SelectedIndex < 0)
                return;

            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;
            SendTestMail();
        }


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
Comments
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.
http://www.codeproject.com/Questions/522381/5bC-23-5dNeedplusHelpplusOnplusAnplusErrorplusInpl
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!

1 solution

If you want to get the selected item from the list the below code is sufficient, hope SelectionMode is one and not multy.
C#
string value = Convert.ToString(lbAccounts.SelectedItem);
 
Share this answer
 
Comments
purps 5-Jan-13 20:26pm    
Here is a screenshot of what I am talking about..
http://puu.sh/1K6y6
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900