Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / Windows Forms

Code First: A Practical Case

Rate me:
Please Sign up or sign in to vote.
4.89/5 (19 votes)
26 Feb 2012CPOL15 min read 72.1K   3.7K   67  
A code first real life data model case
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Xah.MediaCatalogerLib.MetaInfo;
using Xah.MediaCatalogerLib.MetaInfo.Provider;
using Xah.MediaCatalogerLib.Model;

namespace Xah.MediaCataloger
{
    public partial class SearchMetaInfoForm : Form
    {
        public BasicVideoMetaInfo SelectedMatch { get; private set; }

        public SearchMetaInfoForm(string title, VideoMetaInfoProviderEnum provider)
        {
            InitializeComponent();

            // Set title
            titleTextBox.Text = title;

            // Fill providers
            providerComboBox.DataSource = Enum.GetValues(typeof(VideoMetaInfoProviderEnum));
            providerComboBox.SelectedItem = provider;            
        }

        private void SearchMetaInfoForm_Load(object sender, EventArgs e)
        {
            PerformSearch();
        }

        private void PerformSearch()
        {
            this.Cursor = Cursors.WaitCursor;
            
            IVideoMetaInfoProvider provider = VideoMetaInfoProviderFactory.CreateProvider((VideoMetaInfoProviderEnum)providerComboBox.SelectedItem);
            List<BasicVideoMetaInfo> matches = provider.Search(titleTextBox.Text).OrderByDescending(x => x.Year).ToList();
            FillList(matches);
            
            this.Cursor = Cursors.Default;
        }

        private void FillList(List<BasicVideoMetaInfo> matches)
        {
            matchesListView.Items.Clear();

            ImageList imgl = new ImageList();
            imgl.ColorDepth = ColorDepth.Depth24Bit;
            imgl.ImageSize = new Size(MediaCatalogerConstants.StandardPosterWidth / 2, MediaCatalogerConstants.StandardPosterHeight / 2);

            foreach (BasicVideoMetaInfo bvmi in matches)
            {
                if (bvmi.PosterImage != null)
                    imgl.Images.Add(bvmi.VideoMetaInfoProviderUrl, bvmi.PosterImage);
            }

            matchesListView.SmallImageList = imgl;
            foreach (BasicVideoMetaInfo bfi in matches)
            {
                ListViewItem lvi = new ListViewItem(new string[] { bfi.Title, bfi.Year.ToString() }, bfi.VideoMetaInfoProviderUrl);
                lvi.Tag = bfi;
                matchesListView.Items.Add(lvi);
            }

            titleTextBox.SelectAll();
            titleTextBox.Focus();
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            PerformSearch();
        }

        private void matchesListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool isSelectionOk = (matchesListView.SelectedItems.Count == 1);
            selectButton.Enabled = isSelectionOk;
            SelectedMatch = null;

            if (isSelectionOk)
            {
                ListViewItem lvi = matchesListView.SelectedItems[0];
                SelectedMatch = lvi.Tag as BasicVideoMetaInfo;
            }
        }

        private void matchesListView_DoubleClick(object sender, EventArgs e)
        {
            AcceptForm();
        }

        private void selectButton_Click(object sender, EventArgs e)
        {
            AcceptForm();
        }

        private void AcceptForm()
        {
            if (SelectedMatch != null)
                this.DialogResult = DialogResult.OK;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Spain Spain
I studied Telecommunication with spezialization in Sound & Image. I was always very interested in programming at university as well and that is how I earn a living.

Since some years ago, I am also succesfully involved in software architecture and design.

Comments and Discussions