How would I call my Web Browser Navigate method in

2019-08-31 09:10发布

I have two forms. My first form is my web browser, the second one is my History form. I would like the user to be able to open the history links in the web browser from my history form. My web browser form has a Navigate method which I use to open pages. I want to use this method in my History form.

Here's my code.

Web Browser Form Navigate Method

 private void navigateURL(string curURL )
    {

        curURL = "http://" + curURL;
       // urlList.Add(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();

    }

How I call my navigate method within my web browser class

  private void homeButton_Click(object sender, EventArgs e)
    {
        GetHomePageURL();
        navigateURL(addressText);
    }

So how would I call this method in my second form (History)??

1条回答
聊天终结者
2楼-- · 2019-08-31 09:47

Two methods that occur to me right off the bat...

  1. Raise a method the web browser form listens to. You'd need to declare the event and raise it in the history form whenever the user selects the history entry they want to navigate to:

    // In the history form, declare event + event-handler delegate
    // Kind of abusing events here, you'd typically have the sender
    // and some sort of eventargs class you'd make...
    public delegate void NavigationRequestedEventHandler(string address);
    public event NavigationRequestedEventHandler NavigationRequested;
    
    // And where you handle the user selecting an history item to navigate to
    // you'd raise the event with the address they want to navigate to
    if (NavigationRequested != null)
        NavigationRequested(address);
    

    Then in the web browser form you'd need to add a handler for that event when you create the history form:

     // Subscribe to event
     this._historyForm = new HistoryForm()
     this._historyForm.NavigationRequested += HistoryForm_NavigationRequested;
    
    // And in the event handler you'd call your navigate method
    private void HistoryForm_NavigationRequested(string address)
    {
      navigateURL(address);
    }
    

    If you'll be creating and throwing away multiple history forms make sure you remove your handler (_historyForm.NavigationRequested -= HistoryForm_NavigationRequested). It's good practice.

  2. Give the history form a reference to the web browser form. When you create the history form, the web browser form would pass it a reference to itself: new HistoryForm(Me)... ideally it'd take an interface that has the Navigate method defined in it. It'd save off that reference and use it to call navigate.

    IWebBrowser WebBrowserForm; // Has the method Navigate(string address)
    
    public HistoryForm(IWebBrowser webBrowser)
    {
        this.WebBrowserForm = webBrowser;
    }
    
    private void homeButton_Click(object sender, EventArgs e)
    {
        GetHomePageURL();
        WebBrowserForm.Navigate(address);
    }
    
查看更多
登录 后发表回答