Winforms+JavaScript

Quick question for the group. I'm embedding a winforms user control into a web page. This web page is going to be launched from another page in a Modal sense. Once the user is done with the pop-up control webpage I would like the page to close automatically when the finish button is pushed. Is there a way to communicate between the html and the winforms control using javascript so that I don't have to have a two button scenario where the user pushes finish then pushes Ok outside of the control Any help would be appreciated.




Answer this question

Winforms+JavaScript

  • Mark Grinols MSFT

    Have you tried using WinForm parent this should reference the HTMLDocument. Then you can use that to run a JS.

  • Lester Hewett

    Ok, finally got this working as well. In my previous post, I mentioned a msdn article relating to this issue. There are a few lines of COM code that need to be added to the ones already in place in the article. Without these lines the communication never occurs between the control and the script. Here is the msdn article's code with the necessary changes added in red: Hope this helps anybody else out there that might be struggling with the problem.

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    [assembly: ClassInterface(ClassInterfaceType.AutoDual)]

    [assembly: SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode = true)]


    namespace ActiveXSourcing
    {
    public delegate void ClickEventHandler(int x, int y);

    //Source interface for events to be exposed.
    //Add GuidAttribute to the source interface to supply an explicit System.Guid.
    //Add InterfaceTypeAttribute to indicate that interface is IDispatch interface.
    [GuidAttribute("0422D916-C11A-474e-947D-45A107038D12") ]
    [ComVisible(true)]
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ControlEvents

    //Add a DisIdAttribute to any members in the source
    //interface to specify the COM DispId.
    {
    [DispIdAttribute(0x60020000)]
    void ClickEvent(int x, int y);
    }

    //Add a ComSourceInterfaces attribute to the control to
    //identify the list of interfaces that are exposed as COM event sources.

    [ComVisible(true)]

    [ClassInterface(ClassInterfaceType.None)]

     
    [ClassInterface(ClassInterfaceType.None),ComSourceInterfaces(typeof(ControlEvents))]
    public class MyWindowControl : System.Windows.Forms.UserControl
    //, ComInteropControlInterface
    {
    System.Windows.Forms.TextBox tx = new TextBox();

    private void InitializeComponent()
    {

    this.Name = "MyWindowControl";

    }

    event ActiveXSourcing.ClickEventHandler ClickEvent;

    public MyWindowControl() : base()
    {

    initMyWindowControl();

    }

    private void initMyWindowControl()
    {

    Size = new System.Drawing.Size(300, 50);
    tx.Text = "Click on the TextBox to invoke 'ClickEvent'";
    tx.Size = this.Size;
    tx.Click += new System.EventHandler(ClickHandler);
    this.Controls.Add(tx);

    }


    private void ClickHandler(object sender, System.EventArgs e)
    {
    if (ClickEvent != null)
    {
    ClickEvent(0, 0);
    }
    }
    }
    }


  • DevonSprings

    After some digging I came across an MSDN article that talks about this situation. For those who may not have seen it: http://support.microsoft.com/default.aspx kbid=313891
    I've followed every part of that article and I am unable to get it to work. For some reason the event is always equal to null and therefore never has any action taken against it. Has anybody successfully dealth with this and if so how did you do it.

    Eric

  • Duane Ferrell

    I'm afraid that is not possible, because the browser, after it launched the WinForm, can no longer track (or attach an event handler) to the WinForm it shown.

    Regards,

    -chris



  • Winforms+JavaScript