Any Characters - Masked Text Box

1k views Asked by At

I'm writing an app in C# with Visual Studio 2012, and I'm needing to format some input text using MaskedTextBox. The user will type in a folder path to the text box, but since the folder path is relative to another path, I need it to start with ".\", but I do not care how long the path is.

Right now, I have the mask set for the box to \.\\CCCCCCCCCCCCCCCCCC. This works fine except for the fact that when the user clicks into the box, it places the cursor where they click instead of the beginning of the box.

Is there a way to set the mask to still put in the ".\" but not to set any limit on the characters that come after it?

Or is there a way I'm overlooking?

EDIT: More info

So I've tried a couple recommended things, but they don't seem to work. The answer linked here doesn't work well. While I can set it to go to that selection point when I click on the box, it will go there every time you need to click on the box. So you can't select the whole box or edit part of what you typed, which is even worse for usability.

I also tried the method suggested by Adelmo. I made an even handler like so:

public Form1()
{
    InitializeComponent();
    refreshList();
    this.textBoxPrintFolder.GotFocus += new EventHandler(textBoxPrintFolder_GotFocus);

}
private void textBoxPrintFolder_GotFocus(object sender, EventArgs e)
{
    this.textBoxPrintFolder.Select(2, 0);
}

This works when tabbing to the box, but apparently clicking on the box doesn't go into the GotFocus event.

I've also tried using the MouseEnter event. While it does work, it takes a few seconds before it will move. Not ideal.

Any help would be greatly appreciated.

1

There are 1 answers

1
Adelmo Pereira On

Maybe using onFocus event:

You can control cursor position (and selection) by TextBox.SelectionStart and TextBox.SelectionLength properties.

Example if you want move cursor to before 3th character set SelectionStart = 2 and SelectionLength = 0.

http://social.msdn.microsoft.com/Forums/en-US/04362a62-8cbf-4d86-a1bc-2aba8e4978ca/cursor-position-in-textbox

Hope it help you