Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > C # maskedTextBox
 

C # maskedTextBox

hello ,

I am new to C#, and i am havin a problem,

I have several masked textboxes in a format such as this one " 00.#"

so the user can only enter numerical values such as 00.0 , 30.0 , 35.4, etc...

i will be sending the values to a microcontroller to store these values through a serial port..

the formatting os on such a way tha it will take something like this 0_.0

how do i check to make sure that the user enters in the correct format like .... 00.0 or 01.0 or 09.0

this check should be done before it goes to the micro controller ..

Thanks in advance ... any help is appreciated ..

Kilogn  Thursday, June 21, 2007 9:04 PM
Handle the Validating event to parse the Text in the MaskedTextBox into float type, if convertion faild, cancel the validating, which prevent the fucus moving out of the MaskesTextBox.

Code Snippet

maskedTextBox1_Validating(object sender, CancelEventArgs e)

{

try

{

float.Parse(this.maskedTextBox1.Text);

}

catch

{

e.Cancel = true;

}

}

Handle the Validating event to parse the Text in the MaskedTextBox into float type, if convertion faild, cancel the validating, which prevent the fucus moving out of the MaskesTextBox.

Code Snippet

maskedTextBox1_Validating(object sender, CancelEventArgs e)

{

try

{

float.Parse(this.maskedTextBox1.Text);

}

catch

{

e.Cancel = true;

}

}

i tried it but it didnt seem to do what i wanted it to do ...myquestion now is

i have a set button on my window and when the user clicks a wrong format ... other than the masked format i want to catch the error

say for example i have this code write..

private void setvalue_Click(object sender, EventArgs e)

{

string a = txBxP4.Text;

MessageBox.Show(a);

}

i have already set the masked text boxes to only accept input like this "##.#"

now i want to display an error message each time he enters something like 0_.0

I am sorry for the dumb questions .. i am new to programming ... any help will be very much appreciated ..

thanks

Kilogn  Friday, June 22, 2007 1:16 PM
Try something like this:

Code Snippet

private void button1_Click(object sender, EventArgs e)

{

try

{

float.Parse(this.maskedTextBox1.Text);

}

catch

{

MessageBox.Show("Input incorrect!");

}

}

Zhi-Xin Ye  Sunday, June 24, 2007 4:02 PM

You can use google to search for other answers

Custom Search

More Threads

• Other connection method
• Problem with Default namespace: http://tempuri.org
• 'Error : Object reference not set an instance of an object' in DataGridView in Window
• How to Bind Business Entity Components to User-Interface Controls
• Terrarium server !!!
• Hi
• What's VSDeisgner ?
• ObjectDisposedException
• Question about MoveCompleted and IsMoving
• Sample code not working in VB.NET ???