Milot Thaci
Milot Thaci

Reputation: 99

Edit buttons for listbox

My task is to create two edit buttons for a listbox, one edit-start button and one edit-end button, with relevant functionality.

The user should be able to edit a selected item on the list box after pressing the edit-start button. the change should then be saved after pressing edit-end.

Thanks for any input on this matter.

Upvotes: 0

Views: 587

Answers (1)

Mansur Kurtov
Mansur Kurtov

Reputation: 772

If your project is in c# WinForms, i recomended following solution: Add ListBox (name is MyListBox), Two Buttons(btnBeginEdit and btnEndEdit) and one edit component(MyTextBox) to your form; In form source you may use like this code:

      public Form1()
        {
            InitializeComponent();
            for (var i = 1; i <= 10; i++)
                MyListBox.Items.Add($"Item-{i}");
        }

        private void btnBeginEdit_Click(object sender, EventArgs e)
        {
            if (MyListBox.SelectedIndex == -1)
            {
                MessageBox.Show("Please select ListBox item firstly!");
                return;
            }

            var item = MyListBox.SelectedItem.ToString();
            MyTextBox.Text = item;
            MyListBox.Enabled = false;
        }

        private void btnEndEdit_Click(object sender, EventArgs e)
        {
            if (MyListBox.SelectedIndex == -1)
                return;
            MyListBox.Items[MyListBox.SelectedIndex] = MyTextBox.Text;            
            MyListBox.Enabled = true;            
        }

Upvotes: 1

Related Questions