Thursday, January 7, 2016

C# How to move Listbox item up and down In Urdu (Video 5)



Project Code

using System;
using System.Windows.Forms;

namespace ListBoxUpAndDownDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void UpPictureBox_Click(object sender, EventArgs e)
        {
            if (CityListBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select an item to move", "No Item selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                int newIndex = CityListBox.SelectedIndex - 1;

                if (newIndex < 0 || newIndex >= CityListBox.Items.Count)
                    return;

                object selected = CityListBox.SelectedItem;

                CityListBox.Items.Remove(selected);

                CityListBox.Items.Insert(newIndex, selected);

                CityListBox.SetSelected(newIndex, true);
            }
        }

        private void DownPictureBox_Click(object sender, EventArgs e)
        {
            if (CityListBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select an item to move", "No Item selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                int newIndex = CityListBox.SelectedIndex + 1;

                if (newIndex < 0 || newIndex >= CityListBox.Items.Count)
                    return;

                object selected = CityListBox.SelectedItem;

                CityListBox.Items.Remove(selected);

                CityListBox.Items.Insert(newIndex, selected);

                CityListBox.SetSelected(newIndex, true);
            }
        }
    }
}

No comments:

Post a Comment