Click here to Skip to main content
15,895,709 members
Articles / Desktop Programming / Windows Forms

Creating the Same Program in Windows Forms and WPF

Rate me:
Please Sign up or sign in to vote.
4.84/5 (53 votes)
3 May 2008CPOL9 min read 264.7K   5.8K   194  
Shows how to write the same simple program in Windows Forms and WPF.
using System.Drawing;
using System.Windows.Forms;
using BusinessObjects;

namespace WinFormsApp
{
    /// <summary>
    /// A WinForms control that displays an Employee object.
    /// </summary>
    public partial class EmployeeControl : UserControl
    {
        public EmployeeControl()
        {
            InitializeComponent();

            // Convert the picture file path to a Bitmap.
            Binding binding = this.employeePicture.DataBindings[0];
            binding.Format += this.ConvertFilePathToBitmap;
        }

        void ConvertFilePathToBitmap(object sender, ConvertEventArgs e)
        {
            e.Value = Bitmap.FromFile(e.Value as string);
        }

        public Employee Employee
        {
            get { return this.employeeBindingSource.DataSource as Employee; }
            set 
            { 
                this.employeeBindingSource.DataSource = value;

                // The employee's picture shows a tooltip of their ID.
                if (value != null)
                {
                    string msg = "Employee ID:  " + value.ID;
                    this.toolTip.SetToolTip(this.employeePicture, msg);
                }
            }
        }
    }
}

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)
United States United States
Josh creates software, for iOS and Windows.

He works at Black Pixel as a Senior Developer.

Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.

Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.

Check out his Advanced MVVM[^] book.

Visit his WPF blog[^] or stop by his iOS blog[^].

See his website Josh Smith Digital[^].

Comments and Discussions