Getting Started with WinForms SplitButton
You can add ____ either at design time or at run time:
Design Time
- To add a RadSplitButton to your form, drag a RadSplitButton from the toolbox onto the surface of the form designer.
- Like a standard button, you can control the displayed text by setting the Text property.
- Unlike a standard button, RadSplitButton displays drop-down items when clicked. So handling the Click event of this button is not appropriate. Instead, work directly with the events for each item.
Run Time
To programmatically add a RadSplitButton to a form, create a new instance of a RadSplitButton, and add it to the form Controls collection.
Adding a RadSplitButton at runtime
RadSplitButton splitButton = new RadSplitButton();
splitButton.Text = "Fruits";
RadMenuItem item1 = new RadMenuItem("Orange");
splitButton.Items.Add(item1);
RadMenuItem item2 = new RadMenuItem("Lemon");
splitButton.Items.Add(item2);
RadMenuItem item3 = new RadMenuItem("Banana");
splitButton.Items.Add(item3);
this.Controls.Add(splitButton);
Dim splitButton As New RadSplitButton()
splitButton.Text = "Fruits"
Dim item1 As New RadMenuItem("Orange")
splitButton.Items.Add(item1)
Dim item2 As New RadMenuItem("Lemon")
splitButton.Items.Add(item2)
Dim item3 As New RadMenuItem("Banana")
splitButton.Items.Add(item3)
Me.Controls.Add(splitButton)
Working with RadSplitButton Items
The heart of RadSplitButton is the Items collection. This collection defines the menu items that appear when the RadSplitButton is clicked. There are two ways to add items to a RadSplitButton.
Adding Items at Design-time in the UI
To add menu items at design-time, please refer to the Design Time help article.
Adding Items at Run Time in Code
You can also add items to RadSplitButton in code at run time. The following example code illustrates programmatically adding a RadMenuItem to your button.
Adding a RadMenuItem
private void Form1_Load(object sender, EventArgs e)
{
RadMenuItem myRadMenuItem = new RadMenuItem();
myRadMenuItem.Text = "My New Item";
myRadMenuItem.Click += new EventHandler(myRadMenuItem_Click);
radSplitButton1.Items.Add(myRadMenuItem);
}
void myRadMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show((sender as RadMenuItem).Text);
}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myRadMenuItem As New RadMenuItem()
myRadMenuItem.Text = "My New Item"
AddHandler myRadMenuItem.Click, AddressOf myRadMenuItem_Click
radSplitButton1.Items.Add(myRadMenuItem)
End Sub
Sub myRadMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show((TryCast(sender, RadMenuItem)).Text)
End Sub
Create item hierarchies in code by adding new RadMenuItem objects to the Items collection of your existing RadMenuItem.
Adding a sub item
private void Form1_Load(object sender, EventArgs e)
{
radSplitButton1.Items.Add(new RadMenuItem("AAA"));
RadMenuItem mySubMenuItem = new RadMenuItem();
mySubMenuItem.Text = "Submenu Item";
mySubMenuItem.Click += new EventHandler(mySubMenuItem_Click);
RadMenuItem mainItem = radSplitButton1.Items[0] as RadMenuItem;
mainItem.Items.Add(mySubMenuItem);
}
void mySubMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show((sender as RadMenuItem).Text);
}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mySubMenuItem As New RadMenuItem()
mySubMenuItem.Text = "Submenu Item"
AddHandler mySubMenuItem.Click, AddressOf mySubMenuItem_Click
Dim mainItem As RadMenuItem = TryCast(radSplitButton1.Items(0), RadMenuItem)
mainItem.Items.Add(mySubMenuItem)
End Sub
Sub mySubMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show((TryCast(sender, RadMenuItem)).Text)
End Sub
Displaying Images with Items
You can display images as well as text on your menu items.
To add an image to your menu item, click in the Image property of the RadMenuItem, and then click the ellipsis button to launch the Select Resource dialog.
From this dialog you can select an image file from a project resource file or from an image resource on your local hard drive.
Using the Click Event
To handle the Click event of RadMenuItems on the drop down menu, locate the RadMenuItem in the drop down list in the Properties window of the Windows Form designer. Click the events button, then double-click the Click event to generate an event handler. Fill in the details of your event-handling code.
Setting the Default Item
The default item is the item whose Click event is triggered by the user pressing the button, instead of choosing from the menu. The DefaultItem can be assigned in code:
Assigning the default item
radSplitButton1.DefaultItem = mainItem;
radSplitButton1.DefaultItem = mainItem
Telerik UI for WinForms Learning Resources
- Telerik UI for WinForms SplitButton Component
- Getting Started with Telerik UI for WinForms Components
- Telerik UI for WinForms Setup
- Telerik UI for WinForms Application Modernization
- Telerik UI for WinForms Visual Studio Templates
- Deploy Telerik UI for WinForms Applications
- Telerik UI for WinForms Virtual Classroom(Training Courses for Registered Users)
- Telerik UI for WinForms License Agreement)