using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace PhoneApp2 {
    public partial class MainPage : PhoneApplicationPage {

        private int _index;

        // Constructor
        public MainPage() {
            InitializeComponent();
            _index = +listBoxData.Items.Count;
        }

        private void Add_Click(object sender, EventArgs e) {
            listBoxData.Items.Insert(0, "text " + (_index++));
            listBoxData.SelectedIndex = 0;
        }

        private void Delete_Click(object sender, EventArgs e) {
            int index = listBoxData.SelectedIndex;
            if (index > -1) {
                listBoxData.Items.RemoveAt(index);
                listBoxData.SelectedIndex = Math.Min(index, listBoxData.Items.Count - 1);
            }
        }
    }
}