进入USERCORTROL的课程(C#Winforms)
我编辑我的问题fo显示详细信息。现在它编码了,但我不会知道,可能是我完成了不正确的体系结构吗?
主要形式:
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_ADDDDEVICOCONTROL包含其他UserControls(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;
}
}
*/
}
您的意思是在此代码中正确/修改? 我尝试编写清晰的代码,并需要帮助。 谢谢!
I edit my question fo show details. Now it code work, but I wont know, may be I done incorrect architecture?
Main form:
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
}
}
}
User control USC_AddDeviceControl contain other UserControls (DeviceInitControl, SerialPortInitControl and 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;
}
}
*/
}
What you mean correct/modify in this code?
I trying write clear code and need a help for it.
Thank`s!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的USERCONTROL没有BO类实例。您只需在SerialPortinItControl中定义BO类,因此,当您创建它的Instanse时,它将是这样的:
因此,您创建了BO类的对象,但是当您将其存储在UserConrol中时?
因此,这将有效:
您也可以在没有USERCORTROL类的情况下创建类的实例:
Your UserControl hasn't instance of BO class. You just define BO class inside SerialPortInitControl, so when you will create instanse of it, it will be like this:
so, you created object of BO class, but when you stored it in UserConrol?
So, this will works:
Also you can create instances of you classes without UserControl class: