[dba-VB] ControlChars in C#

Gustav Brock Gustav at cactus.dk
Tue Mar 25 13:12:12 CDT 2008


Hi Shamil

Oh, that is beautiful! I was aware that you can mix the languages but not down to this level.

Now I can write code like this:

        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            char c = e.KeyChar;
            if (Char.IsControl(c))
            {
                if (c != ControlChars.Back)
                {
                    e.Handled = true;
                }
            }
        }

which even JC can read in a snap! 

Seriously, I'm old enough to remember ASCII 8 to be a backspace but I'm also old enough to be in doubt, and if I expect someone else to read this code it would require one line of comment like:

    // Check that char c is not a backspace (ASCII 8).

Visual Studio shines.

/gustav


>>> shamil at users.mns.ru 25-03-2008 17:53 >>>
Hi Gustav,

In C# project you can set the reference to Microsoft.VisualBasic assembly,
then write:


using Microsoft.VisualBasic;

....

        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
if (e.KeyChar != ControlChars.Back) e.Handled = true
}

BTW, unlike Visual Basic this "screwed" above code lines will be compiled
well if you'll copy & paste them: I mean while posting Visual basic sample
one have to be careful with line wraps etc.

C# rules! Visual Basic serves :)  (a kind of kidding...)

Thanks.

--
Shamil
 

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Tuesday, March 25, 2008 5:56 PM
To: dba-vb at databaseadvisors.com 
Subject: [dba-VB] ControlChars in C#

Hi all

I can't believe this:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.keypresseventargs(VS.71).aspx

In Visual Basic you have a nice class ControlChars where you, for example,
ControlChars.Back for the BackSpace character.
In C# you have to write ugly (char)8 or '\b' in stone age C++ style.

So I have to write all this code to avoid the beep after pressing Enter in
the small navigator position field of the record navigation toolbar while
still be able to edit the record number with both the Delete and the
BackSpace keys:

        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            char c = e.KeyChar;
            if (Char.IsControl(c))
            {
                if (c != (char)8) 
                {
                    e.Handled = true;
                }
            }
        }

What do you do? Live with the beep or run your own homebuilt ControlChars?

/gustav






More information about the dba-VB mailing list