5 Replies - 14616 Views - Last Post: 21 April 2010 - 09:34 AM Rate Topic: -----

#1 Aldahbra  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 02-January 10

C# Masked Text Box position cursor to left

Posted 18 April 2010 - 03:26 PM

Hi

I am new to programming.

I am using a form with Masked Text Boxes to ensure that the users input numbers. However, I am having the following problem;

If the tab key is used to move between masked text boxes the cursor always goes to the left ready for user input;

BUT

if the user clicks on the maskedTextBox with the mouse the cursor moves to the point where the mouse is clicked rather than to the left ready for input.

How can I set the box to always move the cursor to the left rather than the user having to delete the spaces used by the mask or use the arrow keys?

Is This A Good Question/Topic? 0
  • +

Replies To: C# Masked Text Box position cursor to left

#2 Tanner.R  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 33
  • Joined: 17-April 10

Re: C# Masked Text Box position cursor to left

Posted 18 April 2010 - 05:34 PM

put this into your maskedTextBox click event or whatever.:)
maskedTextBox1.Selectionstart = 0;

Was This Post Helpful? 0
  • +
  • -

#3 Aldahbra  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 02-January 10

Re: C# Masked Text Box position cursor to left

Posted 20 April 2010 - 01:35 PM

View PostTanner.R, on 18 April 2010 - 04:34 PM, said:

put this into your maskedTextBox click event or whatever.:)
maskedTextBox1.Selectionstart = 0;


I have seen answers like this in my Google searches but I don't understand where to put it. The code for my masked textboxes is;

public void X1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)

{
MessageBox.Show("You must enter a number ", "Error");

}

There is no other code for the MaskedTextBoxes.

I am using Visual studio 2008, using a WindowsFormApplication. I drew the MaskedTextBoxes on the form and am using the values in the boxes succesfuly.
Was This Post Helpful? 0
  • +
  • -

#4 pareidolia  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 17-September 09

Re: C# Masked Text Box position cursor to left

Posted 20 April 2010 - 07:41 PM

View PostAldahbra, on 20 April 2010 - 12:35 PM, said:

View PostTanner.R, on 18 April 2010 - 04:34 PM, said:

put this into your maskedTextBox click event or whatever.:)
maskedTextBox1.Selectionstart = 0;


I have seen answers like this in my Google searches but I don't understand where to put it. The code for my masked textboxes is;

public void X1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)

{
MessageBox.Show("You must enter a number ", "Error");

}

There is no other code for the MaskedTextBoxes.

I am using Visual studio 2008, using a WindowsFormApplication. I drew the MaskedTextBoxes on the form and am using the values in the boxes succesfuly.


That's an event, there has to be more code than that. You generally would want to put it in the class that inherits the form class; I think in events or functions if you want it done at certain times or in the constructor if you want it there initially.

This post has been edited by pareidolia: 20 April 2010 - 07:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 Tanner.R  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 33
  • Joined: 17-April 10

Re: C# Masked Text Box position cursor to left

Posted 20 April 2010 - 08:41 PM

OK I will walk you through how to make this work. :)

First select your maskedTextBox that you type into and go into its properties, now at the very top on the bar there should be a lighting bolt, click it, what that does is gives you a list of events you can create for the maskedTextBox so just find the MouseClick event a double click it, and it should create this:
private void maskedTextBox1_MouseClick(object sender, MouseEventArgs e)
{
}

Then add the code I gave you inside that like this:
private void maskedTextBox1_MouseClick(object sender, MouseEventArgs e)
{
maskedTextBox1.Selectionstart = 0;
}
That should work. :)

This post has been edited by Tanner.R: 20 April 2010 - 08:43 PM

Was This Post Helpful? 1
  • +
  • -

#6 Aldahbra  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 02-January 10

Re: C# Masked Text Box position cursor to left

Posted 21 April 2010 - 09:34 AM

Thanks for your help, that was great, problem sorted.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1