In this article I will explain with an example, how to play (embed) YouTube videos in Windows Forms (WinForms) Application using C# and VB.Net.
The YouTube videos will be played in Windows Forms (WinForms) Application with the help of WebBrowser control and HTML IFRAME using C# and VB.Net.
 
 
Form Design
The Form Design consists of a TextBox, a Button and a WebBrowser control. The Button has been assigned a Click event handler.
Play (Embed) YouTube videos in Windows Forms Application using C# and VB.Net
 
 
Playing (Embedding) YouTube videos in Windows Forms Application using C# and VB.Net
When the Button is clicked, an HTML string consisting of HTML IFRAME is generated and then the YouTube Video ID is extracted from the URL and is assigned to the SRC property of the HTML IFRAME.
Note: Compatibility Meta Tag is used in order to prevent compatibility issues.
 

C#
private void btnGo_Click(object sender, EventArgs e)
{
    string html = "<html><head>";
    html += "<meta content='IE=Edge' http-equiv='X-UA-Compatible'/>";
    html += "<iframe id='video' src= 'https://www.youtube.com/embed/{0}' width='420' height='250' frameborder='0' allowfullscreen></iframe>";
    html += "</body></html>";
    this.webBrowser1.DocumentText = string.Format(html, txtUrl.Text.Split('=')[1]);
}
 
VB.Net
Private Sub btnGo_Click(sender As System.Object, e As System.EventArgs) Handles btnGo.Click
    Dim html As String = "<html><head>"
    html &= "<meta content='IE=Edge' http-equiv='X-UA-Compatible'/>"
    html &= "<iframe id='video' src= 'https://www.youtube.com/embed/{0}' width='420' height='250' frameborder='0' allowfullscreen></iframe>"
    html &= "</body></html>"
    Me.webBrowser1.DocumentText = String.Format(html, txtUrl.Text.Split("=")(1))
End Sub
 
 
Screenshot
Play (Embed) YouTube videos in Windows Forms Application using C# and VB.Net
 
 
Downloads