# Implementing 'moveTop' and 'moveBottom' Custom Tools

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC ListBox</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>Created with the 2023.3.1010 version</td>
 </tr>
</table>

## Description

How can I create custom tools (*moveTop* and *moveBottom*) that move the selected option at the top or the bottom of all ListBox options?

## Solution

The ListBox supports [`MoveUp()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/listboxtoolfactory#moveup) and [`MoveDown()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/listboxtoolfactory#movedown) that move up and down the selected option. To create tools that move the option to the top or the bottom of the options list, follow the steps below:

1. Create two buttons and handle their [`Click`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/buttoneventbuilder#clicksystemstring) events.
2. Get a reference to the ListBox and use the client-side API methods [`select()`](https://docs.telerik.com/kendo-ui/api/ui/listbox/methods/select) and [`reorder()`](https://docs.telerik.com/kendo-ui/api/ui/listbox/methods/reorder) to get the selected item and move it to the respective position.
3. To render the "moveTop" and "moveBottom" buttons within the toolbar, handle the `DataBound` event of the ListBox and use the jQuery [`append()`](https://api.jquery.com/append/) method to append each button in the `k-listbox-actions` element.

   ```Razor HtmlHelper
     @(Html.Kendo().ListBox()
           .Name("listBox")
           .Toolbar(toolbar =>
           {
               toolbar.Position(ListBoxToolbarPosition.Right);
               toolbar.Tools(tools => tools
                   .MoveUp()
                   .MoveDown()
                   .Remove()
               );
           })
           .Events(ev => ev.DataBound("onDataBound"))
           ...
       )

       @(Html.Kendo().Button()
           .Name("moveToTop")
           .Icon("chevron-double-up")
           .Content("<span class='k-icon' title='Move Top'></span>")
           .Events(ev => ev.Click("onClickTop"))
       )

       @(Html.Kendo().Button()
           .Name("moveToBottom")
           .Icon("chevron-double-down")
           .Content("<span class='k-icon' title='Move Bottom'></span>")
           .Events(ev => ev.Click("onClickBottom"))
       )
   ```

   ```JS script
       <script>
           function onClickTop() {
               var listBox = $("#listBox").data("kendoListBox");
               var selectedItems = listBox.select(); // Get the selected item element.

               if (selectedItems.length > 0) {
                   listBox.reorder(selectedItems[0], 0); // Move it at position 1 (index 0).
               }
           }

           function onClickBottom() {
               var listBox = $("#listBox").data("kendoListBox");
               var selectedItems = listBox.select(); // Get the selected item element.
               var items = listBox.items(); // Get all ListBox items.

               if (selectedItems.length > 0) {
                   listBox.reorder(selectedItems[0], (items.length - 1)); // Move it at the last position.
               }
           }

           function onDataBound() {
               $(".k-listbox-actions").append($("#moveToTop"));
               $(".k-listbox-actions").append($("#moveToBottom"));
           }
       </script>
   ```

For a runnable example based on the code above, refer to the [REPL example on creating "moveTop" and "moveBottom" custom tools for the ListBox](https://netcorerepl.telerik.com/wdvOwZkB33pJMjAn16).

## More ASP.NET MVC ListBox Resources

* [ASP.NET MVC ListBox Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/listbox/overview)

* [ASP.NET MVC ListBox Demos](https://demos.telerik.com/aspnet-mvc/listbox/index)

* [ASP.NET MVC ListBox Product Page](https://www.telerik.com/aspnet-mvc/listbox)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the ListBox for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/listbox)

* [Server-Side API Reference of the ListBox for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/listbox)

* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
