Hello friends;
In this tutorial I’ll make an application showing YouTube videos in C # WebBrowser.
First, we create the application as follows.
Second, we create a variable named VideoID in From1 and define it as follows.
1 2 3 4 5 6 7 8 9 10 11 |
string _url; public string VideoID { get { var yMatch = new Regex(@"http(?:s?)://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)([\w\-]+)(&(amp;)?[\w\?=]*)?").Match(_url); return yMatch.Success ? yMatch.Groups[1].Value : String.Empty; } } |
Finally, we write the following code to Button1’s click event.
1 2 3 4 5 6 7 8 9 |
_url = textBox1.Text; webBrowser1.DocumentText = String.Format( "<html><head>" + "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\"/>" + "</head><body>" + "<iframe width=\"100%\" height=\"315\" src=\"https://www.youtube.com/embed/{0}?autoplay=1\"" + "frameborder = \"0\" allow = \"autoplay; encrypted-media\" allowfullscreen></iframe>" + "</body></html>", VideoID); |
Output:
All codes of the application
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public partial class Form1 : Form { string _url; public string VideoID { get { var yMatch = new Regex(@"http(?:s?)://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)([\w\-]+)(&(amp;)?[\w\?=]*)?").Match(_url); return yMatch.Success ? yMatch.Groups[1].Value : String.Empty; } } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { _url = textBox1.Text; webBrowser1.DocumentText = String.Format( "<html><head>" + "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\"/>" + "</head><body>" + "<iframe width=\"100%\" height=\"315\" src=\"https://www.youtube.com/embed/{0}?autoplay=1\"" + "frameborder = \"0\" allow = \"autoplay; encrypted-media\" allowfullscreen></iframe>" + "</body></html>", VideoID); } } |