Click here to Skip to main content
15,115,261 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
I am creating a calculator using c#, I just want that my backspace should work on textBoxes.
I have a button that should performs functionality of backspace.
Posted
Updated 27-Mar-11 19:30pm
v2
Comments
[no name] 28-Mar-11 0:18am
   
What do you mean backsapce does not work, would you please clear this
[no name] 28-Mar-11 0:31am
   
Thats good its easy now..

Answering a follow-up Question:

This is one of the ways to emulate backspace:

C#
myButton.Click += (sender, eventArgs) => {
    myTextBox.Focus();
    Application.DoEvents();
    SendKeys.Send("{BACKSPACE}");
};


Warning! Avoid using Application.DoEvents for other cases; one common mistake is doing it periodically as a dirty substitute of threading.

A variant without SendKeys:

C#
myButton.Click += (sender, eventArgs) => {
int currentPosition = myTextBox.SelectionStart;
if (currentPosition > 0)
    myTextBox.Text = myTextBox.Text.Remove(currentPosition - 1, 1);
};


—SA
   
v2
Comments
[no name] 28-Mar-11 2:05am
   
Really Good Sample.
Have My 5
   
Thank you very much.
Importantly, it provides required functionality.
--SA
Bhavna v 29-Mar-11 1:18am
   
Thank for solution.
   
You are welcome.
--SA
I hope you are handling backspace using the System.Windows.Forms.Keys.Back keycode.
   
v2
Comments
   
A useful hint, my 5. (I fixed the ID to make it compile, would you mind?).
--SA
Abhinav S 28-Mar-11 0:52am
   
Oh no not at all. Thanks for the 5.
   
She accepted the completely incorrect Answer. This is useless...
--SA
m@dhu 28-Mar-11 0:50am
   
If you could look at her profile and the comments you will know it why?
   
I did. She and Brijesh work together to mess up most simple things. Being not experienced is one thing, moving in wrong direction is something else... And not listening... Useless, I say...
--SA
   
I put another Answer, after clarification.
--SA
Abhinav S 28-Mar-11 0:52am
   
Weird. [doh]
   
The story is being continuing, unfortunately...
--SA
   
I had to put another Question, after clarification.
--SA
Backspace character does work on all text boxes unless you handled some event to prevent that.
You need to show a fragment of you code to get further help.

Please see my Answer to your other Question (How to save 2 values in a variable[^]) and thing thoroughly: you need to select much easier coding tasks as you only start programming and still have to learn the very basic idea.

[EDIT] See my separate Answer done after clarification by OP.

—SA
   
v2
Comments
Abhinav S 28-Mar-11 0:35am
   
Good answer. My 5.
   
Thank you, Abhinav.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900