Home > Mobile >  Passing through current form instance to another Class
Passing through current form instance to another Class

Time:09-21

I have come across an issue i'm unable to solve, i'm trying to pass through to a class an instance of the form this.

My code:

namespace newapplication
{
    public partial class FormBrowser : Form
    {
        public ChromiumWebBrowser browser;
        private ChromeDriver _driver;
        private readonly FormMain _formMain;
        public static ClassProject _project;
        public static string _mode;
        private readonly string[] _sites;
        private static int counter = 0;
        private static int sitesCount = 0;
        public bool useSelenium = false;
        public bool useDebugJS = false;

        public FormBrowser(string[] sites, string mode, FormMain formMain, ClassProject project)
        {
            InitializeComponent();
            InitializeBrowser(sites, project);

            _formMain = formMain;
            _mode = mode;
            _sites = sites;
            _project = project;
        }

        private FormBrowser _formBrowser;
        public class MyCustomMenuHandler : IContextMenuHandler
        {
            public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
            {
                // Remove any existent option using the Clear method of the model.
                model.Clear();

                if (model.Count > 0)
                {
                    model.AddSeparator();
                }

                model.AddItem((CefMenuCommand)26508, "Copy text ...");
                model.AddItem((CefMenuCommand)26509, "Paste text ...");
                model.AddSeparator();
                model.AddItem((CefMenuCommand)26501, "Open chrome devtools ...");
                model.AddItem((CefMenuCommand)26502, "Open email verifier ...");
                model.AddSeparator();
                model.AddItem((CefMenuCommand)26503, "Copy current URL ...");
                model.AddSeparator();
                model.AddItem((CefMenuCommand)26504, "Save selected text to [REG] flags ...");
                model.AddItem((CefMenuCommand)26505, "Save selected text to [LAP] flags ...");
                model.AddItem((CefMenuCommand)26506, "Save selected text to email link flags ...");
                model.AddItem((CefMenuCommand)26507, "Save selected text to email link ignore flags ...");
            }

            public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
            {
                if (commandId == (CefMenuCommand)26501)
                {
                    browser.GetHost().ShowDevTools();
                    return true;
                }

                if (commandId == (CefMenuCommand)26502)
                {
                    FormEmailsActivation fea = new FormEmailsActivation(_formBrowser);
                    fea.Show();
                    return true;
                }

                if (commandId == (CefMenuCommand)26503)
                {
                    if (_mode == "MODE_LAP")
                    {
                        UpdatePostedLink(_project, _formBrowser);
                    }
                    else

I'm trying to pass through an instance of the current form:

private FormBrowser _formBrowser;

to this class:

public class MyCustomMenuHandler : IContextMenuHandler

I thought i could do:

public class MyCustomMenuHandler : IContextMenuHandler(_formBrowser)

But this is just showing a lot of errors I cannot even list (it was just an attempt to try something)

I am totally stumped on how to get the form instance through so it's value won't be null.

The answer is probably staring me in the face, any tips on this would be appreciated.

CodePudding user response:

Your "thought I could do" is close... You just need to overload the class you are trying to create so it accepts a PARAMETER of the incoming form you are trying to pass it.

public class MyCustomMenuHandler : IContextMenuHandler
{

   private FormBrowser _formBrowser;

   public MyCustomMenuHandler()
   {
      // whatever code if no form parameter
   }

   public MyCustomerMenuHandler(FormBrowser incomingForm )
   {
      // Now, you have the form to do whatever you need to.
      _formBrowser = incomingForm;
   }
}

Then, just call with the parameter

var mnuHndlr = new MyCustomMenuHandler( theOriginalFormBrowserObjectVariable );

And in your case, yes, you CAN pass in with "this"

var mnuHndlr = new MyCustomMenuHandler( this );

CodePudding user response:

It is pretty simple. Follow this skeleton code:

public class FormBrowser : Form
{
    MyCustomMenuHandler menuHandler;

    void InitializeComponent()
    {
        menuHandler = new MyCustomMenuHandler(this);
    }
}

public class MyCustomMenuHandler : IContextMenuHandler
{
    public MyCustomMenuHandler(FormBrowser formBrwoser)
    {
        Browser = formBrowser;
    }

    public FormBrowser Browser { get; }
}

All information needed to construct a MyCustomMenuHandler should be supplied on its constructor. Use the this keyword to get a reference to the current form in any method of FormBrowser.

  • Related