簡體   English   中英

訪問 UserControl 類(C# WinForms)

[英]Access to class into UserControl (C# WinForms)

我編輯我的問題以顯示詳細信息。 現在它的代碼工作了,但我不知道,可能是我做了不正確的架構?

主要形式:

    public partial class MainForm1 : Form
    {
        public MainForm1()
        {
            InitializeComponent();

            string errorMessage = "";

            DeviceInitControl.BO.BO_GetParams deviceParams;
            SerialPortInitControl.BO.BO_GetParams serialPortParams;
            HttpClientInitControl.BO.BO_GetParams httpClientParams;

            if (USC_AddDeviceControl.deviceInitParams.GetValues(out deviceParams, ref errorMessage))
            {
                //Device init
            }
            else
            {
                errorMessage = "Device initialisation parametrs error! " + errorMessage;
            }
            if (USC_AddDeviceControl.serialPortInitParams.GetValues(out serialPortParams, ref errorMessage))
            {
                //Serial port init
            }
            else
            {
                errorMessage = "Serial port initialisation parametrs error! " + errorMessage;
            }
            if (USC_AddDeviceControl.httpClientInitParams.GetValues(out httpClientParams, ref errorMessage))
            {
                //Http client init
            }
            else
            {
                errorMessage = "Http client initialisation parametrs error! " + errorMessage;
            }

            if (String.IsNullOrEmpty(errorMessage))
            {
                //Next step
            }
            else
            {
                //Show errorMessage to logs
            }
        }
    }

用戶控件 USC_AddDeviceControl 包含其他用戶控件(DeviceInitControl、SerialPortInitControl 和 HttpClientInitControl)。

    public partial class SerialPortInitControl : UserControl
    {
        public class BO
        {
            public enum BO_Language
            {
                RUS,
                EN
            }

            public class BO_Init
            {
                public class CbxItems
                {
                    public string[] names;
                    public string[] baudRates;
                    public string[] parities;
                    public string[] dataBits;
                    public string[] stopBits;
                }

                public CbxItems cbxItems = new CbxItems();
            }

            public class BO_GetParams
            {
                public string name;
                public string baudRate;
                public string paritie;
                public string dataBits;
                public string stopBits;
            }
            /*
            public class BO_SetParams
            {
                //Some controls in UserControlLibrary can contain it class 
            }
            */
        }


        public SerialPortInitControl()
        {
            InitializeComponent();
        }

        public void Init(in BO.BO_Language language, in BO.BO_Init initParams) // This methode is public because I can not initialisaion UserControl in constructor, because constructor can not take a parametrs
        {
            ControlsInit(in initParams);
            SetLanguage(in language);

            void ControlsInit(in BO.BO_Init controlsParams)
            {
                CbxInit(in controlsParams.cbxItems); //Some controls in UserControlLibrary can contain TextBox, DataGridView and other 

                void CbxInit(in BO.BO_Init.CbxItems items)
                {
                    CBX_Name.Items.AddRange(items.names);
                    CBX_BaudRate.Items.AddRange(items.baudRates);
                    CBX_Parity.Items.AddRange(items.parities);
                    CBX_DataBits.Items.AddRange(items.dataBits);
                    CBX_StopBits.Items.AddRange(items.stopBits);
                }
            }
        }

        public void SetLanguage(in BO.BO_Language language)
        {
            switch (language)
            {
                case BO.BO_Language.RUS:
                    {
                        LBL_Name.Text = "Имя порта";
                        LBL_BaudRate.Text = "Скорость";
                        LBL_Parity.Text = "Бит четности";
                        LBL_DataBits.Text = "Бит данных";
                        LBL_StopBits.Text = "Стоп бит";
                    }
                    break;

                case BO.BO_Language.EN:
                    {
                        LBL_Name.Text = "Port name";
                        LBL_BaudRate.Text = "Baud rate";
                        LBL_Parity.Text = "Parity";
                        LBL_DataBits.Text = "Data bits";
                        LBL_StopBits.Text = "Stop bits";
                    }
                    break;

                default:
                    {
                        throw new Exception("Control language is not define!");
                    }
                    break;
            }
        }

        public bool GetParams(out BO.BO_GetParams values, ref string errorMessage)
        {
            if (CheckGetParams(out values, ref errorMessage))
            {
                values = new BO.BO_GetParams()
                {
                    name = CBX_Name.Text,
                    baudRate = CBX_BaudRate.Text,
                    paritie = CBX_Parity.Text,
                    dataBits = CBX_DataBits.Text,
                    stopBits = CBX_StopBits.Text,
                };

                return true;
            }
            else
            {
                values = null;
                errorMessage = "Checking failed! " + errorMessage;
                return false;
            }

            bool CheckGetParams(out BO.BO_GetParams values, ref string errorMessage)
            {
                values = new BO.BO_GetParams();
                bool valid = true;

                if (string.IsNullOrEmpty(CBX_Name.Text)) { valid = false; errorMessage = "Name field is null or empty! "; }
                if (string.IsNullOrEmpty(CBX_BaudRate.Text)) { valid = false; errorMessage = "BaudRate field is null or empty! "; }
                if (string.IsNullOrEmpty(CBX_Parity.Text)) { valid = false; errorMessage = "Parity field is null or empty! "; }
                if (string.IsNullOrEmpty(CBX_DataBits.Text)) { valid = false; errorMessage = "DataBits field is null or empty! "; }
                if (string.IsNullOrEmpty(CBX_StopBits.Text)) { valid = false; errorMessage = "StopBits field is null or empty! "; }

                return valid;
            }
        }
        /*
        public bool SetParams(in BO.BO_SetParams values, ref string errorMessage)
        {
            if (CheckSetValues(in values, ref errorMessage))
            {
                return true;
            }
            else
            {
                errorMessage = "Checking failed! " + errorMessage;
                return false;
            }

            bool CheckSetParams(in BO.BO_SetParams values, ref string errorMessage)
            {
                errorMessage = "Methode is not defined! ";
                return false;
            }
        }
        */
    }

你的意思是在這段代碼中正確/修改? 我嘗試編寫清晰的代碼並需要幫助。 謝謝!

您的 UserControl 沒有 BO 類的實例。 您只需在 SerialPortInitControl 中定義 BO 類,因此當您創建它的實例時,它將是這樣的:

var instBO = new SerialPortInitControl.BO.BO_GetValues()

所以,您創建了 BO 類的對象,但是當您將它存儲在 UserConrol 中時?

public partial class SerialPortInitControl : UserControl
{
    public class BO
    {
        public class BO_GetValues
        {
            public string name;
            public string baudRate;
            public string paritie;
            public string dataBits;
            public string stopBits;
        }
    }

    //instance of BO class
    public BO BO_Instanse {get;}
    
    //instance of BO.Get_Values class
    public BO.BO_GetValues BO_GetValues_Instanse {get;}

    public SerialPortInitControl()
    {
        InitializeComponent();
        BO_Instanse = new BO();
        BO_GetValues_Instanse = new BO.BO_GetValues();
    }


    public bool GetValues(out BO.BO_GetValues values, ref string errorMessage)
    {
        if(BO_Instanse == null)//need real code here
        { 
            values = new BO.BO_GetValues()
            {
                name = CBX_Name.Text,
                baudRate = CBX_BaudRate.Text,
                paritie = CBX_Parity.Text,
                dataBits = CBX_DataBits.Text,
                stopBits = CBX_StopBits.Text,
            };

            return true;
        }
        else
        {
            values = null;
            errorMessage = "Checking failed! " + errorMessage;
            return false;
        }
    }
}

所以,這將有效:

var control = new SerialPortInitControl();
var bo = control.BO_Instanse;
var bo_values = control.BO_GetValues_Instanse;

您也可以在沒有 UserControl 類的情況下創建類的實例:

 var boInstanse = new SerialPortInitControl.BO();
 var boGetValues_Instanse = new SerialPortInitControl.BO.BO_GetValues();
public class Personmodel
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string NickName { get; set; }
        public int Points { get; set; }
        public int CurrLevel { get; set; }
        public int CurrClass { get; set; }
        public List<AvatarPicture> Avatars { get; set; } = new List<AvatarPicture>();
        public string FullName
        {
            get { return $"{ FirstName } { LastName }"; }

    }
}

    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM