6 Replies - 3185 Views - Last Post: 06 June 2014 - 09:57 AM Rate Topic: -----

#1 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 223
  • Joined: 25-April 11

overwrite text in masked textbox without selecting or deleting content

Posted 23 August 2012 - 02:15 AM

Hello everybody

I was wondering if there is a -> simple <- way to overwrite existing text in a masked texbox...
Example: I have a masked textbox, where you can insert dates. That means that you can enter only numbers for the day, month and year.
What I would like to do is, that if there is already a date in my textbox and the textbox gets focus, that you can directly write the new date into, without having to select or delete the existing date. Like if you were using the Insert-Key of your Keyboard. Hope you know what I mean...

If I have to do it programmatically, there is no problem... I already have some ideas how I could do something like that, but I would like to know if VS has any parameter or any option I only have to activate.

Thank you in advance

This post has been edited by Anthonidas: 23 August 2012 - 02:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: overwrite text in masked textbox without selecting or deleting content

#2 foohoo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 10-August 07

Re: overwrite text in masked textbox without selecting or deleting content

Posted 23 August 2012 - 04:09 AM

Hi Anthonidas,

Yeah there's a pretty simple way to do it using the .SelectAll property of a textbox.

Just create a method to handle the event of either Focus->Enter or onclick

e.g.

private void Select_All(object sender, EventArgs e)
        {
            TextBox1.SelectAll();
        }



Cheers

Edit: you can use the "sender" object to be a bit more generic about the textbox that is being used.

This post has been edited by foohoo: 23 August 2012 - 04:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 223
  • Joined: 25-April 11

Re: overwrite text in masked textbox without selecting or deleting content

Posted 23 August 2012 - 06:37 AM

Firstly, thank you for your answer.

Secondly,
I know the SelectAll() command, but it is not exactly what I was looking for... I would not like to select and/or delete the content when focusing the textbox. I would just like to have the same effect like if I were using the Insert-key --> that it deletes only the char I am writing over and not the whole content.

Otherwise if I have to do it programmatically i would have to work with replace() in combination with selectionstart() and selectionlength()... but again this is not my question ;o)
Well perhaps the simple answer is, that it is not possible to do it automatically.

This post has been edited by Anthonidas: 23 August 2012 - 06:39 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: overwrite text in masked textbox without selecting or deleting content

Posted 23 August 2012 - 11:24 AM

This is one of the downsides of using the Masked Textbox that comes with Framework. The usability of the control is not the best thing in the world. I think a lot of work went into making things look right. They look great on forms, specially for displaying data. Unfortunately, not as much thought went into how a normal human would interact with it to input data. It works great for the "happy" use case when the user never makes a mistake and inputs data correctly on the first try. For the "unhappy" use cases where the user needs to edit, it becomes a very frustrating control to work with.

I tried doing what you are trying to do now by having tons of keypress event handlers and text changed event handlers. At one point, I was considering just starting from scratch just deriving from the TextBox and writing my own custom control. It was more trouble than it was worth.

In the end, for the sanity of my users, I reverted to using the standard TextBox. What I did was made use of the ErrorProvider to show an icon beside the textbox when the input was incorrect. I also used tooltips to suggest the input format. I also used the Validating event to do any last minute checks on the input. On the Validated event, I updated the text input to the "normalized" output of the data. At the time I wrote the code, the Web style of having prompting text inside the textbox wasn't popular yet. If it had, I would have used that instead of tooltips.

This post has been edited by Skydiver: 23 August 2012 - 11:25 AM

Was This Post Helpful? 1
  • +
  • -

#5 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 223
  • Joined: 25-April 11

Re: overwrite text in masked textbox without selecting or deleting content

Posted 24 August 2012 - 02:10 AM

Lol... you have convinced me... i'll not do anything like that. I already have different validating functions to check the input, so i think i can let it be as it is.

Thank you very much for your reply.
Was This Post Helpful? 0
  • +
  • -

#6 zeeshanef  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 76
  • Joined: 14-April 12

Re: overwrite text in masked textbox without selecting or deleting content

Posted 06 June 2014 - 08:59 AM

Simply:
maskedTextBox1.InsertKeyMode = InsertKeyMode.Overwrite;

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: overwrite text in masked textbox without selecting or deleting content

Posted 06 June 2014 - 09:57 AM

Ah, somebody who obviously has not taken time to do some usability testing with the control. All that does is set the initial mode. It did nothing to fix the indemic broken behavior of the control as you try to edit and overwrite the contents of the control.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1