none
How to make event in the class inherited from Form RRS feed

  • Question

  • I made a form(FormBase) which have a button(bttnOK). bttnOK is public.

    And I made a class(MyForm) inherited from FormBase.

    I want to make bttnOK_Click event from MyForm but all properties and events of MyForm are disabled.

    What should I do to make it enabled and add bttnOK_Click event?

    Can somebody give me some advice?

    I'm using Visual Studio Pro 2017.

    Thursday, November 7, 2019 2:54 AM

Answers

  • How about adding the handler by code in Form_Load event of MyForm?

    public void MyForm_Load(object sender, EventArgs e)
    {
        bttnOk.Click += new EventHandler(bttnOk_Click);
    }
    
    public void bttnOk_Click(object sender, EventArgs e)
    {
    
    }


    • Edited by cheong00Editor Thursday, November 7, 2019 3:08 AM
    • Marked as answer by Jeff0803 Thursday, November 7, 2019 4:41 PM
    Thursday, November 7, 2019 3:07 AM
    Answerer
  • Actually they are locked and can not be changed. You are better off designing the base form then the project menu select "Export Template", when finished you can select the template from the add new items.

    Partial screen shots from the wizard to add a new template, in this case a form.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jeff0803 Thursday, November 7, 2019 4:41 PM
    Thursday, November 7, 2019 3:23 AM
    Moderator
  • Oh, I get your problem.

    You just need to specify the designer you want to use because the IDE don't know.

    Just add attribute to the declaration of your FormBase class like this:

        [System.Runtime.InteropServices.ComVisibleAttribute(true),
            System.Runtime.InteropServices.ClassInterfaceAttribute(1),
            System.ComponentModel.DesignerAttribute("System.Windows.Forms.Design.FormDocumentDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                typeof(System.ComponentModel.Design.IRootDesigner)),
            System.ComponentModel.DesignerCategory("Form"),
            System.ComponentModel.DefaultEventAttribute("Load"),
            System.ComponentModel.InitializationEventAttribute("Load")] public class FormBase: System.Windows.Forms.Form { }




    • Edited by cheong00Editor Thursday, November 7, 2019 4:26 AM Remove the need to directly reference System.Design.dll, as it could cause trouble on deployment
    • Marked as answer by Jeff0803 Thursday, November 7, 2019 4:41 PM
    Thursday, November 7, 2019 3:49 AM
    Answerer

All replies

  • How about adding the handler by code in Form_Load event of MyForm?

    public void MyForm_Load(object sender, EventArgs e)
    {
        bttnOk.Click += new EventHandler(bttnOk_Click);
    }
    
    public void bttnOk_Click(object sender, EventArgs e)
    {
    
    }


    • Edited by cheong00Editor Thursday, November 7, 2019 3:08 AM
    • Marked as answer by Jeff0803 Thursday, November 7, 2019 4:41 PM
    Thursday, November 7, 2019 3:07 AM
    Answerer
  • Actually they are locked and can not be changed. You are better off designing the base form then the project menu select "Export Template", when finished you can select the template from the add new items.

    Partial screen shots from the wizard to add a new template, in this case a form.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jeff0803 Thursday, November 7, 2019 4:41 PM
    Thursday, November 7, 2019 3:23 AM
    Moderator
  • Oh, I get your problem.

    You just need to specify the designer you want to use because the IDE don't know.

    Just add attribute to the declaration of your FormBase class like this:

        [System.Runtime.InteropServices.ComVisibleAttribute(true),
            System.Runtime.InteropServices.ClassInterfaceAttribute(1),
            System.ComponentModel.DesignerAttribute("System.Windows.Forms.Design.FormDocumentDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                typeof(System.ComponentModel.Design.IRootDesigner)),
            System.ComponentModel.DesignerCategory("Form"),
            System.ComponentModel.DefaultEventAttribute("Load"),
            System.ComponentModel.InitializationEventAttribute("Load")] public class FormBase: System.Windows.Forms.Form { }




    • Edited by cheong00Editor Thursday, November 7, 2019 4:26 AM Remove the need to directly reference System.Design.dll, as it could cause trouble on deployment
    • Marked as answer by Jeff0803 Thursday, November 7, 2019 4:41 PM
    Thursday, November 7, 2019 3:49 AM
    Answerer