Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   random selection from list (http://www.go4expert.com/forums/random-selection-list-t27957/)

james101 6Mar2012 07:18

random selection from list
 
hi there masters of c# code
i bow to your hard work and knowledge

i am a beginner and i would be very gratefull for full explainations and not just code snippets


my target is to create a web browser app in c# which load a difffrent website on each runtime
the string i want to run is posted below
i have about 70 webistes i wish the app to load a diffrent one from the 70 each runtime
the other thing is i want a way to make it load a diffrent user agent also each time i have also a list of about 120 user agents

thank you very much

i will post how far i got below

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate("w w w dot anyweb dot com", "_self", null, "User-Agent: Mozilla/4.0
        }
    }
}


james101 6Mar2012 09:04

Re: random selection from list
 
someone gave me this example could anyone help me fix this into working code?

Code:

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        static List<string> urls;
        static Random random;
 
        public Form1()
        {
            InitializeComponent();
            if(urls ==  null)
            {
                urls = GetWebsiteUrls();  //Need to implement
                random = new Random();
            }
 
            string url = urls[random.Next(urls.count)];
            webBrowser1.Navigate(url, "_self", null, "User-Agent: Mozilla/4.0
        }
    }
}


james101 11Mar2012 10:51

Re: random selection from list
 
Thank for all you help guys , not ven 1 response

cheers

bzforum 28May2012 21:01

Re: random selection from list
 
Quote:

Originally Posted by james101 (Post 93099)
someone gave me this example could anyone help me fix this into working code?

Code:

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        static List<string> urls;
        static Random random;
 
        public Form1()
        {
            InitializeComponent();
            if(urls ==  null)
            {
                urls = GetWebsiteUrls();  //Need to implement
                random = new Random();
            }
 
            string url = urls[random.Next(urls.count)];
            webBrowser1.Navigate(url, "_self", null, "User-Agent: Mozilla/4.0
        }
    }
}


The only mistake i see this the above code is that

Quote:

string url = urls[random.Next(urls.count-1)];
The count has to be Minus 1


All times are GMT +5.5. The time now is 11:37.