C# Tutorials

Over the past 6 months I’ve worked on some intermediate C# tutorials, they go over topics that are not usually covered by other video tutorials on the internet.

C# Adding EventHandlers At Runtime with Lambda Expression

C# Adding EventHandlers At Runtime

C# extension methods

C# dynamic and loading DLLs at runtime

C# Static Wrapper Class

How to “open with” your WPF application (command line arguments) C#

C# Useing ref keyword in class constructor

Named and optional parameters in c#

C# Using optional parameters in class constructors

C# delegate (Part 1)

Basics, declaring delegates, using delegates, passing delegates as method parameters.

C# delegate (Part 2)

delegate invocation list and how they work.

Providing plug-in support in your C# application Part 1

Overview + creating the plugin interface.

Providing plug-in support in your C# application Part 2

using the plugin interface in your program.

Providing plug-in support in your C# application Part 3

making the plugin.

 

I also have a lot of videos planned, i’lll start working on them when i get back from overseas.

My tour of windows 8 developer preview (build 8102)

Enjoy.

 

 

AdvancedPostIt Devlopment Update (DevV0.07)

I’ve been working on a lot of things in this Dev Vision, This program is nearly to be Beta tested (by DevV0.10 maybe). I’ve added and fixed a lot of things, this has probbly been the largest DevV.

Here is the change log of this DevV:

0.07: (current)
-Implementing Options (incomplete)
-Back/Next playlist buttons
-lot of error handling
-Finishing up unfinished event handlers
-Notetaker
-YouTube Player… a lot f work needs to be done on this.
-Tray Icon Improvements

Here are a few Screenshots:

Image Viewer

 

Videos (Bottom: Vlc. Top: MediaElement

 

Playlist/Options./Manager

Note Taker and TrayIcon

WindowsAPICodePack Taskbar videos

Codesnippets for the 4 videos on the taskbar.
Developing for Windows 7: Taskbar #1: Overlay Icon:

using Microsoft.WindowsAPICodePack.Taskbar;
        TaskbarManager taskbar = TaskbarManager.Instance;
        Icon play, pause, stop;
        public MainWindow()
        {
            //...
            play = new Icon((Stream)Application.GetResourceStream(new Uri("pack://application:,,,/VideoPlayer;component/Play.ico")).Stream);
            stop = new Icon((Stream)Application.GetResourceStream(new Uri("pack://application:,,,/VideoPlayer;component/Stop.ico")).Stream);
            pause = new Icon((Stream)Application.GetResourceStream(new Uri("pack://application:,,,/VideoPlayer;component/Pause.ico")).Stream);
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Play button
            taskbar.SetOverlayIcon(play, "Playing");
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //Pause button
            taskbar.SetOverlayIcon(pause, "Paused");
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            //Stop button
            taskbar.SetOverlayIcon(stop, "Stopped");
        }

Developing for Windows 7: Taskbar #2: Progress Bar:

        private void slider_seek_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            taskbar.SetProgressValue((int)slider_seek.Value, (int)slider_seek.Maximum);
        }
            taskbar.SetProgressState(TaskbarProgressBarState.Paused); //Yellow
            taskbar.SetProgressState(TaskbarProgressBarState.Error); //Red
            taskbar.SetProgressState(TaskbarProgressBarState.Normal); //Green

Developing for Windows 7: Taskbar #3: Controlling the (Windows Aero) Thumbnail

        using System.Windows.Interop;
        private void Window_LayoutUpdated(object sender, EventArgs e)
        {
            Vector v = VisualTreeHelper.GetOffset(mediaElement1);
            TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip((new WindowInteropHelper(this)).Handle, new Rectangle((int)v.X, (int)v.Y, (int)mediaElement1.RenderSize.Width, (int)mediaElement1.RenderSize.Height));
            //For windows forms:
            //TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(this.Handle, new Rectangle((int)v.X, (int)v.Y, (int)mediaElement1.RenderSize.Width, (int)mediaElement1.RenderSize.Height));
        }

Developing for Windows 7: Taskbar #4: Taskbar Preview Buttons:

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            tbbPlay = new ThumbnailToolBarButton(icon_play, "Play");
            tbbPlay.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(button_play_Click);

            tbbPause = new ThumbnailToolBarButton(icon_pause, "Pause");
            tbbPause.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(button_pause_Click);

            tbbStop = new ThumbnailToolBarButton(icon_stop, "Stop");
            tbbStop.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(button_stop_Click);

            TaskbarManager.Instance.ThumbnailToolBars.AddButtons(new WindowInteropHelper(this).Handle, tbbPlay, tbbPause, tbbStop);
        }
        //I just renamed button1/2/3 click events.. Note changed to "EventArgs e" from "RoutedEventArgs e".
        private void button_play_Click(object sender, EventArgs e)
        {
            taskbar.SetOverlayIcon(icon_play, "Playing");
            taskbar.SetProgressState(TaskbarProgressBarState.Normal);
            mediaElement1.Play();
        }
        private void button_pause_Click(object sender, EventArgs e)
        {
            taskbar.SetOverlayIcon(icon_pause, "Paused");
            taskbar.SetProgressState(TaskbarProgressBarState.Paused);
            mediaElement1.Pause();
        }
        private void button_stop_Click(object sender, EventArgs e)
        {
            taskbar.SetOverlayIcon(icon_stop, "Stopped");
            taskbar.SetProgressState(TaskbarProgressBarState.Error);
            mediaElement1.Stop();
        }

How to import freinds from Facebook to Google plus (via Yahoo and Gmail)

I know that there are tons of tutorials on this already, but well none of them actually worked for me. Here is how i did it.

Here are the four things that you will need:

  1. A Facebook account.
  2. A Yahoo Account (if you don’t already have one, sign up).
  3. A G-mail Account
  4. A Google plus account connected to your G-mail account.

The steps are pretty simple, shouldn’t take more than 5 minutes:

  1. Log in to Yahoo.com (Follow these steps in Internet Explorer, i had trouble with Firefox in Yahooo mail with Firefox).If you get re-directed to the home page, click on mail.
  2. Go to the contacts tab and them click on import contacts. (This is why i recommended doing this in IE, The import contacts button didn’t work for me in Firefox)
  3. Click on the Facebook button.
  4. A window will popup, just follow the steps on it to log on to Facebook and allow access to yahoo.
  5. Once yahoo has finished importing your contacts click on ‘Actions’ then ‘Export all’. In the next window, export the ‘vCard Single File’ and save it somewhere.
  6. Then log on to G-mail (you can stop using IE now). Click on ‘Contacts’, then ‘More Actions”, then ‘Import’, then browse and import the file you saved from yahoo.
  7. Log on to Google+, Click on the circles button, then clock on ‘Find and Invite’, once you are there your G-mail contacts should appear in the list. you can now drag and drop people on to your circles to add/invite them on/to Google+.

Also you can add me to one of your circles :P.

Determine which operating system your program is running on with C#

Another tutorial from my developing for windows 7 series. How to check what OS the program is running on.

Layout of the form I'm using

OperatingSystem SystemInfo = Environment.OSVersion;
        public Form1()
        {
            InitializeComponent();
            label2.Text = SystemInfo.VersionString;
            label1.Text = OSV();
        }
        private string OSV()
        {
            if (SystemInfo.Platform == System.PlatformID.Win32NT)
            {
                if (SystemInfo.Version.Major == 5)
                    return "XP";
                else if (SystemInfo.Version.Major == 6)
                {
                    if (SystemInfo.Version.Minor == 1)
                        return "Win 7";                    
                    return "Vista";
                }
            }
            return "";
        }

Using the TaskDialog (WindowsAPICodePack)

In the last few weeks i started a tutorial series on YouTube on Developing for Windows 7 using the WindowsAPICodePack.

Anyway here is the codesnippets that i show in my tutorials.

Developing for windows 7: Task Dialog #1 :

            using Microsoft.WindowsAPICodePack;
            using Microsoft.WindowsAPICodePack.Dialogs;

            TaskDialog td = new TaskDialog();
            td.Caption = "This is a caption";
            td.InstructionText = "This is instruction text";
            td.Text = "This is text";
            td.Show();

Developing for windows 7: Task Dialog #2:

Adding Stranded Buttons:

TaskDialog td = new TaskDialog();
td.StandardButtons = TaskDialogStandardButtons.Ok | TaskDialogStandardButtons.Yes | TaskDialogStandardButtons.Retry | TaskDialogStandardButtons.No | TaskDialogStandardButtons.Close | TaskDialogStandardButtons.Cancel;
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
td.Show();

Capturing what the user presses:

            TaskDialog td = new TaskDialog();
            td.StandardButtons = TaskDialogStandardButtons.Ok | TaskDialogStandardButtons.Cancel;
            td.Caption = "This is a caption";
            td.InstructionText = "This is instruction text";
            td.Text = "This is text";
            TaskDialogResult tdr = td.Show();
            if (tdr == TaskDialogResult.Ok)
            {
                MessageBox.Show("Ok presed");
            }
            else if (tdr == TaskDialogResult.Cancel)
            {
                MessageBox.Show("Cancle presed");
            }

Developing for windows 7: Task Dialog #3:

Custom Buttons:

TaskDialog td = new TaskDialog();
TaskDialogButton custonbtn = new TaskDialogButton("btnCustom", "This is a custom btn");
custonbtn.Click += new EventHandler(custonbtn_Click);
td.Controls.Add(custonbtn);
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
td.Show();

Capturing what the user presses: (Please note that we are faking a button press event here)

TaskDialog td = new TaskDialog();
TaskDialogButton custonbtn = new TaskDialogButton("btnCustom", "This is a custom btn");
custonbtn.Click += new EventHandler(custonbtn_Click);
TaskDialogButton custonbtn2 = new TaskDialogButton("btnCustom2", "This is a custom btn");
td.Controls.Add(custonbtn);
td.Controls.Add(custonbtn2);
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
TaskDialogResult tdr = td.Show();
MessageBox.Show(tdr.ToString());

void custonbtn_Click(object sender, EventArgs e)
{
    MessageBox.Show("Custom button was pressed");
    TaskDialogButton tdb = (TaskDialogButton)sender;
    ((TaskDialog)tdb.HostingDialog).Close(TaskDialogResult.Ok);
}

Developing for windows 7: Task Dialog #4:

Adding RadioButtons

TaskDialog td = new TaskDialog();
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
TaskDialogRadioButton trb = new TaskDialogRadioButton("rbtBtn1", "Radio button 1");
td.Controls.Add(trb);
trb.Default = true;
TaskDialogRadioButton trb1 = new TaskDialogRadioButton("rbtBtn2", "Radio button 2");
td.Controls.Add(trb1);
TaskDialogRadioButton trb2 = new TaskDialogRadioButton("rbtBtn3", "Radio button 3");
td.Controls.Add(trb2);
td.Show();

Capturing what button the user presses: (Note that I’m using a trick to capture the input, this isn’t the proper way to do this)

            int choice = 0;
            TaskDialog td = new TaskDialog();
            td.Caption = "This is a caption";
            td.InstructionText = "This is instruction text";
            td.Text = "This is text";
            TaskDialogRadioButton trb = new TaskDialogRadioButton("rbtBtn1", "Radio button 1");
            trb.Default = true;
            trb.Click += (s, d) => choice = 0;
            td.Controls.Add(trb);
            TaskDialogRadioButton trb1 = new TaskDialogRadioButton("rbtBtn2", "Radio button 2");
            trb1.Click += (s, d) => choice = 1;
            td.Controls.Add(trb1);
            TaskDialogRadioButton trb2 = new TaskDialogRadioButton("rbtBtn3", "Radio button 3");
            trb2.Click += (s, d) => choice = 2;
            td.Controls.Add(trb2);
            TaskDialogResult tdr = td.Show();

            if (tdr == TaskDialogResult.Ok && choice == 0)
            {
                MessageBox.Show("radio button 1 selected");
            }
            else if (tdr == TaskDialogResult.Ok && choice == 1)
            {
                MessageBox.Show("radio button 2 selected");
            }
            else if (tdr == TaskDialogResult.Ok && choice == 2)
            {
                MessageBox.Show("radio button 3 selected");
            }

Developing for windows 7: Task Dialog #5:

TaskDialog td = new TaskDialog();
TaskDialogCommandLink cl = new TaskDialogCommandLink("cl", "Command link 1");
cl.Click += (s, d) =&gt; MessageBox.Show(cl.ToString() + " Pressed");
TaskDialogCommandLink cl1 = new TaskDialogCommandLink("cl", "Command link 2");
cl1.Click += (s, d) =&gt; td.Close(TaskDialogResult.Close);
cl1.Default = true;
td.Controls.Add(cl);
td.Controls.Add(cl1);
td.StandardButtons = TaskDialogStandardButtons.Ok;
td.FooterCheckBoxText = "Footer check box text";


////You cant add both Command links and custom buttons on the same taskdialog
//TaskDialogButton custonbtn = new TaskDialogButton("btnCustom", "This is a custom btn");
//td.Controls.Add(custonbtn);


TaskDialogResult tdr = td.Show();
MessageBox.Show(tdr.ToString());

Footer Text (I forgot to add this to the YouTube video):

TaskDialog td = new TaskDialog();
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
td.FooterText = "This text is in the footer";
td.Show();

Developing for windows 7: Task Dialog #6:

TaskDialog td = new TaskDialog();
td.Text = "Progress Bar";
TaskDialogProgressBar tdp = new TaskDialogProgressBar(0, 100, 0);
td.ProgressBar = tdp;


TaskDialogButton add = new TaskDialogButton("btn", "add progress");
add.Click += (s, d) =&gt; tdp.Value += 10;
td.Controls.Add(add);


TaskDialogButton ChangeState = new TaskDialogButton("btn", "Error State");
ChangeState.Click += (s, d) =&gt; tdp.State = TaskDialogProgressBarState.Error;
td.Controls.Add(ChangeState);
td.Show();

Developing for windows 7: Task Dialog #7 :

TaskDialog td = new TaskDialog();
td.Caption = "This is a caption";
td.InstructionText = "This is instruction text";
td.Text = "This is text";
td.DetailsExpandedLabel = "Show Less";
td.DetailsCollapsedLabel = "Show More";
td.DetailsExpandedText = "More details";
td.ExpansionMode = TaskDialogExpandedDetailsLocation.ExpandFooter;
td.Show();

Developing for windows 7: Task Dialog #8:

Hyperlinks:

TaskDialog td = new TaskDialog();
td.HyperlinksEnabled = true;
td.Text = "Go to &lt;a href=\"www.google.com\"&gt;Google&lt;/a&gt;";
td.HyperlinkClick += (s, d) =&gt; System.Diagnostics.Process.Start("www.google.com");
td.Show();

HelpInvoked:

TaskDialog td = new TaskDialog();
td.Text = "Press F1";
td.HelpInvoked += (s, d) =&gt; MessageBox.Show("Help Goes here");
td.Show();

Shield icon:

TaskDialogButton btn = new TaskDialogButton("btn", "Shield Icon");
btn.UseElevationIcon = true;
Follow

Get every new post delivered to your Inbox.