Search code examples
c#visual-studio-2012maskedtextbox

How do I convert a masked textbox to an int with the mask of 000-000?


int AccNo = Convert.ToInt32(AccMaskedTextBox.Text);

This is how I try to assign the input of my maskedtextbox. The issue I have is that I set a custom mask. It is in the form of 000-000. When I try to assign the input of maskedtextbox to AccNo it breaks with the error :

Input string was not in a correct format.

The value of AccNo comes out to be for example 123-456 if string type is used. So I need to get rid of the hyphen so I can assign it to type int. How can I do t without changing my mask?


Solution

  • The easiest would be using Replace to replace the hyphen with nothing:

    int AccNo = Convert.ToInt32(AccMaskedTextBox.Text).Replace("-", string.Empty);