Boss: “George, I want you to use a good looking ScrollableButtonList here. And make that for yesterday – we are getting behind schedule already!”
George: “What, for yesterday? It will take me at least a week! Come on, boss, you know how coding is!”
Boss: “Do it, or you will be looking for a new job!”
George (walking towards his office): “Oh maan, what am I gonna do?”
George (thinking again): “Of course! I will use the Telerik WinForms Framework! I bet the ScrollableButtonList will be ready in minutes… ”

This may sound bizarre, but it’s not! With the wide range of predefined elements that Telerik Presentation Framework provides, you can create your custom controls without much effort, and still benefit from the Theming mechanism and the unique Telerik visual appearance.

scrollableBListCheckBoxes scrollableBListToggleButtons

To start off, the ScrollableButtonList can be either a CheckedListBox or a ToggleButtonsListBox. First, you need to create a new RadScrollViewer and a StackLayoutPanel:

RadScrollViewer scrollViewer = new RadScrollViewer();
StackLayoutPanel stackLayout = new StackLayoutPanel();

The Viewport property should be set to the StackLayoutPanel instance. Since this will be a vertical ListBox, we will set the Orientation property of the StackLayoutPanel to Vertical:

stackLayout.Orientation = Orientation.Vertical;
scrollViewer.Viewport = stackLayout;

The RadScrollViewer instance itself will be hosted by the RadPanel control:

this.radPanel1.PanelElement.Children.Add(scrollViewer);
this.radPanel1.Text = String.Empty;

 

The final thing that we should do is to add RadCheckBoxElements to the StackLayoutPanel. That’s all for the CheckedListBox. Alternatively, we can add RadToggleButtonElements and we will have a ToggleButtonsListBox:
         

for (int i = 0; i < 100; i++) 
{ 
   
RadCheckBoxElement checkBoxElement = new RadCheckBoxElement();
   
checkBoxElement.Text = "Test" + i;
   
checkBoxElement.Padding = new Padding(5);
   
checkBoxElement.Margin = new Padding(2);
   
stackLayout.Children.Add(checkBoxElement);
   
// or 
    //RadToggleButtonElement checkBoxElement = new RadToggleButtonElement();
    //checkBoxElement.Text = "Test" + i;
    //checkBoxElement.Padding = new Padding(5);
    //checkBoxElement.Margin = new Padding(2);
    //stackLayout.Children.Add(checkBoxElement);
}

Of course, you can add some custom spices such as events and custom collections to your new control. However, as we saw, the foundation is easy to be made.

Here is the complete code snippet:

private void Form1_Load(object sender, EventArgs e)
{
   
RadScrollViewer scrollViewer = new RadScrollViewer();
   
StackLayoutPanel stackLayout = new StackLayoutPanel();

   
stackLayout.Orientation = Orientation.Vertical;
   
scrollViewer.Viewport = stackLayout;

   
this.radPanel1.PanelElement.Children.Add(scrollViewer);
    this.radPanel1.Text = String.Empty;

   
for(int i = 0 ; i < 100; i++)
   
{
       
RadCheckBoxElement checkBoxElement = new RadCheckBoxElement();
       
checkBoxElement.Text = "Test" + i;
       
checkBoxElement.Padding = new Padding(5);
       
checkBoxElement.Margin = new Padding(2);
       
stackLayout.Children.Add(checkBoxElement);
       
// or 
        //RadToggleButtonElement checkBoxElement = new RadToggleButtonElement();
        //checkBoxElement.Text = "Test" + i;
        //checkBoxElement.Padding = new Padding(5);
        //checkBoxElement.Margin = new Padding(2);
        //stackLayout.Children.Add(checkBoxElement);
    }
}

So, how long did it take for you to read this story? 5-6 minutes? While you were reading, George showed the implementation to his boss, and he is once again put for promotion :)

~END~


Related Posts

Comments