9 Replies - 968 Views - Last Post: 02 February 2012 - 08:28 PM Rate Topic: -----

#1 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Deleting from TextBox.Text

Posted 02 February 2012 - 06:05 PM

I'm a bit new to C# Window Forms Applications. Anyway, I'm writing a basic calculator program to start off. I'm making a button for deleting from the number, I tried doing


TextBox.Text.Length -= 1;




But an error message said that
 TextBox.Text.Length 
was read only or something like that. Any ideas on how I can have something like a backspace event?

It would be greatly appreciated :yes:

Is This A Good Question/Topic? 0
  • +

Replies To: Deleting from TextBox.Text

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Deleting from TextBox.Text

Posted 02 February 2012 - 06:17 PM

Backspaces work natively for Textboxes. Are your textboxes not working when you hit the backspace key?
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Deleting from TextBox.Text

Posted 02 February 2012 - 06:21 PM

The Text property is just a string, so you could manipulate it like any other string. Look into the Substring and Remove methods.

This post has been edited by Atli: 02 February 2012 - 07:49 PM
Reason for edit:: Meant to write Remove, not Replace.

Was This Post Helpful? 0
  • +
  • -

#4 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Deleting from TextBox.Text

Posted 02 February 2012 - 06:53 PM

No, the backspace works, I would just like to have a delete key so that people using it could use either the keyboard or the calculator keypad.

And Atli, did you mean Remove? That's where your link leads to. Anyway, I tried this:


TextBox.Text.Remove(1);




But to no avail. Normally the removing length should work with strings, I don't know why it doesn't work with this.

This post has been edited by superkb10: 02 February 2012 - 07:07 PM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

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

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: Deleting from TextBox.Text

Posted 02 February 2012 - 07:05 PM

He's saying get the .Text of the textbox

Now set it to a substring of itself, for its own length - 1

myTextbox.Text = myTextbox.Text.Substring(myTextbox.Text.Length-1);

.Length for the text is mostly a read-only property. It isn't smart enough to know *how* to reduce it.

So if the text is "yogi" the .Length is 4. But setting the length to 3 won't make it delete the last 'i' character.

rookieWhat 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 no college course teaches. 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.

Also, see FAQ 23 below


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



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Deleting from TextBox.Text

Posted 02 February 2012 - 07:14 PM

The type String in .net is immutable.
Which means when you do an operation on them, it creates a new instance of the type string. So if you want to affect the original, you need to change the reference to the contents.

string modifiedstr = mystring.substring(0, mystring.lenght-1)
mystring = modifiedstr

Was This Post Helpful? 0
  • +
  • -

#7 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Deleting from TextBox.Text

Posted 02 February 2012 - 07:31 PM

Okay, AdamSeight2008

I took your suggestion, however, my problems is it's deleting only the first character in it. I am looking for more of a backspace type thing.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

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

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: Deleting from TextBox.Text

Posted 02 February 2012 - 07:39 PM

If you would just take a moment to read the MSDN on strings rather than asking others to do your research for you, you would see there is an overload for substring that takes a starting point and length.

http://msdn.microsof...y/aka44szs.aspx

This page also explains why the way you were shown (be me included) was wrong and acting as a first character delete instead of a last character delete.
Was This Post Helpful? 0
  • +
  • -

#9 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Deleting from TextBox.Text

Posted 02 February 2012 - 08:03 PM

Okay, well I figured it out(kinda), I guess you have to make 0 the StartIndex. I was thinking that if I put in no value, and just had one parameter, the default value for where it starts would be 0, but guess not.
Was This Post Helpful? 1
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Deleting from TextBox.Text

Posted 02 February 2012 - 08:28 PM

How would it know? that was your intent?
It's only since C#4.0, that the C# language could express optional parameters,and thus default parameters values. The only way available to developers was method overloads, and comes with restrictions on what's possible.
Overload need
to have different input types
or different number of parameters.
or a mixture of both.

It can't have methods with the same name, that take the same input parameters (it signature) and do different things. It can't even do overloads with different return types.

Reading the Documentation of the API, is essential to know what the API does.

This post has been edited by AdamSpeight2008: 02 February 2012 - 08:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1