Windows Forms: Youtube Search with Paging in C#

How to Create a Youtube Search with Paging using YoutubeSearch library in C#. YoutubeSearch is a library for .NET, written in C#, to show search query results from YouTube

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "YoutubeSearchDemo" and then click OK

youtube search in c#Step 2: Right click on your project select Manage NuGet Packages -> Search youtubesearch -> Install

youtube search installStep 3: Design youtube search form as below

youtube search in c#

Step 4: You need to create a Video class to map data

public class Video
{
    public string Title { get; set; }
    public string Author { get; set; }
    public string Url { get; set; }
    public Image Thumbnail { get; set; }
}

Add code to handle youtube search form

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;
using YoutubeSearch;

namespace YoutubeSearchDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int pageNumber = 1;

        private void btnSearch_Click(object sender, EventArgs e)
        {
            VideoSearch items = new VideoSearch();
            List<Video> list = new List<Video>();
            //Search video with paging
            foreach(var item in items.SearchQuery(txtSearch.Text, pageNumber))
            {
                Video video = new Video();
                video.Title = item.Title;
                video.Author = item.Author;
                video.Url = item.Url;
                //Get thumbnail
                byte[] imageBytes = new WebClient().DownloadData(item.Thumbnail);
                using(MemoryStream ms = new MemoryStream(imageBytes))
                {
                    video.Thumbnail = Image.FromStream(ms);
                }
                list.Add(video);
            }
            //Add videos to datagridview
            videoBindingSource.DataSource = list;
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            if (pageNumber > 1)
            {
                pageNumber--;
                btnSearch_Click(null, null);
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            pageNumber++;
            btnSearch.PerformClick();
        }
    }
}

VIDEO TUTORIALS