This tutorial walks you through a real-world scenario that you can have in your application. At a high level, this scenario involves:
- Placing controls on different user controls.
- Docking the user controls inside RadDock.
- Implementing events, methods and properties in the user controls which allow you to have communication between the forms/user controls.
In our particular case we dock a user control instance in a RadDock instance. The user control itself contains a RadCalendar. When the user clicks the RadCalendar a custom DateChanged event for the UserControl1 fires, showing a dialog with the selected date. Using this as a model you can create UserControls with custom properties, methods and events for more complex situations.
- In Visual Studio create a new Windows Application.
- Create a new UserControl - from the Solution Explorer, right-click the project node in the Solution Explorer and select "Add >> UserControl...". Name the new user control CalendarPanel.
- Drop a RadCalendar instance on the CalendarPanel design surface.
- Set the RadCalendar Dock property to Fill.
- In the Property Window locate the SelectionChanged event and double-click it to create an event handler.
- Add a delegate and event to be surfaced by the CalendarPanel control when the date selection changes:
Copy[C#] Setting up DateChanged Event
public delegate void DateChangedHandler(DateTime date);
public event DateChangedHandler DateChanged;
Copy[VB.NET] Setting up DateChanged Event
Public Delegate Sub DateChangedHandler(ByVal [date] As DateTime)
Public Event DateChanged As DateChangedHandler
In the SelectionChanged event handler add the following code:
Copy[C#] Handling the RadCalendar SelectionChanged event
private void radCalendar1_SelectionChanged(object sender, EventArgs e)
{
if (DateChanged != null)
{
DateChanged((sender as Telerik.WinControls.UI.RadCalendar).SelectedDate);
}
}
Copy[VB.NET] Handling the RadCalendar SelectionChanged event
Private Sub RadCalendar1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadCalendar1.SelectionChanged
RaiseEvent DateChanged((TryCast(sender, RadCalendar)).SelectedDate)
End Sub - In the Solution Explorer, double-click the main form to open its design surface.
- Drop a RadDock instance on the opened design surface. Set the RadDock Dock property Fill.
- Dock the CalendarPanel user control to RadDock in the Form_Load event handler using the following code snippet:
Copy[C#] Docking CalendarPanel user control in RadDock
CalendarPanel calendarPanel = new CalendarPanel();
HostWindow host = this.radDock1.DockControl(calendarPanel, Telerik.WinControls.UI.Docking.DockPosition.Left);
host.Text = "Calendar";
calendarPanel.DateChanged += calendarPanel_DateChanged;
Copy[VB.NET] Docking CalendarPanel user control in RadDock
Dim calendarPanel As New CalendarPanel()
Dim host As HostWindow = Me.RadDock1.DockControl(calendarPanel, Telerik.WinControls.UI.Docking.DockPosition.Left)
host.Text = "Calendar"
AddHandler calendarPanel.DateChanged, AddressOf calendarPanel_DateChanged
- Add an event handler for the CalendarPanel DateChanged event:
Copy[C#] Handling the Custom DateChanged event
void calendarPanel_DateChanged(DateTime date)
{
RadMessageBox.Show("Selected date is: " + date.ToShortDateString());
}
Copy[VB.NET] Handling the Custom DateChanged event
Sub calendarPanel_DateChanged(ByVal [date] As DateTime)
RadMessageBox.Show("Selected date is: " + [date].ToShortDateString())
End Sub -
Press F5 to run the application. Click the cells in the calendar to display the date in a message dialog.