WebBrowser控件和Windows窗体之间的互动 [英] Interaction between WebBrowser Control and Windows Forms

查看:202
本文介绍了WebBrowser控件和Windows窗体之间的互动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我管理各种联络信息 - 电话,地址,电子邮件,即时消息,其他的,我希望它好看,节约房地产,所以我想在WebBrowser控件来显示。我可以创建标记和内容动态成流,并用颜色和字体大小的任何格式轻松调节显示。我也可以把按钮<输入> 的添加,编辑和删除。我喜欢这种方法,因为它似乎更容易比一个RichTextBox更好看(如果你认为否则指正。)结果
中的问题是关于应对这些按钮。如果一个人被选中,我想隐藏的web浏览器,并与取消隐藏,使现有的新联系人或编辑条目所需的TextBox控件面板。我听说这是可以做到。我希望的建议。所有我能想到的是一些代码来拿起一个AJAX调用,这将提高Windows事件,但似乎......奇怪。结果
任何想法,链接或建议,将不胜感激..甚至一个很好的理由不这样做,但似乎对高质量信息呈现一个好主意,我已经生成大量动态的HTML。

I am managing various contact information - phone, address, email, IM, other and I want it to look good and save real estate, so I want to display it in a WebBrowser control. I can create the markup and content dynamically into a stream and display it with any format with colors and font size easily adjusted. I can also put buttons <input> for Add, Edit, and Delete. I like this method, because it seems easier and better looking than a RichTextBox (correct me if you think otherwise.)
The question is about responding to those buttons. If one is selected, I want to hide the WebBrowser and unhide a Panel with the TextBox controls needed to allow entry of a new contact or editing of an existing one. I've heard this can be done. I was hoping for suggestions. All I can think of is some code to pick up an AJAX call, that would raise a Windows event, but that seems... odd.
Any ideas, links or suggestions would be appreciated .. or even a good reason why not to do it, but it seems like a good idea for high quality information presentation and I've generated plenty of html dynamically.

推荐答案

您可以操纵表格控制 web浏览器使用JavaScript,你也可以操纵 web浏览器控制内容或呼叫C#JavaScript方法。

You can manipulate the Form and Controls or call C# methods from WebBrowser using JavaScript and also you can manipulate content of WebBrowser control or call JavaScript methods from C#.

要操纵你的表格 web浏览器控制和调用C#方法和访问窗体属性,你应该装点您的形式的 [ComVisibleAttribute(真)] 那么你可以设置形式的 ObjectForScripting web浏览器控制的财产。

To manipulate your Form from WebBrowser control and call C# methods and access your form properties you should decorate your form with [ComVisibleAttribute(true)] then you can set the form as ObjectForScripting property of WebBrowser control.

[ComVisibleAttribute(true)]
public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        this.webBrowser1.ObjectForScripting = this;
    }
}



然后,你可以简单地调用方法和访问元素你的窗户形成是这样的:

Then you can simply call methods and access to elements of your windows form this way:

从JavaScript 拨打C#方法:

Call C# method from JavaScript:

window.external.SomeCSMethod('Method called from JavaScript');

设置一个WinForms控制在JavaScript中的值:

请您表格 textBox1的控件将其值设置为public的修改属性公共使用desginer。然后,它可以从JavaScript访问:

Make the textBox1 control on your Form to be public by setting the value of Modifier property to public using desginer. Then it can be accessible from JavaScript:

window.external.textBox1.Text='Value set from JavaScript';



从WinForms的操作HTML



您可以操作C#代码的Web浏览器控制的HTML内容,并调用JavaScript方法或使用的 文件 web浏览器的特性控件:

从C#调用JavaScript方法:

this.webBrowser1.Document.InvokeScript("someJSMethod", new []{"Method called from C#"});



设置HTML控件从C#值:

this.webBrowser1.Document.GetElementById("text1")
                         .SetAttribute("Value set from C#", "Value From C#");



示例代码:

您可以创建一个 Form1中类,并把按钮1 按钮2 textBox1的 webBrowser1 表格设置 MODIFER textBox1的的公开:

You can create a Form1 class and put button1 and button2 and textBox1 and webBrowser1 on your Form set the Modifer of textBox1 to public:

[ComVisibleAttribute(true)]
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Load += Form1_Load;
        button1.Click += button1_Click;
        button2.Click += button2_Click;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.webBrowser1.DocumentText =
        @"<html>
        <head>
        <title>Test</title>
        <script>
            function someJSMethod(value){alert(value);}
        </script>
        </head>
        <body>
            <input type=""text"" id=""text1""/>
            <br/>
            <input type=""button"" value=""Call C# Method"" id=""button1""
            onclick=""window.external.SomeCSMethod('Method called from JavaScript');""/>
            <br/>
            <input type=""button"" value=""Set WinForms Control Value"" id=""button2""
            onclick=""window.external.textBox1.Text='Value set from JavaScript';""/>
        </body>
        </html>";
        this.webBrowser1.ObjectForScripting = this;
    }

    public void SomeCSMethod(string value)
    {
        MessageBox.Show(value);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.webBrowser1.Document
                        .InvokeScript("someJSMethod", new[]{ "Method called from C#" });
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.webBrowser1.Document.GetElementById("text1")
                                 .SetAttribute("value", "Value set from C#");
    }
}

这篇关于WebBrowser控件和Windows窗体之间的互动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆