views:

115

answers:

5

Using Windows Mobile 6.5 and C#

The CharacterCasing property seems to be missing from WinMo 6.5 and I decide to just catch the textchanged event and set the text with ToUpper.

This works - but on every keypress, it sends the cursor back to the beginning of the string, not the end.

A: 

I figured it out. So on the textChanged event, I replace the entered text with the ToUpper version. Then I set the SelectionStart property to the Text.Length to move the cursor to the end.

Reena
+2  A: 

The soluttion of setting the text position to the end of the string seems like would be a hassle if you ever need to edit text that you have already entered.

It's been a while since I thought about the C# event model but, one alternative might be to catch the KeyPress event and change any lowercase KeyChar values to uppercase before passing them on to the next handler.

torak
+3  A: 

Ritu, just to comment on your answer. You should keep in mind that this might be confusing for a user if the user has positioned the carret in the middle of the string to perform some edit and then the carret jumps to the end of the string on the key press.

An alternative might be to change the text to upper case when the edit control looses focus.

Chris Taylor
I agree. I didn;t think of the case where they might put the cursor at the middle of the word.
Reena
+1  A: 

The way you are approaching seems to be wrong.
There are so many different ways to insert data into that textbox. What about copy & paste for instance?

Just perform a .Text.ToUpper() when accessing the value of the Textbox

citronas
A: 

Save the SelectionStart and SelectionLength before changing the text. The ToUpper should make no changes to the length, so you can simply set the SelctionStart and SelectionLength back to what they had been.

Also, I would expect to get a changed event again when you set it ToUpper. I'm not sure if you also need to check that the ToUpper actually changed anything before you set Text again. It may be smart enough to check that for you when you assign the text and avoiding giving you an infinite recursive loop of change events. However, you probably don't want to alter the selection in the event handler call for the case where you aren't making a further change, only in the outer call where you are assigning back to Text. So you might as well guard recursion directly.

Something like:

bool m_InMyTextChanged = false;  

private void txtMyText_TextChanged(object sender, EventArgs e)
{
    if (m_InMyTextChanged)
        return; // Recursive!  We can bail quickly.  

    m_InMyTextChanged = true; // Prevent recursion when we change it.
    int selectionStart = txtMyText.SelectionStart;
    int selectionLength = txtMyText.SelectionLength;
    string originalText = txtMyText.Text;
    string newText = originalText.ToUpper();
    if (newText != originalText)
    {
        txtMyText.Text = newText; // Will cause a new TextChanged event.
        // Set the selection back *after* the assignment, which has reset them.
        txtMyText.SelectionStart = selectionStart;
        txtMyText.SelectionLength = selectionLength;
    }
    m_InMyTextChanged = false; // Allow it for next time.
}

could work. I haven't worked in Windows Mobile, but I would think this would work the same as in general for .NET.

Rob Parker