MyForm_View


Creating the View

Create a new folder in your project called MyForm
Add a Windows Form "MyForm_View" to the MyForm folder.
This will create a partial class that inherits from the System.Forms.Form class.
Display the Toolbox and add two buttons to this form.
Change button1 to have the name "btnOK" with caption "OK".
Change button2 to have the name "btnCancel" with caption "Cancel".
Display the code behind this form.


Add the following code to the MyForm_View.cs file.
Add an Enumeration above the partial class for all the different events.

public enum enEvents_MyForm 
{
    OK_Click,
    Cancel_Click
}

Add an Interface under the enumeration called "IMyForm_View".
This defines which members are required.

public interface IMyForm_View 
{
   int Property_WindowCenterX { get; set; }
   int Property_WindowCenterY { get; set; }
   string Property_Caption { get; set; }

   void Method_Close();
   void Method_Initialize();
}

Replace the Constructor with the following Partial Class declaration.

public partial class MyForm_View : 
   System.Windows.Forms.Form,
   IMyForm_View
{
   private MVP.BS_EventsCollection _eventsCollection = null;
   private bool _fireEvents = false;
   private bool _formClosing = false;
   
   //-----------------------------------------------------------------------
   public MyForm_View()
   {
      InitializeComponent();
      
      this._eventsCollection = new MVP.BS_EventsCollection(this);
      this._fireEvents = true;
      this._formClosing = false;
   }
}

Add the following Helper methods.

//----------------------------------------------------------------------- 
public void Method_RegisterChangeRequestListener<T>(
    string propertyName,
    System.EventHandler<MVP.BS_EventArgs<T>> eventHandler)
{
    this._eventsCollection.Method_AddListener<T>(propertyName, eventHandler);
}
//-----------------------------------------------------------------------
public void Method_UnRegisterChangeRequestListener<T>(
    string propertyName,
    System.EventHandler<MVP.BS_EventArgs<T>> eventHandler)
{
    this._eventsCollection.Method_RemoveListener<T>(propertyName, eventHandler);
}
//-----------------------------------------------------------------------
public void Method_FireChangeRequest(
    string propertyName,
    string requestedValue)
{
   if (this._eventsCollection != null)
   {
       this._eventsCollection.Method_Fire<string>(propertyName, requestedValue);
   }
}
//-----------------------------------------------------------------------
public void Method_Close()
{
   if (this._formClosing == false)
   {
      this._formClosing = true;
   }
   this.Close();
}
//-----------------------------------------------------------------------
public void Method_Initialize()
{
   int center_x = this.Property_WindowCenterX - (int)this.Width / 2;
   int center_y = this.Property_WindowCenterY - (int)this.Height / 2;

   this.Text = this.Property_Caption;

   if ((this.Property_WindowCenterX > 0) && (this.Property_WindowCenterY > 0))
   {
       this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
       this.Location = new System.Drawing.Point(center_x, center_y);
   }
}

Add the following Properties

public int Property_WindowCenterX { get; set; } 
public int Property_WindowCenterY { get; set; }
public string Property_Caption { get; set; }

Add the following two Event Handlers for the two buttons.
Double click on the buttons to automatically generate the corresponding event handlers.
Add the following code to these event handlers.

private void btnOK_Click(object sender, System.EventArgs e) 
{
   if ((this._eventsCollection != null) && (this._fireEvents == true))
   {
       this._eventsCollection.Method_Fire<string>(this.GetType().Name,
           enEvents_MyForm.OK_Click.ToString());
   }
}
//-----------------------------------------------------------------------
private void btnCancel_Click(object sender, System.EventArgs e)
{
   if ((this._eventsCollection != null) && (this._fireEvents == true))
   {
       this._eventsCollection.Method_Fire<string>(this.GetType().Name,
           enEvents_MyForm.Cancel_Click.ToString());
   }
}

Add another event handler for the OnFormClosing event.
Display the form and display the Properties window.
Switch to the list of events for the form.
Find the event "FormClosing" and double click on it to generate the corresponding event handler.
Add the following code to this event handler.

protected override void OnFormClosing(System.Windows.Forms.FormClosingEventArgs e) 
{
   if (this._formClosing == false)
   {
       this._formClosing = true;

       if ((this._eventsCollection != null) && (this._fireEvents == true))
       {
           this._eventsCollection.Method_Fire<string>(this.GetType().Name,
               enEvents_MyForm.Cancel_Click.ToString());
       }
   }
}

© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext