Programming     Travel Logs     Life Is Good     Surfing Online     About Me
Give me a lever long enough, and a place to stand, and I will move the earth.
-Archimedes
2018-05-01 19:05:06

Copy this link when reproducing:
http://www.casperlee.com/en/y/blog/47

Now that we know how to use Entity Framework along with the SQLite database, let's start to implement a key function of this system: Import/Add Document.

Before doing that, let's add a login form first.

1. Right click on the main project and select "Add -> Windows Form..." on the popped menu.

/Images/20161122/01.jpg

2. Type in "frmLogin" as the name and click on "Add" button to add a new form.

/Images/20161122/02.jpg

3. Change the following properties of the Ok button.

    Name: btnOk
    DialogResult: OK
    Text: OK

4. Change the following properties of the Cancel button.

    Name: btnCancel
    DialogResult: Cancel
    Text: Cancel

5. Change the following properties of the Password textbox.

    Name: tbPassword
    MaxLength: 24
    PasswordChar: *

6. Double click on the Ok button to add an event handler function.

7. Put the following code into the frmLogin.cs.

using System;
using System.Windows.Forms;

namespace com.casperlee.ResourceManager
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
public string Password
{
get; set;
}
private void btnOK_Click(object sender, EventArgs e)
{
this.Password = tbPassword.Text;
}
}
}

8. Right click on the Bll project and select "Add -> Class..." on the popped menu.

/Images/20161122/03.jpg

9. Type in "CEnvironment" as the name and click on "Add" button to add a new class.

10. Put the following code into the class.

using System;
using System.IO;

namespace com.casperlee.ResourceManager.Bll
{
public class CEnvironment
{
public static string CurrentPassword
{
get;
set;
}
}
}

11. Open the Program.cs in the main project and put the following code into it.

using System;
using System.Windows.Forms;
using com.casperlee.ResourceManager.Bll;

namespace com.casperlee.ResourceManager
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

frmLogin loginForm = new frmLogin();
if (loginForm.ShowDialog() == DialogResult.OK)
{
CEnvironment.CurrentPassword = loginForm.Password;
Application.Run(new MainForm());
}
else
{
Application.Exit();
}
}
}
}

12. Rebuild all project and run the application to see if the login form works or not.

    Hint: the password is allowed to be null. I'll explain it later.

Now, we need a new form to allow user to input related information for the document.

13. Add a new form to the main project and name it "frmAddResource".

/Images/20161122/04.jpg

14. Modify the following properties of the controls on this form.

---The "Type" ComboBox---
    Name: cbType
    DropDownStyle: DropDownList

---The "File" TextBox---------
    Name: tbFile
    ReadOnly: True
    TabStop: False

---The "Name" TextBox-------
    Name: tbName

---The "Password" TextBox----
    Name: tbPassword
    MaxLength: 24

---The "Keywords" TextBox---
    Name: tbKeywords
    Multiline: True

---The "Ok" Button-------------
    Name: btnOk
    Text: OK

---The "Cancel" Button---------
    Name: btnCancel
    Text: Cancel

When the form being loaded, we need to load all ResourceTypes into the drop down list of the "Type" ComboBox, so that user can choose among them later.

15. For convenience, I'm gonna extend the automatically generated ResouceType class to support more functions. Add a new class ResourceTypeEx.cs to the Bll project, and put the following code in.

using com.casperlee.ResourceManager.Dal;

namespace com.casperlee.ResourceManager.Bll
{
public class ResourceTypeEx : ResourceType
{
public void CopyFrom(ResourceType aType)
{
this.ID = aType.ID;
this.Key = aType.Key;
this.Name = aType.Name;
}

public override string ToString()
{
return this.Name;
}
}
}

16. Add a new function to the ResourceTypeBll class.

        public static void QueryAll(List<ResourceTypeEx> aItems)
{
using (ResourceManageEntities rme = new ResourceManageEntities())
{
foreach (ResourceType rt in rme.ResourceTypes)
{
ResourceTypeEx rte = new ResourceTypeEx();
rte.CopyFrom(rt);
aItems.Add(rte);
}
}
}

17. Add an handler function for the Load event of the form, and put the following code in.

        private void frmAddResource_Load(object sender, EventArgs e)
{
InitializeResourceTypes();
tbPassword.Text = CEnvironment.CurrentPassword;
}

18. Add a private instance function to this form, and put the following code in.

        private void InitializeResourceTypes()
{
cbType.Items.Clear();
List<ResourceTypeEx> items = new List<ResourceTypeEx>();
ResourceTypeBll.QueryAll(items);
foreach (ResourceTypeEx rte in items)
{
cbType.Items.Add(rte);
}

if (cbType.Items.Count > 0)
{
cbType.SelectedIndex = 0;
}
}

 19. Add a reference to the class.

using com.casperlee.ResourceManager.Bll;

20. Open the MainForm in the main project, and add a OnClick event handler for the "Add" button.

        private void btnAdd_Click(object sender, EventArgs e)
{
frmAddResource frm = new frmAddResource();
frm.ShowDialog(this);
frm.Close();
}

21. Rebuild all projects and see if it works.