-1

Please some body help me with my codes i am getting ArguementOutofRange Exception again & again. Please tell me what am i doing wrong. I tried every loop for this but it always gives ArgueOutofrange Error.

Exception is:-

"InvelidArguement = value of '3' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex"

&

Warning is:

"The result of the exception is always 'true' since a value of type 'int' is never equal to 'null' of type 'int?'"

this is my code:

public partial class Form1 : Form
    {
        public Form1()
        {
        InitializeComponent();
        listBox1.Items.Add("www.google.com");
        listBox1.Items.Add("www.facebook.com");
        listBox1.Items.Add("www.yahoo.com");
        listBox1.SelectedIndex = 0;
        listBox1.DataSource = listBox1.Items;

        }

    private void button1_Click(object sender, EventArgs e)
    {
        var num = listBox1.Items.Count;
        string str = Convert.ToString(num);
        textBox1.Text = str;
        for (int i = 0; i < num; )
        {
            webBrowser1.Navigate(listBox1.SelectedItem.ToString());
            while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
                if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
                {
                    listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
                }
            }
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var num = listBox1.Items.Count;
        string str = Convert.ToString(num);
        textBox1.Text = str;
    }
} 
user1563019
  • 69
  • 1
  • 2
  • 8

1 Answers1

0

First of all, do yourself a favor and look into BackgroundWorker instead of DoEvents. `DoEvents is just wrong.

You really haven't given us much to go on other than dumping a few dozens lines of code in a question and ask people to figure out where the problem is and fix it. Where the exception occurs is common courtesy if you want people to help you.

The issue is probaably at the line that assigns SelectedIndex in button1_Click. If all items are sucessful in the browser, you're going to set the selected index to 3 and the highest valid index is 2.

  • Exception is:- "InvelidArguement = value of '3' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex" & Warning is: "The result of the exception is always 'true' since a value of type 'int' is never equal to 'null' of type 'int?'" – user1563019 Aug 18 '12 at 04:07
  • i am still getting the same Exception & if i am using for (int i = 0; i < num; ) for it then why it's go to index 3. i used while(listbox1.SelectedIndex < num) loop for it put it always go for index 3 – user1563019 Aug 18 '12 at 04:54