navya556
oh.. can you elaborate clearly?
Sure. It refers to my comment to the question. Actually, the code is totally unacceptable. It's better not programming at all than programming this way. Not to worry, it can be learned and understood. This is one of the very basic idea in programming:
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[
^].
Take this multiple "case" lines. You are doing the same thing without any reason. How about
HandleDigitClick(e.KeyChar.ToString())
? What this method should do? Not the "switch". Ultimately, you will operate on some numbers. And, for example,
int.Parse(string)
will give you the integer value corresponding to your click. But even this makes no sense, because you should operate not on digits, but on numbers.
A click should be translated into something like.
MyTextBox.Text += MyTextBox.Text + e.KeyChar.ToString();
And later, when doing some mathematical operation, you would do, say,
int.Parse(MyTextBox.String)
; or
TryParse
.
This way, you can get rid of all those "case" lines, all of them.
But will it be good enough? No, it could be useful as a programming exercise, but not make a useful calculator. What are you trying to achieve, a calculator with buttons? But who needs those, ever? Years ago, they were introduced as a cheap and primitive calculation device, when people simply did not have personal computers. But now? The calculator can be just a text area where you can write any expressions and then, say, click "Run". Is it too hard? For you, at this moment, it may look hard to do, but actually this is a very easy task; and almost everything is already available.
(And yes, Windows calculator is just ridiculous. People just use it by inertia and some ignorance.)
—SA