Saturday, October 8, 2016

DGUIGHF a UI GUI Helper Framework built for .NET WinForm

DGUIGHF is a UI GUI Helper Framework built for .NET WinForm.


The purpose of DGUIGHF is to simplify the building of desktop .NET applications.
It expose a few methods that helps the developer to write less code to mantain the UI of a desktop application.
The idea is to provide an helper to handle the windows status.
Each form of a "standard" application had a list of elements (a DataGridView) and a component to perform CRUD operations on items (a TabControl).
The main list it is binded to a view BindingSource, the CRUD handler it is binded to the database referenced BindingSource.
Whenever a list object is selected, the database BindingSource it is updated, and so the CRUD handler components.
To use this helper, you have just to extend your System.Windows.Forms.Form to DGUIGHFForm.
The key point is to extend to override the InitializeTabElements method to attach the components of your custom form.
Each CRUD handler it's called a TabElement.
There are a few TabElement available. Two of them are the most important, one to attach the components to the parent list, one to attach a list of items and a child TabElement for that list.
Each TabElement can have child TabElements, that way it will be simple to build up a tree of CRUD handler on a single page.
The image below shows a TabElement tree.


When the user perform an operation on a TabElement, the other tab can be locked, this way, the BindingSource will not change due to other data changing.
The code is provided with a sample that exhibit the main TabElement available.
The best place to learn how to use this framework is the Sample project.
There are Data helper to handle operations of the database.
This helper makes uses of the DGDataModel, a generic C# data access layer built on top of the Entity Framework.
There is also a Language helper that perform internationalization operation on each Form component.
As example, building up a simple CRUD Form will take just a few lines of code.
Given a model for a simple "Tag" element that extends the DGDataModel, the following code will handle all the CRUD operation on that.

public partial class FormTags : DGUIGHFForm
{
 private DGUIGHFSampleModel samplemodel = null;
 private TabElement tabElement_tabTags = new TabElement();

 public FormTags()
 {
  InitializeComponent();

  Initialize(Program.uighfApplication);

  samplemodel = new DGUIGHFSampleModel();
  samplemodel.LanguageHelper.LoadFromFile(Program.uighfApplication.LanguageFilename);
 }

 protected override void InitializeTabElements()
 {
  //set sort on View bindingSource
  vTagsBindingSource.Sort = "name";

  //set Readonly OnSetEditingMode for Controls
  DisableReadonlyCheckOnSetEditingModeControlCollection.Add(typeof(DataGridView));

  //set Main BindingSource
  BindingSourceMain = vTagsBindingSource;
  GetDataSourceMain = GetDataSource_main;

  //set Main TabControl
  TabControlMain = tabControl_main;

  //set Main Panels
  PanelFiltersMain = null;
  PanelListMain = panel_list;
  PanelsExtraMain = null;

  //set tabTags
  tabElement_tabTags = new TabElement()
  {
   TabPageElement = tabPage_tabTags,
   ElementItem = new TabElement.TabElementItem()
   {
    PanelData = panel_tabTags_data,
    PanelActions = panel_tabTags_actions,
    PanelUpdates = panel_tabTags_updates,

    ParentBindingSourceList = vTagsBindingSource,
    GetParentDataSourceList = GetDataSource_main,

    BindingSourceEdit = tagsBindingSource,
    GetDataSourceEdit = GetDataSourceEdit_tabTags,
    AfterSaveAction = AfterSaveAction_tabTags,

    AddButton = button_tabTags_new,
    UpdateButton = button_tabTags_edit,
    RemoveButton = button_tabTags_delete,
    SaveButton = button_tabTags_save,
    CancelButton = button_tabTags_cancel,

    Add = Add_tabTags,
    Update = Update_tabTags,
    Remove = Remove_tabTags
   }
  };

  //set Elements
  TabElements.Add(tabElement_tabTags);
 }

 private void FormTags_Load(object sender, EventArgs e)
 {
  ReloadView();
 }

 private object GetDataSource_main()
 {
  IEnumerable<VTags> vTags =
   samplemodel.Tags.List().Select(
   r => new VTags
   {
    tags_id = r.tags_id,
    name = r.tags_name
   }).ToList();

  return DGDataTableUtils.ToDataTable<VTags>(vTags);
 }

 private object GetDataSourceEdit_tabTags()
 {
  return DGUIGHFData.LoadEntityFromCurrentBindingSource<tags, DGUIGHFSampleModel>(samplemodel.Tags, vTagsBindingSource, new string[] { "tags_id" });
 }

 private void AfterSaveAction_tabTags(object item)
 {
  DGUIGHFData.SetBindingSourcePosition<tags, DGUIGHFSampleModel>(samplemodel.Tags, item, vTagsBindingSource);
 }

 private void Add_tabTags(object item)
 {
  DGUIGHFData.Add<tags, DGUIGHFSampleModel>(samplemodel.Tags, item);
 }

 private void Update_tabTags(object item)
 {
  DGUIGHFData.Update<tags, DGUIGHFSampleModel>(samplemodel.Tags, item);
 }

 private void Remove_tabTags(object item)
 {
  DGUIGHFData.Remove<tags, DGUIGHFSampleModel>(samplemodel.Tags, item);
 }
}

Find the hanlded form below:


Code & Binary

Notes
  • read risk disclaimer
  • excuse my bad english