C# .NET - how to make backspace working?

Asked By Priyanka
06-Sep-10 01:01 AM

if (char.IsNumber(e.KeyChar) == false)

e.Handled = true;


i tried the above code on key press event for text box but backspace key is not working in it ... how to make it working?
  harsh shah replied to Priyanka
06-Sep-10 01:13 AM
Hi,
try the below code


if (e.KeyChar == '\b')
   {
     e.Handled = false;
   }


let me know

Regards,

Harsh Shah
  Priyanka replied to harsh shah
06-Sep-10 01:56 AM
 it is not working ...
 it also accepts  both type of values numeric and char.....

i want that text box donot accept char values but backspace should work in that text box...
  harsh shah replied to Priyanka
06-Sep-10 02:05 AM
ok
given code is only for back space.
any way try below code it's accept only numeric and backspace.


if (e.KeyChar == '0' || e.KeyChar == '1' || e.KeyChar == '2' || e.KeyChar == '3' || e.KeyChar == '4' || e.KeyChar == '5' || e.KeyChar == '6' || e.KeyChar == '7' || e.KeyChar == '8' || e.KeyChar == '9' || e.KeyChar == '\b')
    {
     e.Handled = false;
    }
    else
   {
        e.Handled = True;
   }

Regards,

Harsh Shah
  George replied to harsh shah
06-Sep-10 02:11 AM
Thanks from bottom of my heart.....
  Priyanka replied to harsh shah
06-Sep-10 02:17 AM
Thank you so very much....
  Anand Malli replied to Priyanka
06-Sep-10 02:21 AM
Hi Priyanka

I have created one small function for you,its completely different approch,there more then one way with which you can achieve this,i have developed one function which is as follows,just let me know,it is something which you want,just create KeyPress event of your textbox like following

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
   int[] numArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
   char bckSpace = '\b';
   int isInt = 0;
   bool blnIsInt = int.TryParse(e.KeyChar.ToString(), out isInt);
   e.Handled = (isInt != 0 || e.KeyChar == '\b') ? false : true;
          
}

i think code is self explanatory,if you have any doubt just let me know

THANKS 
  Priyanka replied to Anand Malli
06-Sep-10 02:29 AM
Thanks you so very much ... it is working properly......
  Anand Malli replied to Priyanka
06-Sep-10 02:44 AM
:)...its always good idea to discover new hacks...isnt it...gr8...
  bhagavan replied to Priyanka
22-Jul-11 02:22 AM
 if ((e.KeyChar < '0') || (e.KeyChar > '9'))
        {
          if (e.KeyChar != '\b')
          {
            e.Handled = true;
          }
         
        }




It will work
Create New Account
help
int StringLength; private int CurserLocation; private int KeyPressCount; private string NewValue; private string PreviousValue; private bool NewTextChange; / / Boolean variables to keep the status of the ability of using keys. private bool NumbersAllowed; private bool CharactersAllowed; private bool CommaAllowed; private bool DecimalPointAllowed; private bool BackSlashAllowed; / / "Back Slash is \ " private bool EqualMarkAllowed; private bool AddMarkAllowed; private bool QuestionMarkAllowed; private bool ExclamationMarkAllowed; private bool AtMarkAllowed; / / "At Mart is @" private bool
DXUTD3D10DeviceSettings { UINT AdapterOrdinal; D3D10_DRIVER_TYPE DriverType; UINT Output; DXGI_SWAP_CHAIN_DESC sd; UINT32 CreateFlags; UINT32 SyncInterval; DWORD PresentFlags; bool AutoCreateDepthStencil; / / DXUT will create the a depth stencil resource and view if true DXGI_FORMAT AutoDepthStencilFormat void (CALLBACK *LPDXUTCALLBACKFRAMEMOVE)( double fTime, float fElapsedTime, void * pUserContext ); typedef void (CALLBACK *LPDXUTCALLBACKKEYBOARD)( UINT nChar, bool bKeyDown, bool bAltDown, void * pUserContext ); typedef void (CALLBACK *LPDXUTCALLBACKMOUSE)( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos, void * pUserContext ); typedef LRESULT (CALLBACK *LPDXUTCALLBACKMSGPROC)( HWND hWnd
using NAV.Reports.UIL.MainGroupProcess; namespace NAV.Reports.UIL.CustomControls { public class DGVExtended : DataGridView { private bool _IsCellWrap = true ; private bool _IsBackColorDymainc = true ; private bool _IsForeColorDymainc = true ; private bool _isShowDateAsShortFormat = false ; / / / <summary> / / / Is backcolor of the merged cells same as backcolor of owing row set true) or it should be White always(set false). / / / < / summary> public bool IsBackColorDymainc { get { return _IsBackColorDymainc; } set { _IsBackColorDymainc = value ; } } / / / <summary> / / / Is forecolor of the merged cells same of owing row(set true) or it should be White always(set false). / / / < / summary> public bool IsForeColorDymainc { get { return _IsForeColorDymainc; } set { _IsForeColorDymainc = value ; } } public bool IsShowDateAsShortFormat { get { return _isShowDateAsShortFormat; } set { _isShowDateAsShortFormat = value ; } } / / / <summary> / / / Whether the cell contents are to be wrapped or not / / / < / summary> public bool IsCellWrap { get { return _IsCellWrap; } set { _IsCellWrap = value ; } } DataGridView . HitTestInfo hitTestInfo; private ContextMenu contextMenuForCopy = new ContextMenu
Linq.Expressions; using System.Collections.Generic; public static class PredicateBuilder { public static Expression<Func<T, bool> > True<T> () { return f = > true; } public static Expression<Func<T, bool> > False<T> () { return f = > false; } public static Expression<Func<T, bool> > Or<T> (this Expression<Func<T, bool> > expr1, Expression<Func<T bool> > expr2) { var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ()); return Expression.Lambda<Func<T, bool> > (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters); } public static Expression<Func<T, bool> > And<T> (this Expression<Func<T, bool> > expr1, Expression<Func<T, bool> > expr2) { var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression