Browse Sites by Clicking Previous and Next Buttons [closed]

1

I would like to make the WebBrowser browse a list of sites by clicking on the Previous and Next button to navigate through the sites.

    
asked by anonymous 20.04.2017 / 20:33

1 answer

4

Well, here's a chance, using just basic things.

Create an array in the class's scope (from form in this case), all the sites in your "list" of sites will be saved in this array. And create, also in the scope of the class, a variable that will keep save the position of this array that is currently being accessed.

Let's assume that by opening form , webBroser will navigate to the first site in the list, ie index zero > of the array .

string[] listaSites = new[] { "https://pt.stackoverflow.com", "https://stackoverflow.com" };

int posicaoAtual = 0;

Then, in the clicks of the buttons you will validate the following:

  • The Próximo button will capture the next site in the list, based on what is already being shown to the user ( posicaoAtual ).

    a. If there is a next site in the list ( posicaoAtual is less than the last index array, that is array subtracting 1) , it will be accessed.

    b. If there is no next site ( posicaoAtual is exactly the last index of the array ) nothing will happen.

  • The Anterior button will capture the previous site, also based on what is already being displayed.

    a. If there is a previous site in the list (if posicaoAtual greater than 0), it will be accessed.

    b. If there is no previous site (if posicaoAtual is 0), nothing will happen.

  • The code will basically stay like this

    public class Form1: Form
    {
        string[] listaSites = new[] { "https://pt.stackoverflow.com", "https://stackoverflow.com" };
    
        int posicaoAtual = 0;
    
        public Form1()
        {
            InitializeComponents();
        }
    
        private static void btAnterior_Click(object sender, EventArgs e)
        {
            if(posicaoAtual - 1 >= 0)
            {
                posicaoAtual -= 1;
                // (^-) Trocar a posição atual para a anterior
                webBrowser.Navigate(listaSites[posicaoAtual]);
                // (^-) Navegar para o respectivo site
            }
        }
    
        private static void btProximo_Click(object sender, EventArgs e)
        {
            if(posicaoAtual + 1 < listaSites.Length)
            {
                posicaoAtual += 1;
                // (^-) Trocar a posição atual para a próxima
                webBrowser.Navigate(listaSites[posicaoAtual]);
                // (^-) Navegar para o respectivo site
            }
        }
    }
    
        
    20.04.2017 / 21:25