Search code examples
visual-studio-lightswitch

Where do I place the code in lightswitch to set the font family to Consolas?


I want to change the font type of a multiple line enabled textbox control of a lightswitch application. There is a previous question on how to do this. However, I'm not certain WHERE to place that code.

The reference question that was previously answered is:

Setting Font in Lightswitch

However, I'm just not a c# programmer and am sloppily hacking my way through this. I've currently placed the code I copied here in a file called ClinicMessagesListDetail.lsml.cs: (however, it doesn't seem to help or hurt the program)

using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Windows.Controls;
namespace LightSwitchApplication
{
    public partial class ClinicMessagesListDetail
    {
        private void SetMono(string Message)
        {
            var ctrl = this.FindControl(Message);
            if (ctrl != null)
            {
                ctrl.ControlAvailable += (s, e) =>
                {
                    if (e.Control is TextBox)
                    {
                        var tb = (TextBox)e.Control;
                        tb.FontFamily = new System.Windows.Media.FontFamily("Consolas");
                    }
                };
            }
        }
    }
}

This is the first time I've posted here and hope that I'm following all the guidelines.

Thank you Dave S


Solution

  • As it appears that you want to implement this on a Silverlight client screen, you're heading in the right direction.

    In addition to the SetMono method and associated using directive (for System.Windows.Controls) that you've already added to your screen's class module (ClinicMessagesListDetail.lsml.cs) you'll also need to call the SetMono method from the screen's created routine.

    This created routine should be introduced by clicking on the 'ClinicMessagesListDetail_Created' option found under the 'Write Code' menu (at the top of the LightSwitch screen designer).

    Once introduced, this empty created routine should be extended with the following code to call your SetMono method (replacing NameOfYourTextBox with the name of your control):

    partial void ClinicMessagesListDetail_Created()
    {
        // Write your code here.
        SetMono("NameOfYourTextBox");
    }
    

    If, on the other hand, you're wanting to implement the font change on a HTML client screen you'll need to take a substantially different approach (please update your question if this is the case).