|
| 1 | +--- |
| 2 | +title: Customizing AppointmentElement Text in RadScheduler |
| 3 | +description: Learn how to customize the background and text colors of AppointmentElement objects in RadScheduler for WinForms. |
| 4 | +type: how-to |
| 5 | +page_title: How to Customize AppointmentElement Text in RadScheduler |
| 6 | +slug: scheduler-customize-appointmentelement-text |
| 7 | +tags: scheduler, winforms, custom appointment element, background color, text color |
| 8 | +res_type: kb |
| 9 | +ticketid: 1663174 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +|Product Version|Product|Author| |
| 15 | +|----|----|----| |
| 16 | +|2024.3.806|RadGridView for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +Customizing the text in the appointment element is possible through the __AppointmentFormatting__ event of the RadScheduler control. Inside the event, you can pass an HTML text format and customize the text per your requirements. However, the time part inside the text is painted on the appointment. This way you can't directly access and customize the font size, color, etc. of the time part. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## Solution |
| 25 | + |
| 26 | +To customize the background and text colors of `AppointmentElement` time range text, a custom appointment element approach is required. |
| 27 | + |
| 28 | +### Implementing a Custom Appointment Element |
| 29 | + |
| 30 | +Create a custom appointment element by extending the `AppointmentElement` class. Override the `CreateAppointmentText` method to customize the text appearance, including the font and color of the time range: |
| 31 | + |
| 32 | +````C# |
| 33 | +public partial class CustomAppElement : AppointmentElement |
| 34 | +{ |
| 35 | + public CustomAppElement(RadScheduler scheduler, SchedulerView view, IEvent appointment) : base(scheduler, view, appointment) |
| 36 | + { |
| 37 | + } |
| 38 | + |
| 39 | + private Font dateTimeFont = new Font("Arial", 12); |
| 40 | + private Color dateTimeColor = Color.Green; |
| 41 | + |
| 42 | + protected override string CreateAppointmentText() |
| 43 | + { |
| 44 | + CultureInfo schedulerCulture = this.Scheduler.Culture; |
| 45 | + IEvent app = this.Appointment; |
| 46 | + string newLine = string.Empty; |
| 47 | + string separator = string.Empty; |
| 48 | + if (!string.IsNullOrEmpty(this.AppointmentLocation)) |
| 49 | + { |
| 50 | + if (this.Bounds.Height > 36) |
| 51 | + newLine = "<br/>"; |
| 52 | + else |
| 53 | + separator = ";"; |
| 54 | + } |
| 55 | + |
| 56 | + var titleFormatter = (new AppointmentTitleFormatterFactory()).Create(View.ViewType); |
| 57 | + object[] params = new object[] { |
| 58 | + this.View.DefaultTimeZone.OffsetTime(app.Start, this.Scheduler.SystemTimeZone), |
| 59 | + this.View.DefaultTimeZone.OffsetTime(app.End, this.Scheduler.SystemTimeZone), |
| 60 | + this.AppointmentSubject + separator, |
| 61 | + this.AppointmentLocation, |
| 62 | + "<span " + this.CreateStyleAttributeByFontAndColor() + ">", |
| 63 | + "</span>", |
| 64 | + "<b>", |
| 65 | + "</b>", |
| 66 | + newLine |
| 67 | + }; |
| 68 | + string topText = string.Format(schedulerCulture, titleFormatter.GetTitleFormat(this, this.View, this.Scheduler), params); |
| 69 | + return topText; |
| 70 | + } |
| 71 | + |
| 72 | + private string CreateStyleAttributeByFontAndColor() |
| 73 | + { |
| 74 | + StringBuilder styleBuilder = new StringBuilder(); |
| 75 | + styleBuilder.Append("style=\""); |
| 76 | + |
| 77 | + styleBuilder.Append("font-family: "); |
| 78 | + styleBuilder.Append(dateTimeFont.FontFamily.Name); |
| 79 | + styleBuilder.Append("; "); |
| 80 | + styleBuilder.Append("font-size: "); |
| 81 | + styleBuilder.Append(dateTimeFont.Size.ToString((IFormatProvider)NumberFormatInfo.InvariantInfo)); |
| 82 | + styleBuilder.Append("; "); |
| 83 | + styleBuilder.Append("color: #"); |
| 84 | + styleBuilder.Append(this.dateTimeColor.R.ToString("X2")); |
| 85 | + styleBuilder.Append(this.dateTimeColor.G.ToString("X2")); |
| 86 | + styleBuilder.Append(this.dateTimeColor.B.ToString("X2")); |
| 87 | + styleBuilder.Append("\""); |
| 88 | + return styleBuilder.ToString(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +```` |
| 93 | + |
| 94 | +### Applying the Custom Appointment Element |
| 95 | + |
| 96 | +Refer to the [Custom Appointment Element](https://docs.telerik.com/devtools/winforms/controls/scheduler/appointments-and-dialogs/custom-appointment-element) documentation to learn how to replace the default `AppointmentElement` with your custom implementation in the RadScheduler control. |
| 97 | + |
| 98 | +## See Also |
| 99 | + |
| 100 | +- [RadScheduler Overview](https://docs.telerik.com/devtools/winforms/controls/scheduler/overview) |
| 101 | +- [Appointment Formatting in RadScheduler](https://docs.telerik.com/devtools/winforms/controls/scheduler/appointments-and-dialogs/appointment-formatting) |
| 102 | +- [Custom Appointment Element Documentation](https://docs.telerik.com/devtools/winforms/controls/scheduler/appointments-and-dialogs/custom-appointment-element) |
0 commit comments