Question

I am currently trying to make my mp3 player for a project.

I have written this code by following a guide:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }    
        string[] files, path;
        [STAThread]  
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                files = openFileDialog1.SafeFileNames;
                path = openFileDialog1.FileNames;
                for (int i = 0; i < files.Length; i++)
                {
                    listBox1.Items.Add(files[i]);
                }
            }
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.URL = path[listBox1.SelectedIndex];
        }
    }
}

And heres a screen of my form: enter image description here

For some reason when I click the OPEN button, nothing happens. First when I used this code I got an error about "Form1_load", but since I wasn't using this, I just deleted the error line and then there was no errors found.

I am very clueless, so anyone with ANY idea what is wrong?

I followed this guide completely: http://www.c-sharpcorner.com/UploadFile/8ea152/mp3-media-player-in-C-Sharp-4-0/

Thanks

Was it helpful?

Solution

First of all make sure that you have assigned click event for your button for that just double click on your button from design window if it redirects you to the buttonclick method then everything is fine and if it does not then assign it first. For that

 public Form1()
        {
            InitializeComponent();
            button1.Click += button1_Click;
        } 

And

Why do you require [STAThread] here? I mean just remove the attribute and try again because [STAThread] is used for below reson:-

The STAThreadAttribute marks a thread to use the Single-Threaded COM Apartment if COM is needed. By default, .NET won't initialize COM at all. It's only when COM is needed, like when a COM object or COM Control is created or when drag 'n' drop is needed, that COM is initialized. When that happens, .NET calls the underlying CoInitializeEx function, which takes a flag indicating whether to join the thread to a multi-threaded or single-threaded apartment.

For more information :-

http://blogs.msdn.com/b/jfoscoding/archive/2005/04/07/406341.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top