Develop Together11
Develop Together -11 ( (ModuleName)EditForm View Code)
Here, let’s show the variables we will define above in order, what do we need? : one CustomerEntity, one CustomerDAL and one Context object. Let’s define these :
private IBaseEntity _entity;//we define the entity to which we will transfer the next entity to the form.
private CustomerDAL customerDAL = new CustomerDAL(); // we call the CustomerDAL by renewing.
private SaleContext context = new SaleContext(); // We create and import the context to the context variable.
public CustomerEditForm(Customer entity) //Here, the form will be opened and we will get an external data named entity in Customer type.
InitializeComponent(); //Standart
DataLayaoutControl = LayoutControl; // we transfer the LayoutControl object to the variable named DataLayoutControl which we defined in BaseEditForm..
_entity = entity;//we transfer the value from the constraction to the global variable.
DataLoad(); //we load the functions of the buttons on page.
txtCustomerCode.DataBindings.Add("Text", _entity, "CustomerCode", false, DataSourceUpdateMode.OnPropertyChanged); // Here we transfer the values of the database to all our variables. For example, we link the customerCode column with txtCustomerCode.DataBindings property . We transfer the value from _entity and the corresponding CustomerCode in the database.
foreach (var item in LayoutControl.Controls) //we’re walking through LayoutControl’s controllers.
{
switch (item)//The next controller is transferred to the item.
{
case PositiveButton btn://If the next value is a button, we will create events.
btn.ButtonClick += Btn_ButtonClick; //We create Events to see that the button is clicked.
btn.DoubleClick += Btn_DoubleClick; // In the same way, when we make a single click or double click, we want to have the same events and we create an event for double_Click again.
break;
}
Let’s fill the events we create now.
}
private void Btn_DoubleClick(object sender, EventArgs e)
{
Selections(sender); //We call the function we defined in global and transfer the values of the clicked button..
}
private void Btn_ButtonClick(object sender, ButtonPressedEventArgs e)
{
Selections(sender); //We call the function we defined in global here and transfer the values of the clicked button..
}
Now let’s write the codes of the function folder named Selections.
private void Selections(object sender)
{
if (!(sender is ButtonEdit)) return; // If the next sender is not buttonEdit, continue to running the following codes.
if (sender == btnCustomerCustomCode1) //If the sender is btnCustomerCustomCode1 , run the following codes.
{
DefinitionListForm frm = new DefinitionListForm(DefinitionEnums.CustomerCustomCode1); // Here we open the form called Definition and call the Enum named DefinitionEnums and write CustomerCustomCode1 in it. We will be able to see automatically added ones while registering on the related form and then listing to a different user.
frm.DataLoad(); // we load the data of the form that it will be opened.
frm.ShowDialog(); // we open as ShowDialog.
if (frm.selected) // If the value selected in BaseEditForm is true, it means that something is selected.
btnCustomerCustomCode1.Text = frm.entity.DefinitionName; // we transfer the selected value to the Text property of the button.
}
else if (sender == btnCustomerCustomCode2) //If it is btnCustomerCustomCode2, run the following codes..
{
DefinitionListForm frm = new DefinitionListForm(DefinitionEnums.CustomerCustomCode2);
frm.DataLoad();
frm.ShowDialog();
if (frm.selected)
btnCustomerCustomCode2.Text = frm.entity.DefinitionName;
}
Finally, let’s save the changes…
protected override void DatabaseAddOrUpdate() //This is the function file that we have defined in BaseEditForm and we will call by overriding.
{
if (customerDAL.AddOrUpdate(context, (Customer)_entity)) // If there are no errors as a result of these values that we send to CustomerDAL.AddOrUpdate, these errors will return to”true” . What are these errors?…Mapping Validation,which we defined earlier.. etc.
{
customerDAL.Save(context); //we save the data.
this.Close(); //We are done in our related form, we close here.
}
}
It is very simple to add a new module :) Now, You have a very high quality infrastructure…Good Luck :)
}