I created UserControl for input serial ports parameters (port name, baud rate e.t.c.):
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;
}
}
public SerialPortInitControl()
{
InitializeComponent();
}
public bool GetValues(out BO.BO_GetValues values, ref string errorMessage)
{
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;
}
}
}
But when it placed into Form I can access only to GetValue methode, but not to BO class.
What is happen, help pease. May be do I wrong architecture?
CodePudding user response:
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:
var instBO = new SerialPortInitControl.BO.BO_GetValues()
so, you created object of BO class, but when you stored it in 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;
}
}
}
So, this will works:
var control = new SerialPortInitControl();
var bo = control.BO_Instanse;
var bo_values = control.BO_GetValues_Instanse;
Also you can create instances of you classes without UserControl class:
var boInstanse = new SerialPortInitControl.BO();
var boGetValues_Instanse = new SerialPortInitControl.BO.BO_GetValues();
CodePudding user response:
I will assume numerous things here… So, you have a custom UserControl
that appears to have five (5) TextBoxes
. Also, each text box is used to hold the data for each property in a BO
object. It is unclear “why” your current code uses an additional BO_GetValues
Class like below… ? …
public class BO
{
public class BO_GetValues
{
public string name;
public string baudRate;
public string paritie;
public string dataBits;
public string stopBits;
}
}
This looks odd and the BO_GetValues
Class appears to be superfluous. I would remove this as it only unnecessarily complicates things. I suggest something super simple like…
public class BO {
public string Name { get; set; }
public string BaudRate { get; set; }
public string Paritie { get; set; }
public string DataBits { get; set; }
public string StopBits { get; set; }
}
Next in the user control code, there is some odd method …
public bool GetValues(out BO.BO_GetValues values, ref string errorMessage) …
… ? … the code in this method is invalid and it “appears” the code is trying to get a BO
object from the text boxes? To help, I suggest a simpler approach to this UserControl
and if I am not understanding this correctly then please let me know and I will do my best to help.
So, let us step back and take a wider look at this user control. It has five text boxes and a single BO
object. We want the user to type into a text box and then have the user controls BO
object’s property “associated” with that text box to also change. In addition, we want it such that the Form
that contains this user control can also access the controls BO
object at ANY time.
Therefore, if we create a new SerialPortInitControl
user control, and add five text boxes named as you have in your current code, then all we need to do is create the BO
object as a property of the new user control. Something like…
public BO ThisControlsBO_Instance;
public SerialPortInitControl() {
InitializeComponent();
// change to default values if they exist
ThisControlsBO_Instance = new BO { Name = "", BaudRate = "", Paritie = "", DataBits = "", StopBits = "" };
}
All we really need to do is make sure that the controls BO
object named ThisControlsBO_Instance
is instantiated and not null
. However, … there is no mechanism for the code to “update” the ThisControlsBO_Instance
when the user types text into one of the controls text boxes.
There are several ways you could approach this. One would be to subscribe to the text boxes TextChanged
event or some other event. However, since this is a “Control” … then it would make sense to “BIND” the text boxes DIRECTLY to our ThisControlsBO_Instance
properties. This is accomplished using the TextBoxes
DataBindings
collection. This will “automatically” update the BO
objects properties when the text boxes change. So, the updated user control code may look something like…
public BO ThisControlsBO_Instance;
public SerialPortInitControl() {
InitializeComponent();
// change to default values if they exists
ThisControlsBO_Instance = new BO { Name = "", BaudRate = "", Paritie = "", DataBits = "", StopBits = "" };
}
private void SerialPortInitControl_Load(object sender, EventArgs e) {
CBX_Name.DataBindings.Add("Text", ThisControlsBO_Instance, "Name", true, DataSourceUpdateMode.OnPropertyChanged);
CBX_BaudRate.DataBindings.Add("Text", ThisControlsBO_Instance, "BaudRate", true, DataSourceUpdateMode.OnPropertyChanged);
CBX_Parity.DataBindings.Add("Text", ThisControlsBO_Instance, "Paritie", true, DataSourceUpdateMode.OnPropertyChanged);
CBX_DataBits.DataBindings.Add("Text", ThisControlsBO_Instance, "DataBits", true, DataSourceUpdateMode.OnPropertyChanged);
CBX_StopBits.DataBindings.Add("Text", ThisControlsBO_Instance, "StopBits", true, DataSourceUpdateMode.OnPropertyChanged);
}
In a big picture, the … SerialPortInitControl : UserControl
… is simply a UI control to hold and display a BO
object to the user.
In addition, through some mechanism (a button click), the Form
the user control is on would be able to get the BO
object (and its properties) from the user control at ANY time. Something like…
serialPortInitControl1.ThisControlsBO_Instance.ToString();
Putting all this together may look something like…
The User Control Code…
public partial class SerialPortInitControl : UserControl {
public BO ThisControlsBO_Instance;
public SerialPortInitControl() {
InitializeComponent();
// change to default values if they exists
ThisControlsBO_Instance = new BO { Name = "", BaudRate = "", Paritie = "", DataBits = "", StopBits = "" };
}
private void SerialPortInitControl_Load(object sender, EventArgs e) {
CBX_Name.DataBindings.Add("Text", ThisControlsBO_Instance, "Name", true, DataSourceUpdateMode.OnPropertyChanged);
CBX_BaudRate.DataBindings.Add("Text", ThisControlsBO_Instance, "BaudRate", true, DataSourceUpdateMode.OnPropertyChanged);
CBX_Parity.DataBindings.Add("Text", ThisControlsBO_Instance, "Paritie", true, DataSourceUpdateMode.OnPropertyChanged);
CBX_DataBits.DataBindings.Add("Text", ThisControlsBO_Instance, "DataBits", true, DataSourceUpdateMode.OnPropertyChanged);
CBX_StopBits.DataBindings.Add("Text", ThisControlsBO_Instance, "StopBits", true, DataSourceUpdateMode.OnPropertyChanged);
}
}
public class BO {
public string Name { get; set; }
public string BaudRate { get; set; }
public string Paritie { get; set; }
public string DataBits { get; set; }
public string StopBits { get; set; }
public bool BO_Is_Valid() {
if (string.IsNullOrEmpty(Name))
return false;
if (string.IsNullOrEmpty(BaudRate))
return false;
if (string.IsNullOrEmpty(Paritie))
return false;
if (string.IsNullOrEmpty(DataBits))
return false;
if (string.IsNullOrEmpty(StopBits))
return false;
// futher checking for invalid values...
// if all checks pass, then this `BO` object is valid
return true;
}
public override string ToString() {
StringBuilder sb = new StringBuilder();
sb.AppendLine("BO.Name: " Name);
sb.AppendLine("BO.BaudRate: " BaudRate);
sb.AppendLine("BO.Paritie: " Paritie);
sb.AppendLine("BO.DataBits: " DataBits);
sb.AppendLine("BO.StopBits" StopBits);
return sb.ToString();
}
}
Then some code in the Form
holding this control to check the controls values may look something like…
private void button1_Click(object sender, EventArgs e) {
MessageBox.Show("UserControl Data: " serialPortInitControl1.ThisControlsBO_Instance.ToString());
}
I added some code to show where you could add additional validation checking as I am confident it will be needed. I hope this makes sense and helps.
CodePudding user response:
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 }"; }
}
}