How to Embed Youtube Video into a Windows Form in C#
By FoxLearn 11/27/2024 9:27:14 AM 5.86K
In this article, we will explore how to embed YouTube videos in a Windows Forms application using C#. We will walk through the process of capturing a YouTube video URL, extracting the video ID, and displaying the video in a WebBrowser
control.
How to add Youtube Video into a Windows Form in C#?
Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "EmbedYoutube" and then click OK
Drag and drop the Label, TextBox, Button and WebBrowser controls from the Visual toolbox onto your form designer, then design your form as shown below.
We are using System.Text.RegularExpressions
to work with regular expressions, which will allow us to extract the video ID from the YouTube URL.
string _ytUrl; // Property to get the video ID from the YouTube URL public string VideoId { get { var ytMatch = new Regex(@"youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)").Match(_ytUrl); return ytMatch.Success ? ytMatch.Groups[1].Value : string.Empty; } }
This property is responsible for extracting the YouTube video ID from the given YouTube URL.
We use a regular expression to match a variety of YouTube video URL formats (like youtube.com
, youtu.be
)
The regular expression matches the video ID from different URL structures, such as:
https://www.youtube.com/watch?v=VIDEO_ID
https://youtu.be/VIDEO_ID
https://www.youtube.com/v/VIDEO_ID?version=3
If the match is successful, it extracts and returns the video ID; otherwise, it returns an empty string.
Embedding YouTube Videos in a Windows Form Application Using C#
When the "Go" button is clicked, this event handler is triggered. The URL entered by the user in the txtUrl
TextBox is stored in the _ytUrl
variable.
// Event handler for the "Go" button click private void btnGo_Click(object sender, EventArgs e) { // Get the URL from the TextBox _ytUrl = txtUrl.Text; // Navigate to the embedded YouTube video webBrowser.Navigate($"http://youtube.com/v/{VideoId}?version=3"); }
We then call the Navigate()
method on the WebBrowser
control to load the YouTube video.
The video URL is constructed in the format http://youtube.com/v/VIDEO_ID?version=3
, where VIDEO_ID
is replaced by the actual ID extracted from the URL.
The WebBrowser
control is used to display web content, such as HTML pages. In this case, it is used to display the embedded YouTube player.
By using the WebBrowser
control and a regular expression to extract the video ID from a YouTube URL, you can easily embed YouTube videos within your Windows Forms applications.
VIDEO TUTORIAL
- How to Print Text in a Windows Form Application Using C#
- How to fill ComboBox and DataGridView automatically in C#
- How to Read text file and Sort list in C#
- How to pass ListView row data into another Form in C#
- How to read and write to text file in C#
- How to make a Countdown Timer in C#
- How to Display selected Row from DataGridView to TextBox in C#
- How to Get all Forms and Open Form with Form Name in C#