Passing parameters in the Form constructor, winforms c#

坚强是说给别人听的谎言 提交于 2020-01-01 11:50:32

问题


I have a following inheritance hierarchy:

Class A : Form
Class B : Class A

Class A needs to be able to accept a parameter so that I can create the instance of Class B like this:

ClassB mynewFrm = new ClassB(param);

How do I define such a constructor in Class A?

thanks!

I am using Winforms in .net 3.5, c#

EDITED: Class A and Class B are defined as forms, using partial classes. So I guess this is turning into a question about partial classes and custom (overriden) constructors.


回答1:


Here is a complete demo sample that demostrates required behaviour.

For the sake of ease your learning, I chose a string type parameter that you adjust to your case.

To test it, create a new Visual Studio *C#* project and fill program.cs with the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Stackoverflow
{

    public class ClassA : Form
    {
        public ClassA()
        {
            InitializeComponent();
        }

        public ClassA(string WindowTitleParameter)
        {
            InitializeComponent();
            this.Text = WindowTitleParameter;
            MessageBox.Show("Hi! I am ClassB constructor and I have 1 argument. Clic OK and look at next windows title");
        }

        private void InitializeComponent() // Usually, this method is located on ClassA.Designer.cs partial class definition
        {
            // ClassA initialization code goes here
        }

    }

    public class ClassB : ClassA
    {
        // The following defition will prevent ClassA's construtor with no arguments from being runned
        public ClassB(string WindowTitleParameter) : base(WindowTitleParameter) 
        {
            InitializeComponent();
            //this.Text = WindowTitleParameter;
            //MessageBox.Show("Hi! I am ClassB constructor and I have 1 argument. Clic OK and look at next windows title");
        }

        private void InitializeComponent() // Usually, this method is located on ClassA.Designer.cs partial class definition
        {
            // ClassB initialization code goes here
        }

    }

    static class Program
    {
        /// <summary> 
        /// The main entry point for the application. 
        /// </summary> 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // If you debug this code using StepInto, you will notice that contructor of ClassA (argumentless)
            // will run prior to contructor of classB (1 argument)

            Application.Run(new ClassB("Look at me!"));
        }
    }
}



回答2:


A constructor for class would look like this.

private Object _object1;

public ClassA(object object1)
{
    _object1 = object1;
}



回答3:


For ClassA your constructor would look like

public ClassA(Object param)
{
    //...
}

and for ClassB it would look like

public ClassB(Object param) : base(param) 
{
    //...
}

where base(param) will actually be calling the ClassA constructor that accepts that parameter.



来源:https://stackoverflow.com/questions/12753947/passing-parameters-in-the-form-constructor-winforms-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!