C#动态对象表列表框 - listbox1和动态列表框之间的拖放方法[n]

时间:2016-03-20 15:26:06

标签: c# dynamic methods drag-and-drop listbox

我遇到拖放效果问题。我想将选定的索引从listboxListaPrzedmiotow移动到动态分配的listboxA [n]。我没有任何想法解决在动态对象上使用方法的问题。请帮忙。

这是我的代码:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static decimal kolumny = 3;//numericUpDownKolumny.Value;
        static decimal wiersze = 4;//numericUpDownWiersze.Value;
        static int n = (int)kolumny * (int)wiersze;
        ListBox[] listboxA = new ListBox[n];

        public Form1()
        {
            InitializeComponent();
        }
        public void AddNewListBox()
        {
            int i = 0;


            for (int y = 0; y < wiersze; y++)
            {
                for (int x = 0; x < kolumny; x++)
                {
                    //ListBox[] listboxA = new ListBox();
                    listboxA[i] = new ListBox();
                    listboxA[i].Left = 9 + 126 * x;
                    listboxA[i].Top = 64 + 101 * y;
                    splitContainer1.Panel2.Controls.Add(listboxA[i]);
                    listboxA[i].AllowDrop = true;
                    i++;

                }
            }
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            AddNewListBox();     
        }
        private void listBoxListaPrzedmiotow_MouseDown(object sender, MouseEventArgs e)
        {
            listBoxListaPrzedmiotow.DoDragDrop(listBoxListaPrzedmiotow.SelectedIndex, DragDropEffects.Copy);
        }

        private void listBoxListaPrzedmiotow_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;

        }

        private void listBoxListaPrzedmiotow_DragDrop(object sender, DragEventArgs e)
        {
            listBoxListaPrzedmiotow.Items.Add(e.Data.GetData(DataFormats.Text));
        }

        private void listBoxA_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        private void listBoxA_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                string str = (string)e.Data.GetData(
                    DataFormats.StringFormat);
                for (int i=0; i < n; i++)
                    listboxA[i].Items.Add(str);
            }
        }
    }
}

0 个答案:

没有答案