2013-10-23 114 views
0

我已经为我的网页浏览器写了一个向前和向后导航的方法。其目的是将被隐藏的网站(网址)存储到名为webHistory的列表中。然后我尝试通过这个字符串来循环我的后退和前进导航。但它似乎并不奏效。我已检查并确认列表正在填充。这是我的代码。网络浏览器返回功能不起作用

我的Web浏览器类

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(); 

    } 

当我点击后退按钮,当前页面仍然显示,没有错误given.Where我会出错吗?

测试编辑

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); 
    } 
+0

首先(只是为了确定):你不能依赖内置的WebBrowser,不是吗?第二件事:用WebHistory你只想存储最后访问过的页面并通过点击后退/前进按钮来检索它们?因为你似乎太复杂了。 – varocarbas

+0

@varocarbas是的我不应该使用webBrowser类。是的,这正是我想用webHistory做的事情。 – b0w3rb0w3r

+0

你有一些帮助。 – varocarbas

回答

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)。此代码以更清晰(准确)的方式提供您想要的行为。我没有太多的测试,但原则上应该在任何情况下都能正常工作。无论如何,我的意图是帮助您查看您的方法存在的问题,以便您可以从头开始重做,而不是为您提供盲目使用的工作代码。

+0

因此,我构建了一个快速表单来尝试和测试代码,但是我在addurl方法上得到了nullreferenceexeption错误。具体来说就是在这个“curIndex”中说“对象引用未设置为对象的实例”。可能是什么问题呢? – b0w3rb0w3r

+0

@ user2268970!? (我已经写了这段代码开始后悔......我期待着你有足够的知识)。从逻辑上说,你不复制我的代码,因为这个错误是由一个未定义的变量触发的,正如你所看到的,curIndex是全局定义的...... – varocarbas

+0

@ user2268970打开一个新项目,在其上放置3个按钮(上述名称),在每个按钮上单击一次(将使用上述名称生成3个方法;在每个方法中放入相应的代码);在窗体上单击一次,将创建一个新方法(在其中写入Form1_Load内容);忽略我的代码中的公共Form1()(应该已经在你的代码中自动生成)并且写入类中的其余位(默认情况下称为Form1),其中包括标题列表 webHistory;和int curIndex = -1; – varocarbas