来自另一个类的事件调用

时间:2013-02-10 05:28:06

标签: c# .net winforms browser webbrowser-control

有两种形式和一种类。 一种形式是两个按钮。 1 - 开始运行到页面。 2 - 该按钮打开浏览器窗口以显示页面上发生的操作。在类中,您要订阅事件DocumentCompleted

1 - 带有两个按钮的表单

public partial class MainForm : Form
    {
        public System.Windows.Forms.WebBrowser wb;
        public WebbrowserForm webBrowserForm;
        public MainForm()
        {
            webBrowserForm = new WebbrowserForm();
            this.wb = webBrowserForm.webBrowser1;
            InitializeComponent();
        }

        private void bWebBrowser_Click(object sender, EventArgs e)
        {
            if (!this.webBrowserForm.Visible)
            {
                this.webBrowserForm.Show();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            wb.Navigate("http://www.google.ru");
        }
    }

2 - 使用浏览器构建

public partial class WebbrowserForm : Form
    {

        public WebbrowserForm()
        {
            InitializeComponent();
        }

        private void WebbrowserForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                base.Hide();
                //this.MainForm.bWebBrowser.Text = this.MainForm.bWebBrowser.Text.Replace("Спрятать", "Показать");
            }
        }


    }

3 - 必须在页面加载结束时进行类操作,即webBrowser2.DocumentCompleted

class WebBrowser
    {
        //MainForm mainform;
        public System.Windows.Forms.WebBrowser webBrowser2;
        public WebbrowserForm webBrowserForm;

        public WebBrowser()
        {
            webBrowserForm = new WebbrowserForm();
            webBrowser2 = webBrowserForm.webBrowser1;
            this.webBrowser2 = webBrowserForm.webBrowser1;
            this.webBrowser2.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(webbrowser2_DocumentCompleted);
        }

        void webbrowser2_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            webBrowser2.Navigate("http://www.yandex.ru");

        }

如何订阅课程中的事件,以便以浏览器的形式处理?

0 个答案:

没有答案