2 Replies - 779 Views - Last Post: 15 July 2012 - 06:00 AM Rate Topic: -----

#1 husain611987  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-July 12

following code not working in c sharp

Posted 15 July 2012 - 12:45 AM

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
           {

               if (char.IsDigit(e.KeyChar))
               {
                   stemp += e.KeyChar.ToString();
                   double.TryParse(stemp, out tempararymain);
               }
               else if (e.KeyChar == Convert.ToChar(Keys.Back) || e.KeyChar == Convert.ToChar(Keys.Delete) || e.KeyChar == Convert.ToChar(Keys.Enter))
               {
                   if (textBox1.Text.Length > 0)
                   {
                       stemp = textBox1.Text.Substring(0, textBox1.Text.Length-1);
                       double.TryParse(stemp, out tempararymain);
                   }
                 
                }
             else if (e.KeyChar.ToString() == ".")
                  {
                      stemp = textBox1.Text + e.KeyChar.ToString();
                      double.TryParse(stemp, out tempararymain);
                  }
                  else
                  {
                      e.Handled = true;
                  }
           }



The above code is working only for bakspace. i try to delete the text from text box by selecting previous data and want to enter another data but at time of exeution that previous data which i previously entered also come along with new data. what can i do???
in attaced file first i enter 1 and execute it. it shows right answer.
then i select that that 1 and using delet button i delete it and wirte 5 and again execute it
but at this time the answer apper as shown in fig

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: following code not working in c sharp

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,280
  • Joined: 05-May 12

Re: following code not working in c sharp

Posted 15 July 2012 - 01:08 AM

When you press the Delete key, does the program actually go to line 13? If so, does that set stemp to an empty string? When you press the '5', does the program go to line 6? If so, what is the value of stemp? Assuming no other code plays with stemp, it should be an empty string. Based on the behavior you've described, it looks like it gets to line 6 containing a "1" and so the '5' gets appended to the string. I would try to find the code that sets the string back to "1" after line 13 sets it to an empty string.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5578
  • View blog
  • Posts: 11,924
  • Joined: 02-June 10

Re: following code not working in c sharp

Posted 15 July 2012 - 06:00 AM

rookie: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # 2. (Click the SHOW button below)



TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1