Question How to name multiple buttons after files

SpookedOnion

New member
Joined
Aug 8, 2018
Messages
2
Programming Experience
1-3
Alright, so I'm trying to make a MP3 player that takes the videos from youtube turns them into MP3's and then it creates a button for every MP3 in a folder.
I have already finished on making all the MP3s export to a folder, and a button is made for every single mp3 in the folder, but the only hangup I'm having is
naming all of the buttons after the filenames of the MP3s - the .MP3 at the end.
C#:
public int ButtonAmount = 5;        public int x = 20;
        public int y = 20;
        public int files = 0;


        private void Form1_Load(object sender, EventArgs e)
        {


            int fCount = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\MusicP", "*.mp3", SearchOption.TopDirectoryOnly).Length;
            label1.Text = Convert.ToString(fCount);


            Point[] p = new Point[fCount];
            Button[] btn = new Button[fCount];
            int buttoncount = 0;


            string log = "";


            DirectoryInfo d = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\MusicP");
            FileInfo[] Files = d.GetFiles("*.mp3");
            string str = "";
            int stroong = 0;


            


            foreach (FileInfo file in Files)
            {
                
            }


                for (int i = 0; i < btn.GetLength(0); i++)
                {


                    x = x + 0;
                    y = y + 40;


                    btn[i] = new Button();
                    btn[i].Height = 33;
                    btn[i].Width = 145;


                    btn[i].Location = new Point(x, y);


                    log += p.ToString() + "\n";
                    btn[i].PointToClient(p[i]);
                    btn[i].Show();
                    btn[i].Text = "MEMES";
                    btn[i].ForeColor = Color.FromArgb(255, 255, 255);


                    btn[i].Click += new EventHandler(Music_Click);


                    Controls.Add(btn[i]);
            }


            foreach (Control control in this.Controls)
            {
                if (control.GetType() == typeof(Button))
                {
                    buttoncount = buttoncount + 1;
                    label2.Text = Convert.ToString(buttoncount);
                }
            }


        }


        private void Music_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button Clicks");
        }
    }
 
Alright I managed to fix this by writing the MP3 filename to a txt file line by line after it downloads, and then it sets the buttons name according the # button it is, so I download a third song it writes the third line so it will set the name of the button to the third line, but if you have a better way I am still open to other options :)
 
You get all the files from the folder and then you ignore them other than to count them. Why not use the names of the files you just got? I would tend to do it something like this:
var filePaths = Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments, "MusicP"), "*.mp3", SearchOption.TopDirectoryOnly);
var buttons = new List<Button>();

foreach (var filePath in filePaths)
{
    var fileName = Path.GetFileNameWithoutExtension(filePath);
    var btn = new Button {Name = fileName};

    buttons.Add(btn);
    Controls.Add(btn);
}

So, there are a few things to note there. Firstly, the use of Path.Combine to combine partial paths. Secondly, actually using the list of files to create the Buttons. Thirdly, the use of a List<T> rather than an array. Finally, the use of initialiser syntax.

To be honest, I'm not sure that you need an array or a List. If you were just using that array to know how many Buttons to create then that issue goes away by using the 'foreach' loop. With regards to the initialiser syntax, you can put as many property assignments as you like inside the braces, separated with commas. I would also recommend that you use a TableLayoutPanel or FlowLayoutPanel to automatically position the Buttons rather than setting the Location explicitly. Also, your calls to PointToClient and Show are completely pointless.
 
Back
Top Bottom