Amit Kumar
Amit Kumar

Reputation: 1059

List box items go outside from listbox in window application

I am facing a UI issue in list box. The issue is very inconsistent sometimes it comes at first button and sometimes it occurs after many clicks or when I use scroll bar of listbox. I do not understand how and from where it is coming.

I am sorry to say that I can not define the issue but I am attaching a image of this issue to illustrate my problem:

enter image description here

I have done some code for selecting all items and deselect all items at button clicks. The code is given below:

private void btnSelectAll_Click(object sender, EventArgs e)
        {
            lstSelectRows.SelectionMode = SelectionMode.MultiSimple;
            for (int i = 0; i < lstSelectRows.Items.Count; i++)
            {
                lstSelectRows.SetSelected(i, true);
            }
        }

private void btnSelectNone_Click(object sender, EventArgs e)
        {
            this.lstSelectRows.SelectedIndex = -1;
        }

Upvotes: 3

Views: 346

Answers (1)

osvein
osvein

Reputation: 645

To avoid visual glitches like this, make sure pause drawing while the items are being updated.

Simply call ListBox.BeginUpdate() before you update the items, then call ListBox.EndUpdate() when you are done.

private void btnSelectAll_Click(object sender, EventArgs e)
        {
            lstSelectRows.BeginUpdate();
            lstSelectRows.SelectionMode = SelectionMode.MultiSimple;
            for (int i = 0; i < lstSelectRows.Items.Count; i++)
            {
                lstSelectRows.SetSelected(i, true);
            }
            lstSelectRows.EndUpdate();
        }

private void btnSelectNone_Click(object sender, EventArgs e)
        {
            lstSelectRows.BeginUpdate();
            this.lstSelectRows.SelectedIndex = -1;
            lstSelectRows.EndUpdate();
        }

Upvotes: 1

Related Questions