List Box Item içindeki elemanları aşağı veya yukarı taşımak

Bu fonksiyonu kullanabilirsiniz.

Aşağı ve yukarı butonları şekildeki gibi çağırabilirsiniz. Buttondown ilgili ögeyi aşağıya indirecektir. ButtonUp ilgili ögeyi yukarı taşıyacaktır.

 protected void ButtonDown_Click(object sender, EventArgs e)
    {
        MoveListboxItem(1, ListBox2);
    }
    protected void ButtonUp_Click(object sender, EventArgs e)
    {
        MoveListboxItem(-1, ListBox2);
    }

private void MoveListboxItem(int index, ListBox listBox)
    {
        if (listBox.SelectedIndex != -1) //is there an item selected?
        {
            //if it's moving up, the loop moves from first to last, otherwise, it moves from last to first
            for (int i = (index < 0 ? 0 : listBox.Items.Count - 1); index < 0 ? i < listBox.Items.Count : i > -1; i -= index)
            {
                if (listBox.Items[i].Selected)
                {
                    //if it's moving up, it should not be the first item, or, if it's moving down, it should not be the last
                    if ((index < 0 && i > 0) || (index > 0 && i < listBox.Items.Count - 1))
                    {
                        //if it's moving up, the previous item should not be selected, or, if it's moving down, the following item should not be selected
                        if ((index < 0 && !listBox.Items[i - 1].Selected) || (index > 0 && !listBox.Items[i + 1].Selected))
                        {
                            ListItem itemA = listBox.Items[i]; //the selected item

                            listBox.Items.Remove(itemA); //is removed

                            listBox.Items.Insert(i + index, itemA);//and swapped
                        }
                    }
                }
            }
        }
    }

Yorumlar

Bu blogdaki popüler yayınlar

22.06.2020 - 26.06.2020 arası işler

Asp.net RestSharp ile data post etmek