Beezgetz

It is about inches.

I have a textbox, where user inputs length in inches. Now, if input is whatever number (23.14; 7,34;...) is fine, but if it is not, it is cleared. What I want is, to be able to input let say 3/4 of inch. Like '3' and division sign '/' and than '4'.

I started like this:

string inch = textBox1.Text;
if ( inch.Contains("/"))

and it does detect if I input '/' character. Now, I was thinking of spliting that inch string into left and right (new ) strings, and normaly divide them in order to get 0,75 (which is 3/4).
But I have to ask you all for some advice and example line, because I dont know how to do that.

And that is not all. The real aim is to be able to put the length like 4 5/8. So 4 inches and five eights (4,625).

Thank you!











Re: Windows Forms General dividing string/textbox

bola shokry

Hi,
You can use MaskedTextBox
this control enable you to set a spcified format for user input




Re: Windows Forms General dividing string/textbox

Kenny99

making assumptions about user input can always be tricky and could lead to problems, and usually helps to be as explicit as possible (maybe an input box for ech part)

However, what you want to do could be done as possible:

Code Block

// first split based on a space (assuming myTextBox has string in format 4 5/8.)

string[] parts = myTextBox.Text.Split(' ');

// Then get the fractions (whole and divisor)

string[] fractions = parts[1].Split('/');

so your example could become:

Code Block

string inch = myTextBox.Text;

if (inch.Contains("/"))

{

// first split based on a space

string[] parts = myTextBox.Text.Split(' ');

// Then get the fractions (whole and divisor)

string[] fractions = parts[1].Split('/');

float number = 0;

float fractionA;

float fractionB;

if (float.TryParse(parts[0], out number))

{

if (float.TryParse(fractions[0], out fractionA) &&

float.TryParse(fractions[1], out fractionB))

{

number = number += (fractionA / fractionB)f;

}

}

}





Re: Windows Forms General dividing string/textbox

Kenny99

didnt notice post above, I would say you could use maskedtextbox to replace stuff like:

if (inch.Contains("/"))

{

}

my code shows how to obtain the actual numbers from the string;





Re: Windows Forms General dividing string/textbox

Beezgetz

Hello Kenny,

Thanks for fair warning!
And thanks for example!!!!
It looks promising, now I see how things are splited into parts and so on.
I will try to work with this. If I come accross problems, I'll come back...

Kind regards!