You are viewing our Forum Archives. To view or take place in current topics click here.
Youtube Custom Control (VB)
Posted:
Youtube Custom Control (VB)Posted:
Status: Offline
Joined: Sep 26, 201014Year Member
Posts: 401
Reputation Power: 17
Status: Offline
Joined: Sep 26, 201014Year Member
Posts: 401
Reputation Power: 17
Hey Guys,
I have been working on a youtube like/dislike bar custom control. I got the idea from a guy on youtube. He coded one in C# but i am not a big fan of C# so i decided to make my own in visual basic. I also added a feature that means you can get the likes OR dislikes from any youtube video!
So to use this go in and make a new project.
Now you have made a project go and add a new class and name it YoutubeRatingBar.
In this class you must add this code
Okay so now you have done that if you click to build the project(It is important you do that!) and go back to the main form and look at the tool box you will notice that there is a control called YoutubeRatingBar.
Okay so now with the form add :
1 YoutubeRatingBar : Leave the name as it is.
1 Button : Name it BtnGetStats
1 Textbox : Name it TxtLink
1 Lable : Name it LblStats
Okay now all you have to do is add this code:
For btnGetStats Click add this code
Please note it is very important to put it in a try catch statement as if you enter a invalid URL the program will crash.
Now for the form load as this code
Feel free to use this code in any of your programs. All i ask is for you to look over the code and try your best to understand it.
Hope its helpful,
Follow : @BighairSoftware
Last edited by Bighair ; edited 1 time in total
I have been working on a youtube like/dislike bar custom control. I got the idea from a guy on youtube. He coded one in C# but i am not a big fan of C# so i decided to make my own in visual basic. I also added a feature that means you can get the likes OR dislikes from any youtube video!
So to use this go in and make a new project.
Now you have made a project go and add a new class and name it YoutubeRatingBar.
In this class you must add this code
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Net
Imports System.Text.RegularExpressions
Public Class YouTubeRatingBar : Inherits Control
#Region "Properties"
'This property will contain the amount of likes.
Private Likes_ As Integer
Public Property Likes As Integer
Set(ByVal value As Integer)
Likes_ = value
Me.Invalidate()
End Set
Get
Return Likes_
End Get
End Property
'This property will contain the amount if dislikes.
Private Dislike_ As Integer
Public Property Dislikes As Integer
Set(ByVal value As Integer)
Dislike_ = value
Me.Invalidate()
End Set
Get
Return Dislike_
End Get
End Property
#End Region
Public Sub YoutubeControle()
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
'Declare a new instane of a bitmap.
Dim bitmap As Bitmap = New Bitmap(Me.Width, Me.Height)
'Give graphics the values of the bitmap
Dim graphics As Graphics = graphics.FromImage(bitmap)
'This if statment checks to see if there is 1 or more likes in it
'and no dislikes. if this is the case it will color it in pure green
If (Likes >= 1) And (Dislikes = 0) Then
graphics.FillRectangle(Brushes.Green, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))
End If
'This if statment checks to see if there is 1 or more dislikes in it
'and no likes. if this is the case it will color it in pure Red
If (Likes = 0) And (Dislikes >= 1) Then
graphics.FillRectangle(Brushes.Red, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))
End If
'This checks to see if there is a mixture of likes and dislikes
If (Likes > 0) And (Dislikes > 0) Then
'This calculates how much of the control should be green
Dim LikesWidth As Integer = Likes / (Likes + Dislikes) * Me.Width
'This calculates how much of the control should be red
Dim DislikesWidth As Integer = Dislikes / (Likes + Dislikes) * Me.Width
'This colors the triangle in.
graphics.FillRectangle(Brushes.Green, 0, 0, LikesWidth, Me.Height)
graphics.FillRectangle(Brushes.Red, Me.Width - DislikesWidth, 0, DislikesWidth, Me.Height)
End If
'Draw a box around the controle
graphics.DrawRectangle(Pens.Black, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))
'Draw the graphics to the controle
e.Graphics.DrawImage(bitmap, 0, 0)
MyBase.OnPaint(e)
End Sub
Public Sub GetLikesAndDisklike(ByVal Link As String)
'Get the youtube page source
Dim Request As HttpWebRequest = HttpWebRequest.Create(Link)
Dim Response As HttpWebResponse = Request.GetResponse()
Dim StreamReader As System.IO.StreamReader = New System.IO.StreamReader(Response.GetResponseStream())
Dim SourceCode As String = StreamReader.ReadToEnd()
'Now Parse the source code to get the dislikes and the likes
Dim R As Regex = New Regex("<span class=""likes"">(.*)</span> likes, <span class=""dislikes"">(.*)</span> dislikes")
Dim M As Match = R.Match(SourceCode)
Dim likess = M.Groups(1)
Dim Dislikess = M.Groups(2)
'Set the likes Property and the dislike propety to the results
Likes = likess.ToString
Dislikes = Dislikess.ToString
End Sub
End Class
Okay so now you have done that if you click to build the project(It is important you do that!) and go back to the main form and look at the tool box you will notice that there is a control called YoutubeRatingBar.
Okay so now with the form add :
1 YoutubeRatingBar : Leave the name as it is.
1 Button : Name it BtnGetStats
1 Textbox : Name it TxtLink
1 Lable : Name it LblStats
Okay now all you have to do is add this code:
For btnGetStats Click add this code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
YouTubeRatingBar1.GetLikesAndDisklike(TextBox1.Text)
LblStats.Text = YouTubeRatingBar1.Likes & " Likes and " & YouTubeRatingBar1.Dislikes & " Dislikes"
Catch ex As Exception
MsgBox("Error", "The error is probably caused by an invald Url", 6)
End Try
End Sub
Please note it is very important to put it in a try catch statement as if you enter a invalid URL the program will crash.
Now for the form load as this code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LblStats.Text = YouTubeRatingBar1.Likes & " Likes and " & YouTubeRatingBar1.Dislikes & " Dislikes"
End Sub
Feel free to use this code in any of your programs. All i ask is for you to look over the code and try your best to understand it.
Hope its helpful,
Follow : @BighairSoftware
Last edited by Bighair ; edited 1 time in total
The following 1 user thanked Bighair for this useful post:
Imp (09-24-2012)
#2. Posted:
Status: Offline
Joined: Oct 31, 201014Year Member
Posts: 2,084
Reputation Power: 95
Status: Offline
Joined: Oct 31, 201014Year Member
Posts: 2,084
Reputation Power: 95
#3. Posted:
Status: Offline
Joined: Jan 01, 201114Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201114Year Member
Posts: 1,957
Reputation Power: 401
#4. Posted:
Status: Offline
Joined: Sep 26, 201014Year Member
Posts: 401
Reputation Power: 17
Status: Offline
Joined: Sep 26, 201014Year Member
Posts: 401
Reputation Power: 17
#5. Posted:
Status: Offline
Joined: Sep 26, 201014Year Member
Posts: 401
Reputation Power: 17
Status: Offline
Joined: Sep 26, 201014Year Member
Posts: 401
Reputation Power: 17
Right guys!!! Updated the post to make it look cleaner! I also added in a new feature
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.