Web浏览器后退功能不起作用

时间:2013-10-23 21:59:49

标签: c# list url browser

我已经为我的webbrowser编写了一个前进和后退导航方法。目的是将访问的站点(URL)存储到名为webHistory的列表中。然后我尝试循环遍历此字符串以进行后退和前进导航。然而它似乎不起作用。我检查并确认列表正在填充。这是我的代码。

我的网页浏览器类

  public partial class WebBrowser : Form
{

    public string url;
    public string addressText;
    private homeForm homeForm;
    private List <string> urlList = new List <string> ();
    List<String> webHistory;
    int webHistory_Index;
    bool checkHistory;


    public WebBrowser()
    {
        InitializeComponent();
        webHistory = new List<String>();
        webHistory_Index = 0;
        checkHistory = false;

    }

后退按钮我正在测试的那个

 private void backButton_Click(object sender, EventArgs e)
    {
         String backPage = webHistory.ElementAt(webHistory.Count-1);
         webNavigate(backPage);
    }

按钮导航方法

 private void updateNavigation()
    {
        if (webHistory_Index == 0)
        {
            this.backButton.Enabled = false;
        }
        else
        {
            this.backButton.Enabled = true;
        }

        if (webHistory_Index < webHistory.Count)
        {
            this.forwardButton.Enabled = true;

        }
        else
        {
            this.forwardButton.Enabled = false;
        }
    }
    private void navigatedPages(string urlbartext)
    {

        addressText = urlBar.Text;
        urlbartext = "http://" + addressText;
        webHistory.Add(urlbartext);
        if (!checkHistory)
    {
        if (webHistory_Index < webHistory.Count)
        {
            webHistory.RemoveRange(webHistory_Index, webHistory.Count - webHistory_Index);
        }

        System.Diagnostics.Debug.Print(urlbartext + "   -    " + urlBar.SelectedText);
        webHistory_Index += 1;
        updateNavigation();
    }
    checkHistory = false;
    System.Console.WriteLine(webHistory.Count.ToString());
}

Web浏览器导航方法。

 private void webNavigate(string urlbartext )
    {
        addressText = urlBar.Text;
        urlbartext = "http://" + addressText;
        urlList.Add(urlbartext);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlbartext);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream pageStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(pageStream, Encoding.Default);
        string s = reader.ReadToEnd();
        webDisplay.Text = s;

        reader.Dispose();
        pageStream.Dispose();
        response.Close();

    }

当我点击后退按钮时,仍会显示当前页面并且没有给出错误。我哪里出错了?

测试编辑

 List<String> webHistory;
    int curIndex = -1;
    public Form1()
    {
        InitializeComponent();
        webHistory = new List<string>();
    }

    private void gotoUrl(string curUrl)
    {
        curUrl = "http://" + curUrl;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(curUrl);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream pageStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(pageStream, Encoding.Default);
        string s = reader.ReadToEnd();
        webDisplay.Text = s;
        reader.Dispose();
        pageStream.Dispose();
        response.Close();
    }


    private void addUrl(string curUrl)
    {

        if (webHistory.Count > 0 && webHistory.Count - 1 > curIndex) webHistory.RemoveRange(curIndex, webHistory.Count - curIndex - 1);
        webHistory.Add(curUrl);
        curIndex = webHistory.Count - 1;

        gotoUrl(curUrl);
    }

    private void back_Click(object sender, EventArgs e)
    {
        if (curIndex - 1 >= 0)
        {

            curIndex = curIndex - 1;
            gotoUrl(webHistory[curIndex]);
        }
    }

    private void forward_Click(object sender, EventArgs e)
    {
        if (curIndex + 1 <= webHistory.Count - 1)
        {

            curIndex = curIndex + 1;
            gotoUrl(webHistory[curIndex]);
        }
    }

    private void navigate_Click(object sender, EventArgs e)
    {
        addUrl(urlText.Text);
    }

1 个答案:

答案 0 :(得分:1)

您的代码中存在很多问题。但我认为主要问题是代码的主要结构缺乏明确性。在这里,您有一个小代码执行您想要的主要操作,我希望这将有助于您在不同的场所重做代码:

List<String> webHistory;
int curIndex = -1;
public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    webHistory = new List<string>();
}

private void gotoUrl(string curUrl)
{
    //display the url in the browser
}


private void addUrl(string curUrl)
{
    //Add a new Url
    if (webHistory.Count > 0 && webHistory.Count - 1 > curIndex) webHistory.RemoveRange(curIndex, webHistory.Count - curIndex - 1);
    webHistory.Add(curUrl);
    curIndex = webHistory.Count - 1;

    gotoUrl(curUrl);
}


private void Previous_Click(object sender, EventArgs e)
{
    if (curIndex - 1 >= 0)
    {
        //Previous URL
        curIndex = curIndex - 1;
        gotoUrl(webHistory[curIndex]);
    }
}

private void Next_Click(object sender, EventArgs e)
{
    if (curIndex + 1 <= webHistory.Count - 1)
    {
        //Next URL
        curIndex = curIndex + 1;
        gotoUrl(webHistory[curIndex]);
    }
}

private void Navigate_Click(object sender, EventArgs e)
{
    //Simulate the user input by introducing new URLs
    addUrl("");
}

只需在新表单上放置三个按钮:NavigatePreviousNext(并关联相应的Click Events)。此代码以更清晰(和准确)的方式提供您想要的行为。我没有测试太多,但原则上应该在任何情况下都能正常工作。在任何情况下,我的目的都是帮助您查看您的方法存在的问题,以便您可以从头开始重做,而不是为您提供工作代码以便盲目使用它。