C # method of calling WinForm through HTML _c# tutorial

Source: Internet
Author: User

The example in this article describes how C # calls WinForm through HTML. Share to everyone for your reference, specific as follows:

Complete test Code:

Form1.cs:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Namespace Test
{
  [System.Runtime.InteropServices.ComVisibleAttribute (true)] public
  partial class Form1:form
  {public
    Form1 ()
    {
      initializecomponent ()
    }
    private void Form1_Load (object sender, EventArgs e)
    {
      System.IO.FileInfo file = new System.IO.FileInfo ( application.startuppath+@ "\test1.htm");
      Webbrowser1.url = new Uri (file. FullName);
      Webbrowser1.objectforscripting = this;
    }
    private void Button1_Click (object sender, EventArgs e)
    {
      object[] objects = new Object[1];
      Objects[0]= "C # access JavaScript script";
      WebBrowser1.Document.InvokeScript ("MessageBox", objects);
    }
    public void Mymessagebox (String message)
    {
      MessageBox.Show (message);
    }
  }
}

Class Winoper:

[System.Runtime.InteropServices.ComVisibleAttribute (True)]
  public class Winoperationclass
  {public
    void MyMessageBox1 ()
    {
      MessageBox.Show (message);
    }
    public void ShowForm ()
    {
      Form2 F2 = new Form2 ();
      F2. WindowState = Formwindowstate.normal;
      F2. Show ();
    }
  

Web page:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
 
 

Add:

Webbrowser1.objectforscripting = this;

The meaning of this sentence is that the WebBrowser1 script executes COM binding method from Form1, and MyMessageBox1 and ShowForm are in the Winoperationclass class, certainly not.

The first one can be because the Form1 inside has mymessagebox this method, you MyMessageBox1 and ShowForm move to Form1 or move Mymessagebox inside Winoperationclass, then

Webbrowser1.objectforscripting = this;

Change this sentence into

Winoperationclass w=new Winoperationclass ();
webbrowser1.objectforscripting = W;

It's okay.

Recommend the second kind of ... Put all the COM visible methods inside a class to maintain

Read more about C # Interested readers can view the site topics: "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # Common control usage Tutorial", "C # object-oriented Program design Introductory Course" and "C # Programming Thread Usage Skills Summary"

I hope this article will help you with C # programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.