Loop foreach/for

Duarte Vinagre 60 Reputation points
2023-07-07T16:30:30.4066667+00:00

Hello... i have this code but my loop is not working...i want to apply the code to all items in the listbox...but i cannot perform the loop...it keeps applying the code just to 1 file... any help? Thanks!

for (int i = 0; i < 100; i++)
                    {

                        string mp3File = folderBrowserDialog2.SelectedPath + "\\" + listBox1.SelectedItem + ".mp3";
                        string outputFile = folderBrowserDialog1.SelectedPath + "\\" + listBox1.SelectedItem + ".wav";
                        int samples = Int32.Parse(comborate.Text);
                        int bits = Int32.Parse(combobits.Text);
                        int channs = Int32.Parse(combochans.Text);

                        using (Mp3FileReader reader = new Mp3FileReader(mp3File))
                        {


                        
                            progressBar1.Value = 0;
                            using (WaveStream pcmStream = new WaveFormatConversionStream(new WaveFormat(samples, bits, channs), reader))

                            {
                                WaveFileWriter.CreateWaveFile(outputFile, pcmStream);
                            }

                            progressBar1.PerformStep();
                        }
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2023-07-07T16:36:44.1233333+00:00

    Try this:

    foreach( var item in listBox1.Items )
    {
       string mp3File = folderBrowserDialog2.SelectedPath + "\\" + item + ".mp3";
       string outputFile = folderBrowserDialog1.SelectedPath + "\\" + item + ".wav";
       . . .
    }
    

  2. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2023-07-10T03:31:53.1066667+00:00

    Hi,@Duarte Vinagre. Welcome Microsoft Q&A. You could also update the code as follows.

    
    
            private void button1_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    string mp3File = folderBrowserDialog2.SelectedPath + "\\" + listBox1.Items[i] + ".mp3";
                    string outputFile = folderBrowserDialog1.SelectedPath + "\\" + listBox1.Items[i] + ".wav";
                    int samples = Int32.Parse(comborate.Text);
                    int bits = Int32.Parse(combobits.Text);
                    int channs = Int32.Parse(combochans.Text);
    
                    using (Mp3FileReader reader = new Mp3FileReader(mp3File))
                    {
                        progressBar1.Value = 0;
                        using (WaveStream pcmStream = new WaveFormatConversionStream(new WaveFormat(samples, bits, channs), reader))
                        {
                            WaveFileWriter.CreateWaveFile(outputFile, pcmStream);
                        }
    
                        progressBar1.PerformStep();
                    }
                }
            }
    
    
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.