Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string.toUpper() 1

Status
Not open for further replies.

Eli20

Programmer
Oct 30, 2003
119
MX
hi, i have a windows.net application in c#, im trying to make it so when they user starts typing in a textbox the letter automaticly gets in upper case.

this was very easy to do in vb6.0, but i cannt find a way to do it, can anybody help me??

i created an event, and binded it to the keypress event of my textbox. but im lost now.

thank you very much!!

Eli

 
Hi,

You can extend the System.Windows.Forms.TextBox class that will give you a reusable control that always displays text in upper-case. Below is a sample of a user-control that overrides TextBox.

Code:
sing System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace UCaseTextBoxControl
{
   /// <summary> 
   /// A text box that only accepts upper-case text
   /// </summary>
	public class UCaseTextBox : System.Windows.Forms.TextBox
	{

		private System.ComponentModel.Container components = null;
      // member declarations
      [DllImport("user32")]
      public static extern int GetKeyboardState(ref byte KeyState);
      [DllImport("user32")]
      public static extern int SetKeyboardState(ref byte KeyState);
      private const int WM_KEYDOWN = 0x100;

      protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
      {
         try
         {
            Keys keyCode = (Keys)(msg.WParam.ToInt32() 
               & System.Convert.ToInt32(Keys.KeyCode));
            if (msg.Msg == WM_KEYDOWN)
            {
               // If the user is already shifting, 
               // no need to upper-case it... Also,
               // don't handle non alpha characters
               if (Control.ModifierKeys != Keys.Shift && 
                  ((int)keyCode >= 65 && (int)keyCode <= 90) 
                  || ((int)keyCode >= 97 && (int)keyCode <= 122))
               {
                  byte[] keyStates = new byte[255];
                  // Load the keyboard
                  GetKeyboardState(ref keyStates[0]);	      
                  // Turn off the lowercase key
                  keyStates[(int)keyCode] = 0;					
                  // Reset keyboard
                  SetKeyboardState(ref keyStates[0]);       
                  // Send new keystroke of of uppercase character
                  SendKeys.Send(keyCode.ToString().ToUpper());
                  return true;
               }
            }
         }
         catch (Exception ex)
         {
            System.Diagnostics.Debug.WriteLine(string.Format("Error in ProcessCmdKey(override) {0}", ex.ToString()));
         }
         finally 
         {

         }
         return base.ProcessCmdKey (ref msg, keyData);
      }

Certainly not as easy as VB6's way where you just handle the KeyPress event and make the KeyAscii variable the uppercase equivelent. But this way is more extensible :)

By the way, this is just a snippet of the UCaseTextBox class. To make your own, create a new Windows Control Library project, and rename the default class to UCaseTextBox or whatever you like. Then, in the class declaration, change the control to inherit from TextBox instead of UserControl. Finally, just add the ProcessCmdKey override code in the example and compile. Hope this helps you.
 
thank you JohnYingling thats exactly what i needed.. ;-)

Eli
 
How do you add the "Code" Blocks to your posts?

David Hexter
Business/Applications Analyst, DBA
 
You use a TGML markup tag: [ignore]
Code:
[/ignore].

Example:
[ignore]
[CODE]
Class HelloWorld()
{
   System.Windows.Forms.MessageBox.Show("Hello World!");
}
[/ignore]

appears as:
Code:
Class HelloWorld()
{
   System.Windows.Forms.MessageBox.Show("Hello World!");
}

More information on TGML tags can be found here
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top