Browse Sites By Clicking Previous and Next Buttons

Asked

Viewed 374 times

1

I would like to have Webbrowser browse a list of sites by clicking the Previous and Next button to browse the sites.

Print da Form no Visual Studio 2015

  • 1

    I think the question is very generic Sergio, let’s go by parts.. first you know how to load the url for webbrowser? then use the next buttons with the urls array?.. etc

  • Look, I have a notion... webBrowser1.Navigate("www.site.com"); to navigate but I have no idea how to do this xD array business

1 answer

4


Well, here’s a possibility, using only basic things.

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

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

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

int posicaoAtual = 0;

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

  1. On the button Próximo, will be captured the next site of the list, based on what is already being shown to the user (posicaoAtual).

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

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

  2. On the button Anterior, will be captured the previous site, also based on what is already being shown.

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

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

The code will basically look like this

public class Form1: Form
{
    string[] listaSites = new[] { "https://answall.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
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.