Club Manager : Create MemberShipPlan : View

MemberShip Plan Manager

Let us create our view for MemberShipPlan. Since we do not have a lots of fields in MemberShipPlan we will be creating, editing and listing all the MemberShipPlan in single view and name the form frmMemberShipPlan.

Add a new form (Add -> Windows Form) in ClubManager Project and name it frmMemberShipPlan.

Add Windows Form
Add Windows Form

As per the discussion on Model MemberShipPlan we require TextBoxes for 6 fields viz Name, Description and 4 to accept fee, we also require a CheckBox to save if Plan is active or not. We also have appropriate labels for all the fields. Since we will be listing all the Plans on this form itself so we require a DataGridView as well.

Let us drag and drop these components on newly created form and set properties as below :

Label : Name = lbl<PropertyName>, Text = <PropertyName>
TextBox : Name = txt<PropertyName>
CheckBox : Name = chk<PropertyName>
DataGridView : Name = dgvMemberShipPlan, Anchor = “Top, Bottom, Left, Right”
GroupBox : Name = grpPlanDetails, Text = MemberShipPlan Details, Anchor = “Top, Left, Right”
txtDescription : MultiLine = true, ScrollBars = Both, WordWrap = False, Anchor = “Top, Left, Right”
frmMemberShipPlan : Text = MemberShipPlan

Also set the Tab orders accordingly and arrange all the fields beautifully.

Tab Order frmMemberShipPlan
Tab Order frmMemberShipPlan

Please note : Anchor property specifies how the components behaves when form is resized. On Resize corresponding distance between form border and control border will remain fixed.

Now here comes the question how are we going to maintain New and Edit on same form? We will be maintaining a private MemberShipPlan variable in the form to track if form is open in new or edit form. So first add reference to project ClubManager.Model and initialize a private MemberShipPlan variable MemberShipPlanBeforeEdit to null.

Next we code click event of btnReset where we check MemberShipPlanBeforeEdit for null and set the form components accordingly.

private void btnReset_Click(object sender, EventArgs e)
{
    if (MemberShipPlanBeforeEdit != null)
    {
        txtMemberShip.Text = MemberShipPlanBeforeEdit.MemberShip.ToString();
        txtDescription.Text = MemberShipPlanBeforeEdit.Description.ToString();
        txtMonthlyFee.Text = MemberShipPlanBeforeEdit.MonthlyFee.ToString();
        txtQuarterlyFee.Text = MemberShipPlanBeforeEdit.QuarterlyFee.ToString();
        txtHalfYearlyFee.Text = MemberShipPlanBeforeEdit.HalfYearlyFee.ToString();
        txtAnnualFee.Text = MemberShipPlanBeforeEdit.AnnualFee.ToString();
        chkIsActive.Checked = MemberShipPlanBeforeEdit.IsActive;
    }
    else
    {
        txtMemberShip.Text =
            txtDescription.Text =
            txtMonthlyFee.Text =
            txtQuarterlyFee.Text =
            txtHalfYearlyFee.Text =
            txtAnnualFee.Text = "";
        chkIsActive.Checked = true;
    }
}

This form can be viewed in New and Edit mode so let us create 2 public methods to handle the same:

public void NewPlan()
{
    MemberShipPlanBeforeEdit = null;
    btnReset_Click(null, null);
    this.Show();
    this.Focus();
}
 
public void EditPlan(MemberShipPlan MemberShipPlan )
{
    MemberShipPlanBeforeEdit = MemberShipPlan;
    btnReset_Click(null, null);
    this.Show();
    this.Focus();
}

Leave a Reply