ListControl doesn't scroll to new item

1 Answer 90 Views
ListControl
Marc Simkin
Top achievements
Rank 2
Iron
Veteran
Iron
Marc Simkin asked on 18 Jul 2021, 07:57 PM

Hi.

I have a listbox on a form.  The user enters a value in a text box, clicks a button, the value is added to the list.  All that works.  The button click also sets the selected item to be the just entered value and calls scrolltoitem to reposition the listbox.  The list box doesn't reposition.

Here is the code from the button click function:


        private void radButton1_Click(object sender, EventArgs e)
        {
            var item = radTextBox1.Text;
            if (string.IsNullOrWhiteSpace(item))
                return;

            // let's make sure this folder doesn't already exist in the list.
            if (radListControl1.Items.Contains(item))
                return;

            var newItem = new RadListDataItem(item, item);

            radListControl1.Items.Add(newItem);
            radListControl1.SelectedItem = newItem;

            radListControl1.ScrollToItem(newItem);
        }

I have also attached a working example.

Why doesn't this work as expected?

Thanks

-marc

 

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 19 Jul 2021, 01:35 PM

Hello, Marc,

The provided project is greatly appreciated. In order to make it work correctly it is appropriate to first set the SelectedItem property to the new item, then add the item in the Items collection:

private void radButton1_Click(object sender, EventArgs e)
{
    var item = radTextBox1.Text;
    if (string.IsNullOrWhiteSpace(item))
        return;

    // let's make sure this folder doesn't already exist in the list.
    if (radListControl1.Items.Contains(item))
        return;

    var newItem = new RadListDataItem(item, item);

    radListControl1.SelectedItem = newItem;
    radListControl1.Items.Add(newItem);

    radListControl1.ScrollToItem(newItem);
}

I hope this helps. Should you have any other questions do not hesitate to contact me. 

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
ListControl
Asked by
Marc Simkin
Top achievements
Rank 2
Iron
Veteran
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or