1

I am trying to set set the font of a TextBox in LightSwitch. I am not sure if there is a problem with my code, or if this just isn't possible.

The code executes, and I have stepped through it to make sure it executes, and all code is reached and executed, but there is not change to the controls on the screen.

My code is:

private void SetMono(string controlName)
        {
            var ctrl = this.FindControl(controlName);
            if (ctrl != null)
            {
                ctrl.ControlAvailable += (s, e) =>
                {
                    if (e.Control is TextBox)  // I put break point here to test.                        {
                        var tb = (TextBox)e.Control;
                        var ff = new System.Windows.Media.FontFamily("courierNew,courier,monospace");
                        tb.FontFamily = ff;
                    }
                };
            }
        }

Am I doing something wrong?

(I am using VS 2013)

JonathanPeel
  • 743
  • 1
  • 7
  • 19
  • To test I added `tb.FontSize = 20;` This works, and proves that the control is correct, and I am interacting with the right control. My problem must be with the FontFamily. – JonathanPeel Jun 12 '14 at 08:25
  • `Consolas` seemed to work, I copied from [here](http://stackoverflow.com/questions/4002290/setting-font-of-textbox-from-code-behind), it seems to be a monospace font, and it worked as a font family so it will work for me. – JonathanPeel Jun 12 '14 at 08:34

1 Answers1

0

The problem was my FontFamily. I could not find to much information on how the Font Family should be named, but "Consolas" did work.

Final Code:

private void SetMono(string controlName)
        {
            var ctrl = this.FindControl(controlName);
            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");
                    }
                };
            }
        }
JonathanPeel
  • 743
  • 1
  • 7
  • 19