Setting font in light switch textboxes

2019-07-27 03:31发布

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)

1条回答
姐就是有狂的资本
2楼-- · 2019-07-27 03:35

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");
                    }
                };
            }
        }
查看更多
登录 后发表回答