Programming     Travel Logs     Life Is Good     Surfing Online     About Me
Specific knowledge is found by pursuing your genuine curiosity and passion rather than whatever is hot right now.
-Naval Ravikant
2018-05-03 20:45:13

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

The search function is not hard to be implemented, so I'll just post source code here without explaining too much.

/Images/20161212/01.jpg

/Images/20161212/02.jpg

/Images/20161212/03.jpg

1. Open the MainForm and put the following code into the Load event handler.

        private void MainForm_Load(object sender, EventArgs e)
{
...
InitializeColumns();
}

2. Here is the code of the InitializeColumns function.

        private void InitializeColumns()
{
lvResult.BeginUpdate();
try
{
lvResult.Columns.Clear();
lvResult.Columns.Add("ID", 0);
lvResult.Columns.Add("Name", 480);
lvResult.Columns.Add("Keywords", 250);
lvResult.Columns.Add("Type", 100);
lvResult.Columns.Add("Filename", 0);
}
finally
{
lvResult.EndUpdate();
}
}

3. Double click on the "Search" button to add an OnClick event handler, and put the following source code in.

        private void btnSearch_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(tbSearchText.Text.Trim()))
{
return;
}

List<ResourceEx> resources = new List<ResourceEx>();
ResourceBll.Instance.Search(tbSearchText.Text, CEnvironment.CurrentPassword, resources);
RefreshResultList(resources);
}

4. Here is the code of the RefreshResultList function.

        private void RefreshResultList(List<ResourceEx> aResources)
{
lvResult.BeginUpdate();
try
{
ListViewItem lvi;
lvResult.Items.Clear();
foreach (ResourceEx r in aResources)
{
lvi = lvResult.Items.Add(r.ID.ToString());
lvi.SubItems.Add(r.Name);
lvi.SubItems.Add(r.Keywords);
lvi.SubItems.Add(r.ResourceTypeEx.ToString());
lvi.SubItems.Add(r.FileName);
lvi.Tag = r;
}
}
finally
{
lvResult.EndUpdate();
}
}

5. Open the ResourceBll.cs in the Bll project add the following function.

        public void Search(string aText, string aPassword, List<ResourceEx> aResult)
{
CreateHandler(true);
handler.Search(aText, aPassword, aResult);
}

6. Here is the code of the CreateHandler function.

        private void CreateHandler(bool aSupportFulltextSearch)
{
if ((handler == null)
|| (handler is FulltextResourceHandler != aSupportFulltextSearch))
{
handler = aSupportFulltextSearch ?
new FulltextResourceHandler() : new ResourceHandler();
}
}

7. Open ResourceHandler.cs in the Bll project and add the following function.

        public virtual void Search(string aText, string aPassword, List<ResourceEx> aResult)
{
string anEncryptedPassword;
ResourceEx rx;

using (ResourceManageEntities rme = new ResourceManageEntities())
{
foreach (Resource r in rme.Resources)
{
anEncryptedPassword = CEncoding.GetMD5String(aPassword + r.ID.ToString());
if (string.Compare(r.Password, anEncryptedPassword) != 0)
{
continue;
}

rx = new ResourceEx();
rx.CopyFrom(r);
rx.EntityType = ResourceEntityType.retDB;
rx.Encrypted = true;
rx.Decrypt(CEnvironment.CurrentPassword);

if ((string.Compare(aText.ToUpper(), "CASPER") != 0)
&& !rx.Name.ToUpper().Contains(aText.ToUpper())
&& !rx.Keywords.ToUpper().Contains(aText.ToUpper()))
{
continue;
}

aResult.Add(rx);
}
}
}

Finished.