1

I'm trying to set a maximum value of a metro theme trackbar using an integer from WindowsMediaPlayer's current media however it keeps throwing the following error:

Specified argument was out of the range of valid values.
Parameter name: Maximal value is lower than minimal one

This means that the maximum value is not being set at all, I'm not sure why.

Code:

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;
using MetroFramework;
using MetroFramework.Forms;
using VideoLibrary;
using System.IO;
using System.Threading;
using System.Diagnostics;
using WMPLib;
using UITimer = System.Windows.Forms.Timer;

namespace Composer
{
    public partial class Form1 : MetroForm
    {
        private string _pDirectory;
        private string[] _songs;

        private int _sIndex;

        private WindowsMediaPlayer wmp;
        private UITimer _timer;

        public Form1()
        {
            InitializeComponent();
        }

        private void metroTrackBar1_Scroll(object sender, ScrollEventArgs e)
        {
            wmp.settings.volume = metroTrackBar1.Value;
        }

        private void metroTile3_Click(object sender, EventArgs e)
        {
            playAudio(Path.Combine(_pDirectory, _songs[_sIndex]));
        }

        private void playAudio(string path)
        {
            wmp.URL = path;
            wmp.controls.play();
            _timer.Start();

            displayHeader(path);

            metroTrackBar2.Maximum = (int)wmp.currentMedia.duration;
        }

        private void t_Tick(object sender, EventArgs e)
        {
            metroTrackBar2.Value = (int)wmp.controls.currentPosition;
        }

        private void displayHeader(string song)
        {
            MethodInvoker invoke = new MethodInvoker(delegate
            {
                metroTile1.Text = Path.GetFileNameWithoutExtension(_songs[_sIndex]);
            });

            this.Invoke(invoke);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string bin;

            _sIndex = 0;
            _pDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Composer\");
            bin = Path.Combine(_pDirectory, "bin");

            _timer = new UITimer();
            _timer.Interval = 1000;
            _timer.Tick += new EventHandler(t_Tick);

            if (!Directory.Exists(_pDirectory))
            {
                Directory.CreateDirectory(_pDirectory);
                if(!Directory.Exists(bin))
                {
                    Directory.CreateDirectory(bin);
                }
            } else
            {
                _songs = Directory.GetFiles(bin);
            }

            wmp = new WindowsMediaPlayer();
            //MessageBox.Show(_pDirectory);
        }

        private void metroTile4_Click(object sender, EventArgs e)
        {
            if(_sIndex == (_songs.Length - 1))
            {
                _sIndex = 0;
            } else
            {
                _sIndex++;
            }

            playAudio(Path.Combine(_pDirectory, _songs[_sIndex]));
        }
    }
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • Well at the time of error, what is the value you are trying to set maximum to, and what currently is minimum set to – BugFinder Nov 14 '17 at 11:11
  • Just as I was trying to find the value by displaying it the error no longer occurred however, I don't want to show a MessageBox each time. –  Nov 14 '17 at 11:14
  • you can just put break points in, or, watches, or debug messages.. – BugFinder Nov 14 '17 at 11:15
  • Minimum is 0 and maximum is 100 when a break point is added –  Nov 14 '17 at 11:16

0 Answers0