c# – How to implement an application-wide strategy to force text to upper case? – Education Career Blog

I am using .Net (C#, WinForms, Linq to SQL, SQL Server 2008) to develop a new line-of-business application that will replace a crufty old Foxpro application. I did not write the old app, but do provide support for it, and I am the in-house developer for the new app.

One of the design decisions that was made years ago with the Foxpro app was to force all entered text to be uppercase. This was to ensure that all text has a uniform format for sales reports, mailing lists, etc.

A current topic of conversation between me and some of the users, is whether this should be continued in the new app. It looks likely that it will be continued.

My question is, what is the best strategy to make this happen, involving the least amount of my time as possible?

It feels like a real drag to always have to remember to set the CharacterCasing property of every. single. textbox. that I drop onto a form. And it appears that DataGridViews don’t even have a simple property to set; you have manually handle CellChanged events, etc.

How would you do it?

,

I would suggest you to create your own User Control for TextBox, DataGridView and any other controls that you want to capitalize it.

That way, you can simply drag and drop your User Controls to any forms in your application.

Sample Code:

public partial class CapTextBox : UserControl
{
    public CapTextBox()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text.ToUpper();
        textBox1.SelectionStart = textBox1.Text.Length;
    }
}

,

Personally,
I would build an assembly containing all the application needed controls (TextBoxes, DataGridViews etc.) overridden/customized to get the desired result (uppercase text in this case).

In this way, every developer would have just to use the customized controls, instead of .NET ones and change properties one by one.
Moreover you would have a centralized point to control application UI behaviour.

DataGridView Example:

public class UpperCaseDataGrid : DataGridView
{
    protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
    {
        var txtBox = EditingControl as TextBox;
        if (txtBox != null)
            txtBox.CharacterCasing = CharacterCasing.Upper;

        base.OnEditingControlShowing(e);
    }
}

,

Thanks to all who contributed. I thought digEmAll in particular had the best answer. However, I am posting my full implementation as the answer, in order to benefit others who find this questions.

UpperCaseTextBox implementation:

public class UpperCaseTextBox : TextBox
{
    public UpperCaseTextBox()
        : base()
    {
        base.CharacterCasing = this.CharacterCasing;
    }

    private CharacterCasing _characterCasing = CharacterCasing.Upper;
    DefaultValue(CharacterCasing.Upper)
    public new CharacterCasing CharacterCasing
    {
        get
        {
            return _characterCasing;
        }
        set
        {
            base.CharacterCasing = value;
            _characterCasing = value;
        }
    }
}

UpperCaseDataGridView implementation:

public class UpperCaseDataGridView : DataGridView
{
    private CharacterCasing _textBoxCharacterCasing = CharacterCasing.Upper;
    CategoryAttribute("Behavior")
    DescriptionAttribute("Sets CharacterCasing of all contained TextBox controls.")
    DefaultValue(CharacterCasing.Upper)
    public CharacterCasing TextBoxCharacterCasing
    {
        get
        {
            return _textBoxCharacterCasing;
        }
        set
        {
            _textBoxCharacterCasing = value;
        }
    }

    protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
    {
        var txtBox = EditingControl as TextBox;
        if (txtBox != null)
            txtBox.CharacterCasing = this.TextBoxCharacterCasing;

        base.OnEditingControlShowing(e);
    }
}

One could implement this functionality using less code than the above, however, I purposely aimed for a flexible, robust implementation that plays nice with the Properties window, allows me to override the uppercasing in instances where needed without having to tear out the control and replace with vanilla control, etc.

Thanks again to all who contributed.

Leave a Comment