using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OliviaAddIn
{
    public partial class ListForm : Form
    {
        private bool selec = false;
        public int nelem = 0;
        public string[] elem_str=null;
        public int[] elem_id = null;

        public ListForm(string[] elems, string title, bool multiselec)
        {
            InitializeComponent();
            listBox.Items.Clear();
            listBox.Items.AddRange(elems);
            selec = false;
            Text = title;
            if (multiselec)
                listBox.SelectionMode = SelectionMode.MultiExtended;
            else
                listBox.SelectionMode = SelectionMode.One;

            ShowDialog();

        }

        private void ListForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!selec)
            {
                nelem = 0;
                elem_str = null;
                elem_id = null;
            }
        }

        private void listBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            selec_item();
        }

        private void bt_aceptar_Click(object sender, EventArgs e)
        {
            selec_item();
        }

        void selec_item()
        {
            ListBox.SelectedIndexCollection selindexs = listBox.SelectedIndices;

            nelem = selindexs.Count;
            if (nelem>0)
            {
                selec = true;
                elem_id = new int[nelem];
                for (int i = 0; i < nelem; i++)
                    elem_id[i] = selindexs[i];

                ListBox.SelectedObjectCollection selitems = listBox.SelectedItems;
                elem_str = new string[nelem];
                for (int i = 0; i < nelem; i++)
                {
                    elem_str[i] = (string)selitems[i];
                }

                Close();
            }
        }
    }
}
