none
[C#] listbox select next/previous

    Question

  • hi,

    i'm trying to create al little mediaplayer and as playlist i use a listbox. But i don't know how to create an option to select the next/previous item in the listbox by clicking a button. can someone help me?

    Saturday, October 15, 2011 7:06 PM

Answers

  • > i want the 'next' code to be used when the *.mp3 is finished.

     
    below is an example with playlist; files would play one after the other.
     

    using System;
    using System.Data;
    using System.IO;
    using System.Windows.Forms;
    using WMPLib;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            ListBox _ListBox;
            public Form1()
            {
                var ds = new DataSet();
                ds.ReadXml(new StringReader(@"
                    <list>
                        <item title='title1' url='C:\s1.wma' />
                        <item title='title2' url='C:\s2.wma' />
                    </list>"));
                ds.AcceptChanges();
    
                _ListBox = new ListBox();
                _ListBox.Parent = this;
                _ListBox.Dock = DockStyle.Fill;
                _ListBox.DisplayMember = "item.title";
                _ListBox.ValueMember = "item.url";
                _ListBox.DataSource = ds;
    
                var wmp = new WindowsMediaPlayer();
                wmp.PlayStateChange += value =>
                {
                    var state = (WMPLib.WMPPlayState)value;
                    if (state == WMPPlayState.wmppsStopped)
                    {
                        var cm = this.BindingContext[ds, "item"];
                        var p = cm.Position;
                        cm.Position++;
                        if (cm.Position > p)
                            this.BeginInvoke(new Action(() => this.Play(wmp)));
                    }
                };
                this.Menu = new MainMenu();
                this.Menu.MenuItems.Add("Play", (s, e) => this.Play(wmp));
            }
            void Play(WindowsMediaPlayer wmp)
            {
                wmp.URL = (string)_ListBox.SelectedValue;
                wmp.controls.play();
            }
        }
    }
    

    Tuesday, October 18, 2011 10:17 PM

All replies

  • Hi aquaticdevelopment,

    The following code should work I think:

    private void btnPrevious_Click(object sender, EventArgs e)
            {
                if (listBox1.SelectedIndex > 0)
                listBox1.SelectedIndex = listBox1.SelectedIndex - 1;
            }
    
            private void btnNext_Click(object sender, EventArgs e)
            {
                if (listBox1.SelectedIndex != listBox1.Items.Count - 1)
                listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
            }



    Tom Overton
    • Proposed as answer by Adnan Dedic Wednesday, October 19, 2011 12:11 AM
    Saturday, October 15, 2011 10:53 PM
  • That works,

    now i need the 'next' version for when the mediaplayer is finished. but the 'EndOfStream' event doesn't work.

    Should i use another event?

    Sunday, October 16, 2011 7:20 AM
  • Can you show us the code you have which causes the problem, or you would like to have?
    Mitja
    Sunday, October 16, 2011 7:49 AM
  • the code i tried was this:

     

            private void axWindowsMediaPlayer1_EndOfStream(object sender, AxWMPLib._WMPOCXEvents_EndOfStreamEvent e)
            {
                if (listBox1.SelectedIndex != listBox1.Items.Count - 1)
                {
                    listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
                }
            }
    


    Sunday, October 16, 2011 8:12 AM
  • Aquatic,

    Try to use AxWindowsMediaPlayer_PlayStateChange event instead.  I think it should fire when the song ends.

     private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
            {
                if (listBox1.SelectedIndex != listBox1.Items.Count - 1)
                {
                    listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
                }
            }
    




    Tom Overton
    Sunday, October 16, 2011 8:28 AM
  • that doesn't work, when i start a song it jumps to the last one.
    Sunday, October 16, 2011 9:21 AM
  • below is an example of playlist editor with following features: 1) load playlist from file; 2) move listbox selection by clicking a button 3) insert new item 4) save playlist to file.
    using System;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                var lb = new ListBox();
                lb.Parent = this;
                lb.Dock = DockStyle.Fill;
                this.Menu = new MainMenu();
                this.Menu.MenuItems.Add("Load", (s, e) =>
                {
                    lb.DataSource = LoadData();
                    lb.ValueMember = "item.title";
                });
                this.Menu.MenuItems.Add("Save", (s, e) => SaveData((DataSet)lb.DataSource));
                this.Menu.MenuItems.Add("Prev", (s, e) => lb.BindingContext[lb.DataSource, "item"].Position -= 1);
                this.Menu.MenuItems.Add("Next", (s, e) => lb.BindingContext[lb.DataSource, "item"].Position += 1);
                this.Menu.MenuItems.Add("Remove", (s, e) =>
                {
                    var cm = lb.BindingContext[lb.DataSource, "item"];
                    cm.RemoveAt(cm.Position);
                });
                this.Menu.MenuItems.Add("InsertNew", (s, e) =>
                {
                    var cm = lb.BindingContext[lb.DataSource, "item"];
                    var dt = ((DataSet)lb.DataSource).Tables["item"];
                    var dr = dt.NewRow();
                    dr.ItemArray = new [] {"song", "1234"};
                    dt.Rows.InsertAt(dr, cm.Position);
                    cm.Position -= 1;
                });
            }
            void SaveData(DataSet ds)
            {
                ds.WriteXml(_Path);
            }
            string _Path = @"c:\temp\playlist.xml";
            DataSet LoadData()
            {
                var ds = new DataSet();
                if (System.IO.File.Exists(_Path) == false)
                {
                    ds.ReadXml(new StringReader(@"<list>" +
                        String.Join("", Enumerable.Range(0, 20).Select(v => String.Concat("<item title='song", v, "' year='200", v, "' />"))) +
                        "</list>"));
                }
                else
                    ds.ReadXml(_Path);
                return ds;
            }
        }
    }
    

    Sunday, October 16, 2011 10:35 AM
  • that doesn't help, i want the 'next' code to be used when the *.mp3 is finished.

    EndOfStream event and PlaystateChange event both don't work.

    Tuesday, October 18, 2011 1:43 PM
  • > i want the 'next' code to be used when the *.mp3 is finished.

     
    below is an example with playlist; files would play one after the other.
     

    using System;
    using System.Data;
    using System.IO;
    using System.Windows.Forms;
    using WMPLib;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            ListBox _ListBox;
            public Form1()
            {
                var ds = new DataSet();
                ds.ReadXml(new StringReader(@"
                    <list>
                        <item title='title1' url='C:\s1.wma' />
                        <item title='title2' url='C:\s2.wma' />
                    </list>"));
                ds.AcceptChanges();
    
                _ListBox = new ListBox();
                _ListBox.Parent = this;
                _ListBox.Dock = DockStyle.Fill;
                _ListBox.DisplayMember = "item.title";
                _ListBox.ValueMember = "item.url";
                _ListBox.DataSource = ds;
    
                var wmp = new WindowsMediaPlayer();
                wmp.PlayStateChange += value =>
                {
                    var state = (WMPLib.WMPPlayState)value;
                    if (state == WMPPlayState.wmppsStopped)
                    {
                        var cm = this.BindingContext[ds, "item"];
                        var p = cm.Position;
                        cm.Position++;
                        if (cm.Position > p)
                            this.BeginInvoke(new Action(() => this.Play(wmp)));
                    }
                };
                this.Menu = new MainMenu();
                this.Menu.MenuItems.Add("Play", (s, e) => this.Play(wmp));
            }
            void Play(WindowsMediaPlayer wmp)
            {
                wmp.URL = (string)_ListBox.SelectedValue;
                wmp.controls.play();
            }
        }
    }
    

    Tuesday, October 18, 2011 10:17 PM