Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
Greetings,

I have a windows forms app coded in c# for playing some music using the axWidowsMediaPplayer component. I am trying to add thumbnail toolbar buttons for playing and and pausing the player.
The buttons are being created but their events are not being fired or nothing's happening on clicking them .

Please Help I have killing myself over this for two days now with no luck. Please Help.

Here's My Code :
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TagLib;
 
using System.Windows.Shell;
using System.IO;
using System.Runtime.InteropServices;
using System.Net;
using System.Xml;
using System.Linq;
using Lastfm;
using Lastfm.Services;
using System.Web;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.WindowsAPICodePack.Taskbar;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.ApplicationServices;
using System.Windows.Interop;
 

namespace KryptSoft
{
 
    public partial class Form1 : Form
    {
        static Icon B1Icon  = new StockIcon(StockIconIdentifier.Shield).Icon;
        static Icon B2Icon  = new StockIcon(StockIconIdentifier.Users).Icon;
        static Icon B3Icon  = new StockIcon(StockIconIdentifier.Help).Icon;
        private ThumbnailToolbarButton ads;
        private ThumbnailToolbarButton ads2;
       
       
           
 
        public Form1()
        {
 
            InitializeComponent();
            textBox5.Visible = false;
            button8.Visible = false;
            selector.Visible = false;
            textBox2.Visible = false;
            p3.Visible = false;
            textBox7.Visible = false;
            label15.Visible = false;
            
        }
 
         private void Form1_Load(object sender, EventArgs e)
        {
           
 
        }
        private void CreateTabbedThumbnail()
        {
 
        }
 
        private void Form1_Shown(object sender, EventArgs e)
        {
            this.ads = new ThumbnailToolbarButton(this.Icon, "Next");
            this.ads2 = new ThumbnailToolbarButton(this.Icon, "prev");
           this.ads.Enabled = true;
            this.ads.IsInteractive = true;
            this.ads2.Enabled = true;
            this.ads2.IsInteractive = true;
            this.ads.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(thumbButtonNext_Click);
            this.ads2.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(thumbButtonPrev_Click);
            TaskbarManager.Instance.ThumbnailToolbars.AddButtons(this.Handle, this.ads2, this.ads);
        
        }
        void thumbButtonPrev_Click(object sender, Microsoft.WindowsAPICodePack.Taskbar.ThumbnailButtonClickedEventArgs e)
        {
            player.Ctlcontrols.pause();
        }
        void thumbButtonNext_Click(object sender, Microsoft.WindowsAPICodePack.Taskbar.ThumbnailButtonClickedEventArgs e)
        {
            player.Ctlcontrols.play();
        }
 
        /*=====================================================================================================
          --------------------------------------------------------TAB 1----------------------------------------
          ======================================================================================================*/
 
        string[] files, files2, files3, path, path2, path3;
        int counter = 1, c2;
 

 
        private bool button1WasClicked = false;
        private bool button7WasClicked = false;
 
        private void button10_Click(object sender, EventArgs e)
        {
            try
            {
 
                playlist.Items.Clear();
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    files = openFileDialog1.SafeFileNames;
                    path = openFileDialog1.FileNames;
 

                    for (int i = 0; i < files.Length; i++)
                    {
                        playlist.Items.Add(files[i]);
                    }
                    playlist.SelectedIndex = 0;
                    player.URL = path[playlist.SelectedIndex];
                    player.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(player_PlayStateChange);
                    if (player.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
                    {
                        playlist.SelectedIndex++;
                        player.URL = path[playlist.SelectedIndex];
 
                    }
 
                }
            }
            catch (Exception)
            { }
        }
 
        private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
 
            if (e.newState == 8)
            {
                timer1.Interval = 100;
                timer1.Enabled = true;
 
            }
        }
        private void playlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                player.URL = path[playlist.SelectedIndex];
 
                TagLib.File f1 = TagLib.File.Create(path[playlist.SelectedIndex]);
                textBox3.Text = f1.Tag.Title;
                textBox4.Text = GetAllStringsFromArrary(f1.Tag.AlbumArtists, ",");
                textBox55.Text = f1.Tag.Album;
                textBox6.Text = GetAllStringsFromArrary(f1.Tag.Performers, ",");
                lyric.Clear();
            }
            catch (Exception)
            {
 
                MessageBox.Show("OOPS!! some error occurred. Please Restart The Application or Click Properly On The Song Name");
 
            }
        }
 

 
        /*=====================================================================================================
          --------------------------------------------------------TAB 2----------------------------------------
          ======================================================================================================*/
 

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                button1WasClicked = false;
                button7WasClicked = false;
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Mp3 Files | *.mp3";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = ofd.FileName;
                    p2.Visible = false;
 
                }
 

                TagLib.File f2 = TagLib.File.Create(textBox1.Text);
                title.Text = f2.Tag.Title;
                artist.Text = GetAllStringsFromArrary(f2.Tag.AlbumArtists, ",");
                albums.Text = f2.Tag.Album;
                year.Text = f2.Tag.Year.ToString();
                duration.Text = f2.Properties.Duration.ToString();
                genre.Text = GetAllStringsFromArrary(f2.Tag.Genres, ",");
                cartist.Text = GetAllStringsFromArrary(f2.Tag.Performers, ",");
                if (f2.Tag.Pictures.Length > 0)
                {
                    TagLib.IPicture pic = f2.Tag.Pictures[0];
                    System.Drawing.Image currentImage = null;
                    MemoryStream ms = new MemoryStream(pic.Data.Data);
                    if (ms != null && ms.Length > 4096)
                    {
                        currentImage = System.Drawing.Image.FromStream(ms);
                        // Load thumbnail into PictureBox
                        p2.Image = currentImage;
                        p2.Visible = true;
                    }
                    ms.Close();
                }
            }
            catch (Exception)
            { }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
 

                if (string.IsNullOrWhiteSpace(this.textBox1.Text))
                {
                    MessageBox.Show("Please Select A File");
                }
 
                else
                {
                    string filePath = textBox1.Text;
                    bool isFileInUse;
                    isFileInUse = FileInUse(filePath);
 
                    if (isFileInUse)
                    {
                        MessageBox.Show("File is in use");
                    }
                    else
                    {
 
                        if ((button1WasClicked == false) && (button7WasClicked == false))
                        {
                            TagLib.File f = TagLib.File.Create(textBox1.Text);
                            f.Tag.Title = title.Text;
                            f.Tag.AlbumArtists = this.artist.Text.Split('\0');
                            f.Tag.Album = albums.Text;
                            f.Tag.Year = Convert.ToUInt32(year.Text);
                            f.Tag.Genres[0] = genre.Text;
                            f.Tag.Performers = this.cartist.Text.Split(',');
                            f.Save();
                            MessageBox.Show("applied");
                            p3.Image = null;
                            p2.Image = null;
                            ClearTextBoxes(this);
                        }
 

                        else if (button1WasClicked)
                        {
 

                            TagLib.File f = TagLib.File.Create(textBox1.Text);
                            IPicture[] pictures = new IPicture[1];
                            pictures[0] = new Picture(textBox7.Text + "\\" + c2.ToString() + "####" + c2.ToString() + ".png");
                            f.Tag.Pictures = pictures;
                            f.Tag.Title = title.Text;
                            f.Tag.AlbumArtists = this.artist.Text.Split(',');
                            f.Tag.Album = albums.Text;
                            f.Tag.Year = Convert.ToUInt32(year.Text);
                            f.Tag.Genres = this.genre.Text.Split(',');
                            f.Tag.Performers = this.cartist.Text.Split(',');
                            f.Save();
                            deleter();
                            MessageBox.Show("applied");
                            p3.Image = null;
                            p2.Image = null;
                            ClearTextBoxes(this);
 
                        }
 
                        else if (button7WasClicked)
                        {
 
                            TagLib.File f = TagLib.File.Create(textBox1.Text);
                            IPicture[] pictures = new IPicture[1];
                            pictures[0] = new Picture(textBox2.Text);
                            f.Tag.Pictures = pictures;
                            f.Tag.Title = title.Text;
                            f.Tag.AlbumArtists = this.artist.Text.Split(',');
                            f.Tag.Album = albums.Text;
                            f.Tag.Year = Convert.ToUInt32(year.Text);
                            f.Tag.Genres = this.genre.Text.Split(',');
                            f.Tag.Performers = this.cartist.Text.Split(',');
                            f.Save();
                            MessageBox.Show("Applied");
                            p3.Image = null;
                            p2.Image = null;
                            ClearTextBoxes(this);
                        }
                        else
                        {
                            p3.Image = null;
                            p2.Image = null;
                            ClearTextBoxes(this);
                            MessageBox.Show("Nothing Done");
                        }
 

 
                    }
                }
                button8.Visible = false;
                selector.Visible = false;
                textBox2.Visible = false;
                p3.Visible = false;
                label15.Visible = false;
            }
            catch (Exception)
            { }
        }
 
        bool FileInUse(string path)
        {
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
                {
                    if (fs.CanWrite)
                    { }
                }
                return false;
            }
            catch (IOException)
            {
                return true;
            }
        }
 

        public void ClearTextBoxes(Control control)
        {
            foreach (Control c in control.Controls)
            {
                if ((c is System.Windows.Forms.TextBox) && (c.Name != "textBox7"))
                {
                    ((System.Windows.Forms.TextBox)c).Clear();
 
                }
                if (c.HasChildren)
                {
                    ClearTextBoxes(c);
                }
            }
        }
 
        public string GetAllStringsFromArrary(string[] strArray, string strDelimeter)
        {
            string strFinal = string.Empty;
 
            for (int i = 0; i < strArray.Length; i++)
            {
                strFinal += strArray[i];
 
                if (i != strArray.Length - 1)
                {
                    strFinal += strDelimeter;
                }
            }
            return strFinal;
 

        }
 

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd1 = new OpenFileDialog();
                ofd1.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
                if (ofd1.ShowDialog() == DialogResult.OK)
                {
                    textBox2.Text = ofd1.FileName;
                }
                Image image = Image.FromFile(textBox2.Text);
                p3.Image = image;
 
            }
            catch (Exception)
            { }
        }
 
        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                button8.Visible = true;
                selector.Visible = true;
                textBox2.Visible = true;
                p3.Visible = true;
                button7WasClicked = true;
                button1WasClicked = false;
            }
            catch (Exception)
            { }
        }
 
        private void button9_Click_1(object sender, EventArgs e)
        {
            try
            {
                p3.Image = null;
                p2.Image = null;
                ClearTextBoxes(this);
                button8.Visible = false;
                selector.Visible = false;
                textBox2.Visible = false;
                p3.Visible = false;
                label15.Visible = false;
                deleter();
            }
            catch (Exception)
            { }
        }
 
        /*=====================================================================================================
        --------------------------------------------------------TAB 3------------------------------------------
        =======================================================================================================*/
 
        //Substring method, but with starting index and ending index too.
        private static string slice(string source, int start, int end)
        {
            if (end < 0)
            {
                end = source.Length + end;
            }
            int len = end - start;
            return source.Substring(start, len);
        }
 
        //Method replaces first letter of all words to UPPERCASE and replaces all spaces with underscores.
        private static string sanitize(string s)
        {
            char[] array = s.Trim().ToCharArray();
            if (array.Length >= 1)
            {
                if (char.IsLower(array[0]))
                {
                    array[0] = char.ToUpper(array[0]);
                }
            }
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i - 1] == ' ')
                {
                    if (char.IsLower(array[i]))
                    {
                        array[i] = char.ToUpper(array[i]);
                    }
                }
            }
            return new string(array).Trim().Replace(' ', '_');
        }
 
        private string pullLyrics(string strArtist, string strSongTitle)
        {
            try
            {
                if ((string.IsNullOrWhiteSpace(this.textBox3.Text)) || ((string.IsNullOrWhiteSpace(this.textBox3.Text)) && (string.IsNullOrWhiteSpace(this.textBox3.Text))))
                {
                    MessageBox.Show("No Title or Artists Listed");
                    return null;
                }
                else
                {
                    WebClient wc = new WebClient();
                    string sLyrics = null;
                    string sUrl = null;
                    int iStart = 0;
                    int iEnd = 0;
                    sUrl = @"http://lyrics.wikia.com/index.php?title=" + sanitize(strArtist) + ":" + sanitize(strSongTitle) + "&action=edit";
                    //Set encoding to UTF8 to handle accented characters.
                    wc.Encoding = Encoding.UTF8;
                    sLyrics = wc.DownloadString(sUrl);
                    //Get surrounding tags.
                    iStart = sLyrics.IndexOf("<lyrics>") + 12;
                    iEnd = sLyrics.IndexOf("</lyrics>") - 1;
                    //Replace webpage standard newline feed with carriage return + newline feed, which is standard on Windows.
                    sLyrics = slice(sLyrics, iStart, iEnd).Replace("\n", Environment.NewLine).TrimEnd();
                    //If Lyrics Wikia is suggesting a redirect, pull lyrics for that.
                    if (sLyrics.Contains("#REDIRECT"))
                    {
                        iStart = sLyrics.IndexOf("#REDIRECT [[") + 12;
                        iEnd = sLyrics.IndexOf("]]", iStart);
                        strArtist = slice(sLyrics, iStart, iEnd).Split(':')[0];
                        strSongTitle = slice(sLyrics, iStart, iEnd).Split(':')[1];
                        pullLyrics(strArtist, strSongTitle);
                    }
                    //If lyrics weren't found :-(
                    else if (sLyrics.Contains("!-- PUT LYRICS HERE (and delete this entire line) -->"))
                        sLyrics = "Lyrics not found.";
 
                    return sLyrics;
                }
            }
            catch (Exception)
            { return null; }
        }
        private void lyrics_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrWhiteSpace(this.textBox4.Text)))
            {
                lyric.Text = pullLyrics(textBox6.Text.ToString(), textBox3.Text.ToString());
 
            }
 
            else
                lyric.Text = pullLyrics(textBox4.Text.ToString(), textBox3.Text.ToString());
        }
 

        /*=====================================================================================================
          --------------------------------------------------------TAB 4------------------------------------------
          =======================================================================================================*/
 
      
        public static string AbsUrlOfArt(string album, string artist)
        {
 
            string ApiKey = "2c5ae3d75d62bab28cd42352e3b22ede";
            string LastFMLogin = "kryptonian1111";
            Lastfm.Services.Session session = new Lastfm.Services.Session(ApiKey, LastFMLogin);
            Lastfm.Services.Artist lArtist = new Lastfm.Services.Artist(artist, session);
            Lastfm.Services.Album lAlbum = new Lastfm.Services.Album(lArtist, album, session);
 
            try
            {
                return lAlbum.GetImageURL(AlbumImageSize.ExtraLarge);
            }
 
            catch (Exception)
            {
                return lArtist.GetImageURL(ImageSize.Large);
            }
 

        }
 

        public static System.Drawing.Image AlbumArt(string album, string artist)
        {
            Stream stream = null;
            try
            {
 
                WebRequest req = WebRequest.Create(AbsUrlOfArt(album, artist));
                WebResponse response = req.GetResponse();
                stream = response.GetResponseStream();
                Image img = Image.FromStream(stream);
                // Image img = FromStream(stream);
                return img;
 
            }
 
            catch (Exception)
            {
                return null;
            }
 
            finally
            {
                if (stream != null)
                    stream.Dispose();
            }
 
        }
        public static class HtmlRemoval
        {
            /// <summary>
            /// Remove HTML from string with Regex.
            /// </summary>
            public static string StripTagsRegex(string source)
            {
                return Regex.Replace(source, "<.*?>", string.Empty);
            }
 
            /// <summary>
            /// Compiled regular expression for performance.
            /// </summary>
            static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
 
            /// <summary>
            /// Remove HTML from string with compiled Regex.
            /// </summary>
            public static string StripTagsRegexCompiled(string source)
            {
                return _htmlRegex.Replace(source, string.Empty);
            }
 
            /// <summary>
            /// Remove HTML tags from string using char array.
            /// </summary>
            public static string StripTagsCharArray(string source)
            {
                char[] array = new char[source.Length];
                int arrayIndex = 0;
                bool inside = false;
 
                for (int i = 0; i < source.Length; i++)
                {
                    char let = source[i];
                    if (let == '<')
                    {
                        inside = true;
                        continue;
                    }
                    if (let == '>')
                    {
                        inside = false;
                        continue;
                    }
                    if (!inside)
                    {
                        array[arrayIndex] = let;
                        arrayIndex++;
                    }
                }
                return new string(array, 0, arrayIndex);
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                label15.Visible = true;
                textBox7.Visible = false;
                textBox7.Text = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                if ((string.IsNullOrWhiteSpace(this.textBox7.Text)))
                {
                    MessageBox.Show("Please Select A Location To Save The File");
                }
                else
                {
                    string ApiKey = "2c5ae3d75d62bab28cd42352e3b22ede";
                    string LastFMLogin = "kryptonian1111";
                    p3.Visible = true;
                    button7WasClicked = false;
                    button1WasClicked = true;
                    if ((string.IsNullOrWhiteSpace(this.albums.Text)) && (string.IsNullOrWhiteSpace(this.artist.Text)) && (string.IsNullOrWhiteSpace(this.cartist.Text)))
                    {
                        MessageBox.Show("No Artist Or Album Listed");
                    }
 
                    else
                    {
                        if (string.IsNullOrWhiteSpace(this.artist.Text))
                        {
                            string x = textBox7.Text + "\\" + counter.ToString() + "####" + counter.ToString() + ".png";
                            Lastfm.Services.Session session = new Lastfm.Services.Session(ApiKey, LastFMLogin);
                            Lastfm.Services.Artist lArtist = new Lastfm.Services.Artist(cartist.Text, session);
                            Lastfm.Services.Album lAlbum = new Lastfm.Services.Album(lArtist, albums.Text, session);
                            textBox5.Text = AbsUrlOfArt(albums.Text, cartist.Text);
                            System.Net.WebClient Client = new System.Net.WebClient();
                            Client.DownloadFile(AbsUrlOfArt(albums.Text, cartist.Text), x);
                            p3.ImageLocation = x;
                        }
 
                        else
                        {
                            string x = textBox7.Text + "\\" + counter.ToString() + "####" + counter.ToString() + ".png";
                            Lastfm.Services.Session session = new Lastfm.Services.Session(ApiKey, LastFMLogin);
                            Lastfm.Services.Artist lArtist = new Lastfm.Services.Artist(artist.Text, session);
                            Lastfm.Services.Album lAlbum = new Lastfm.Services.Album(lArtist, albums.Text, session);
                            //textBox1.Text = HtmlRemoval.StripTagsCharArray(lArtist.Bio.GetSummary());
                            textBox5.Text = AbsUrlOfArt(albums.Text, artist.Text);
                            System.Net.WebClient Client = new System.Net.WebClient();
                            Client.DownloadFile(AbsUrlOfArt(albums.Text, artist.Text), x);
                            p3.ImageLocation = x;
 
                        }
                    }
                    c2 = counter;
                    counter = counter + 1;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Some Error Occured.\n Please Check Your Album Name, Artist Name and Contributing Artists. \nSee To It That The Entries Are Valid.");
            }
        }
 
        public void deleter()
        {
            string x = textBox7.Text + "\\" + c2.ToString() + "####" + c2.ToString() + ".png";
            System.IO.File.Delete(x);
        }
 

        /*=====================================================================================================
          --------------------------------------------------------TAB 4------------------------------------------
          =======================================================================================================*/
 

        private void saveloc_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                location.Text = folderBrowserDialog1.SelectedPath;
            }
        }
 
        private void Select_Click(object sender, EventArgs e)
        {
            pl.Items.Clear();
            if (openFileDialog3.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                files3 = openFileDialog3.SafeFileNames;
                path3 = openFileDialog3.FileNames;
                for (int i = 0; i < files3.Length; i++)
                {
                    pl.Items.Add(files3[i]);
                }
            }
            pl.SelectedIndex = 0;
        }
 
        private void pl_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
 
                TagLib.File f2 = TagLib.File.Create(path3[pl.SelectedIndex]);
                textBox11.Text = (path3[pl.SelectedIndex]);
                title2.Text = f2.Tag.Title;
                artist2.Text = GetAllStringsFromArrary(f2.Tag.AlbumArtists, ",");
                album2.Text = f2.Tag.Album;
                cartist2.Text = GetAllStringsFromArrary(f2.Tag.Performers, ",");
 
            }
 
            catch (Exception)
            {
                MessageBox.Show("OOPS!! some error occurred. Please Restart The Application or Click Properly On The Song Name");
 

            }
        }
 
        private void Search_Click(object sender, EventArgs e)
        {
 
            if ((string.IsNullOrWhiteSpace(this.artist2.Text)) && (string.IsNullOrWhiteSpace(this.title2.Text)) && (string.IsNullOrWhiteSpace(this.album2.Text)) && (string.IsNullOrWhiteSpace(this.cartist2.Text)))
            {
                MessageBox.Show("Please Select a File First");
            }
            else
            {
 
                if ((string.IsNullOrWhiteSpace(this.artist2.Text)))
                {
                    lyric2.Text = pullLyrics2(cartist2.Text.ToString(), title2.Text.ToString());
 
                }
 
                else
                    lyric2.Text = pullLyrics2(artist2.Text.ToString(), title2.Text.ToString());
            }
        }
 
        private void Generate_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrWhiteSpace(this.location.Text)))
            {
                MessageBox.Show("Please Select A Location To Save The File");
            }
            else if ((string.IsNullOrWhiteSpace(this.artist2.Text)) && (string.IsNullOrWhiteSpace(this.title2.Text)) && (string.IsNullOrWhiteSpace(this.album2.Text)) && (string.IsNullOrWhiteSpace(this.cartist2.Text)))
            {
                MessageBox.Show("Please Select a File First");
            }
 
            else
            {
 

                if ((string.IsNullOrWhiteSpace(this.artist2.Text)))
                {
                    using (StreamWriter sw1 = new StreamWriter(location.Text + "\\" + cartist2.Text + "####" + title2.Text + ".zpl"))
                    {
                        sw1.WriteLine(lyric2.Text);
                    }
                }
                else
                {
 
                    using (StreamWriter sw1 = new StreamWriter(location.Text + "\\" + artist2.Text + "####" + title2.Text + ".zpl"))
                    {
                        sw1.WriteLine(lyric2.Text);
                    }
                }
 
            }
        }
 

        private string pullLyrics2(string strArtist, string strSongTitle)
        {
            try
            {
                if ((string.IsNullOrWhiteSpace(this.title2.Text)) || ((string.IsNullOrWhiteSpace(this.artist2.Text)) && (string.IsNullOrWhiteSpace(this.cartist2.Text))))
                {
                    MessageBox.Show("No Title or Artists Listed");
                    return null;
                }
                else
                {
                    WebClient wc = new WebClient();
                    string sLyrics = null;
                    string sUrl = null;
                    int iStart = 0;
                    int iEnd = 0;
                    sUrl = @"http://lyrics.wikia.com/index.php?title=" + sanitize(strArtist) + ":" + sanitize(strSongTitle) + "&action=edit";
                    //Set encoding to UTF8 to handle accented characters.
                    wc.Encoding = Encoding.UTF8;
                    sLyrics = wc.DownloadString(sUrl);
                    //Get surrounding tags.
                    iStart = sLyrics.IndexOf("<lyrics>") + 12;
                    iEnd = sLyrics.IndexOf("</lyrics>") - 1;
                    //Replace webpage standard newline feed with carriage return + newline feed, which is standard on Windows.
                    sLyrics = slice(sLyrics, iStart, iEnd).Replace("\n", Environment.NewLine).TrimEnd();
                    //If Lyrics Wikia is suggesting a redirect, pull lyrics for that.
                    if (sLyrics.Contains("#REDIRECT"))
                    {
                        iStart = sLyrics.IndexOf("#REDIRECT [[") + 12;
                        iEnd = sLyrics.IndexOf("]]", iStart);
                        strArtist = slice(sLyrics, iStart, iEnd).Split(':')[0];
                        strSongTitle = slice(sLyrics, iStart, iEnd).Split(':')[1];
                        pullLyrics(strArtist, strSongTitle);
                    }
                    //If lyrics weren't found :-(
                    else if (sLyrics.Contains("!-- PUT LYRICS HERE (and delete this entire line) -->"))
                        sLyrics = "Lyrics not found.";
 
                    return sLyrics;
                }
            }
 
            catch (Exception )
            { return null; }
        }
 
        private void lyric_TextChanged(object sender, EventArgs e)
        {
 
        }
 
       
 
        private void player_Enter(object sender, EventArgs e)
        {
 
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (playlist.SelectedIndex < files.Count() - 1)
            {
                playlist.SelectedIndex++;
                timer1.Enabled = false;
            }
            else
            {
                playlist.SelectedIndex = 0;
                timer1.Enabled = false;
            }
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            player.Ctlcontrols.pause();
        }
 
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
 
        }
 
       
 
        
 
    }
}
Posted
Updated 6-Jan-14 21:52pm
v3

I believe you need to move your code from FormShown event handler to FormLoad event handler. FormShown event is fired only with the form is first displayed and not not after that.
 
Share this answer
 
Comments
[no name] 7-Jan-14 3:02am    
I just tried Form_Load event handler. Now the buttons are not even being generated.
I could mail you the project if you want for a better look.
BillWoodruff 7-Jan-14 3:24am    
Hang on, I have a working example for you :)
[no name] 7-Jan-14 3:34am    
Sir please help
I doubt that your description of the problem is correct.
Note that in 64bit systems, exceptions arising in Form_Load or Form_Shown are just swallowed (yes, that's a WTF by design)! Wrap the code there in a try-catch block, and show an error message if something bad happens.
Also, for purpose of analysis, add a success message! You did not show any line of code which would cause that method to be invoked... I suspect the problem here.
Also, add some debug message / messagebox to the functions called in the Click event.
 
Share this answer
 
Comments
BillWoodruff 7-Jan-14 3:22am    
"64 bit system" is not a relevant issue here. I am about to post a working code example done in Win 8/64, compiled in VS 2013 against FrameWork 4.5, with "Any CPU" set as the target: the OP's code is solid, but there something else wrong, which his code does not show.
[no name] 7-Jan-14 3:26am    
Just tried that... no exceptions were generated , no message boxes appeared. nothing happened.
Bernhard Hiller 7-Jan-14 3:42am    
So you nowhere added a
this.Form_Shown += new EvenHandler(Form1_Shown);
Just do that e.g. in the constructor.
[no name] 7-Jan-14 3:51am    
I added the event using the properties of the form
Here's some code that works in Windows 8/64, compiled in VS 2013 against FrameWork 4.5, with "Any CPU" set as the target.

Your Project must reference the WindowsBase, and PresentationCore dll's, in addition to the WindowsAPICodePack and WindowsAPICodePack.Shell dll's.
C#
// required !
using System;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Taskbar;

private void Form1_Load(object sender, EventArgs e)
{
    ThumbnailToolBarButton thumbButtonNext = new ThumbnailToolBarButton(null, "button1");

    thumbButtonNext.Icon = new StockIcon(StockIconIdentifier.Shield).Icon;
    
    // for testing only: weird side-effects !
    thumbButtonNext.Click += (o, ea) => MessageBox.Show("next clicked");

    ThumbnailToolBarButton thumbButtonPrev = new ThumbnailToolBarButton(null, "button2");
    
    thumbButtonPrev.Icon = new StockIcon(StockIconIdentifier.Users).Icon;

    // for testing only: weird side-effects !
    thumbButtonPrev.Click += (o, ea) => MessageBox.Show("prev clicked");

    TaskbarManager.Instance.ThumbnailToolBars.AddButtons(Handle, thumbButtonPrev, thumbButtonNext); 
}
 
Share this answer
 
Comments
[no name] 7-Jan-14 3:43am    
i just tried your code , sir. the button were created but the events were not fired. I have a reference to PresentationCore and PresentationFramework using the NuGet Package manager as well.
BillWoodruff 7-Jan-14 4:12am    
What OS version are you using; what version of Visual Studio are you using ? I may not be able to help you further, since I am only using the OS, and VS, versions I described.

I doubt this would make a difference, but try adding the references manually from the Solution Explorer/Reference drop-down.

You have WindowsAPICodePack 1.1 ?

http://archive.msdn.microsoft.com/WindowsAPICodePack/Release/ProjectReleases.aspx?ReleaseId=4906
[no name] 8-Jan-14 7:12am    
Sir, I have solved it. There was some problem when I ran VS2010 as an Administrator as my app required it. But Then I deleted my manifest file and set the manifest to "Manifest With Defaukt Settings" and Now it works. Do you know why this happened ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900