Quince
Quince

Reputation: 188

Clicking on the link inside the Geckofx webbrowser will trigger the method in Winforms

There is an HTML file with named "test.html" and two links in it. This HTML file is shown in geckoWebBrowser1 as follows:

<!DOCTYPE html>
<html>
<head lang="tr">
    <meta charset="UTF-8">
    <title>Test HTML</title>
</head>
<body>
<p><a href="#" id="open_file" class="open-file button" onclick="openFile()">Open A PDF file...</a></p>
<p><a href="#" id="go_to_articles" class="go-to-articles button" onclick="goToArticles()">Go to articles...</a></p>
</body>
<script>
    function openFile()
    {
        // What should I write here?
    }

    function goToArticles()
    {
        // What should I write here?
    }
</script>
</html>

And here is the winforms content:

using System;
using System.Windows.Forms;
using Gecko;

namespace Test
{
    public partial class Frm1 : Form
    {

        public Frm1()
        {
            InitializeComponent();

            Xpcom.Initialize("Firefox64");
        }
        private void Frm1_Load(object sender, EventArgs e)
        {
            FormBorderStyle = FormBorderStyle.None;

            geckoWebBrowser1.Navigate("start\\test.html");
        }

        public void OpenPDFFile()
        {
            var ofd = new OpenFileDialog { Filter = @"PDF |*.pdf", Title = @"Select a PDF file..." };
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                // Here, action will be taken regarding the selected file.
            }
        }
    }
}

When I click the Open A PDF file ... link in the HTML file, the OpenPDFFile method must be triggered in Winforms and selected the PDF file from the dialog box, but I have not been able to do so. Yet I would like to see the "FrmArticles" form located in Winforms by clicking the Go to articles ... link in the HTML file, but I have not been able to achieve it so far.

Upvotes: 0

Views: 376

Answers (1)

Habip Oğuz
Habip Oğuz

Reputation: 1193

It is derived from the answer to the Error in 'AddMessageEventListener' on GeckoFX question.

<!DOCTYPE html>
<html>
<head lang="tr">
    <meta charset="UTF-8">
    <title>Test HTML</title>
</head>
<body>
<p><a href="#" id="open_file" class="open-file button" onclick="fireEvent('openFiles', 'SomeData');">Open A PDF file...</a></p>
<p><a href="#" id="go_to_articles" class="go-to-articles button" onclick="goToArticles()">Go to articles...</a></p>
</body>
<script>
function fireEvent(name, data)
{
    var event = new MessageEvent(name,{'view': window, 'bubbles': false, 'cancelable': true, 'data': data});
    document.dispatchEvent(event);
}
</script>
</html>

Form.cs content,

using System;
using System.Windows.Forms;
using Gecko;

namespace Test
{
    public partial class Frm1 : Form
    {

        public Frm1()
        {
            InitializeComponent();

            Xpcom.Initialize("Firefox64");
        }
        private void Frm1_Load(object sender, EventArgs e)
        {
            FormBorderStyle = FormBorderStyle.None;

            geckoWebBrowser1.Navigate("start\\test.html");
            AddMessageEventListener("openFiles", showMessage);
        }


        public void AddMessageEventListener(string eventName, Action<string> action)
        {
            geckoWebBrowser1.AddMessageEventListener(eventName, action);
        }

        private void showMessage(string s)
        {
            var ofd = new OpenFileDialog { Filter = @"PDF |*.pdf", Title = @"Select a PDF file..." };
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(ofd.FileName);
            }

        }
    }
}

In the example, you can use the string "SomeData" as a parameter if you want.

Upvotes: 1

Related Questions