-4
private void textBox4_TextChanged(object sender, EventArgs e)
    {
        Double d;
        if (!string.IsNullOrWhiteSpace(textBox4.Text))
        {
            d = Convert.ToDouble(textBox4.Text);
            var c = d / 1;

            if (c == 0.75)
            {


                string myString = c.ToString();


            }
            else if (c == 0.25)
            {  }
            else if (c == 0.50) {  }
            else if (c == 0.00) { }
            else { MessageBox.Show("you can't enter this"); }
        }
    }

ok so i fixed that error but every time i enter a number the messagebox shows you cant enter this , even though d=5.0/1 = 0 and d=0.75/1 = 0.75 and i don't know why
any help would be appreciated

  • What value you enter in texbox4 when it give you an error, you can use Int32.TryParse() to avoid errors – Baximilian Jul 11 '14 at 23:23
  • when im trying to enter .75 , when i used your Int32.TryParse() code it gave me an error : No overload for method 'TryParse' takes 1 arguments – user3810508 Jul 11 '14 at 23:26
  • try like `int num = 0;Int32.TryParse(textBox4.Text, out num) ` – Rahul Jul 11 '14 at 23:28
  • `.75` is not an integer. it is `double` – EZI Jul 11 '14 at 23:28
  • then i need to convert my textbox4 value to double , how to do that ? – user3810508 Jul 11 '14 at 23:30
  • In your VS type `Convert.` and see what methods are there.... http://msdn.microsoft.com/en-us/library/aa711900(v=vs.71).aspx – EZI Jul 11 '14 at 23:31
  • Be aware that `double`s are not supposed to be compared using `==` (`(c == .75)`). – AlexD Jul 11 '14 at 23:32
  • then how can i compare between 0.75 and the input im entering in textbox4 ? – user3810508 Jul 11 '14 at 23:34
  • It is about Java, but the principle is the same: http://stackoverflow.com/questions/1088216/whats-wrong-with-using-to-compare-floats-in-java (Well, if you really want to be absolutely precise, you could write code to normalize your double-as-string and compare against "0.75"-string. But I doubt you really want it.) – AlexD Jul 11 '14 at 23:48
  • What is your `CurrentCulture` exactly? – Soner Gönül Jul 12 '14 at 00:04

4 Answers4

0

There are many ways to skin the cat:

string s = "3"; // you should have your text here
int i ;

// Cannot implicitly convert type 'int' to 'string'
// i=s; 

//The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type)
//i = s as int; 

// OK
i = Convert.ToInt32(s);

// Ok, but if s is not number, will throw an exception
i = Int32.Parse(s)

// OK, try_parse will be true, and i will hold the value
// or try_parse will be false, and i will not be set
bool try_parse = Int32.TryParse(s, out i);

while i was writting this, i see the conversation continues ... double has a try parse method as well:

var s = "0.45";
double d;
bool b = Double.TryParse(s, out d);
Noctis
  • 11,507
  • 3
  • 43
  • 82
0

then i need to convert my textbox4 value to double , how to do that ?

You can Convert.ToDouble(string) method which is explicitly call Double.Parse(string, CurrentCulture). Here how this method implemented;

public static double ToDouble(String value)
{
      if (value == null)
          return 0;
      return Double.Parse(value, CultureInfo.CurrentCulture);
}

And this Double.Parse(string, CurrentCulture) implemented as;

public static double Parse(String s, IFormatProvider provider)
{
     return Parse(s, NumberStyles.Float| NumberStyles.AllowThousands, 
                     NumberFormatInfo.GetInstance(provider));
}

NumberStyles.Float is a composite style which is include AllowDecimalPoint style. You didn't tell us what is your CurrentCulture exactly but if your current thread culture's NumberFormatInfo.NumberDecimalSeparator property is dot (.) you can use this method.

If it is not, you can use directly Double.Parse(string, IFormatProvider) which format has a dot (.) as a decimal separator like InvariantCulture for example.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0
string text = "5.0"; // or 5.0 or .75 or 23 or ..
string err = "input is not a number!";

if (string.IsNullOrWhiteSpace(text))
{
MessageBox.Show(err);

return;
}

Double d;

// using CultureInfo.InvariantCulture - assuming that decimal point will always be "."
if (!Double.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out d))
{
MessageBox.Show(err);

return;
}

// at this point there is a "d" variable 100% a number, do whatever you want with it
// AND BTW 5.0/1 = 5.0, not 0 !!

MessageBox.Show(d.ToString());

var c = d / 1; //WHY is this there? Every number (even negative number) divide by 1 is the same  number, not matter if it is 4 or 100565.432345 or googol ;)

string myString = String.Empty;

if (c == 0.75)
myString = c.ToString();
else if (c == .25)
myString = c.ToString();
else if (c == .5)
myString = c.ToString();

// etc, etc.. don't know your use case
paYa
  • 231
  • 1
  • 7
-3

That's because you are trying to convert a string to a integer and you get an exception. Convert class does not handle exceptions. You should use int.TryParse instead of Convert.ToInt32, because TryParse won't crush your program and you can act accordingly to format errors. Use Convert only when you are 100% sure the input can be converted

Something like

if(int.TryParse(textBox4.Text,out d))
{
    //continue with your code
}
else
{
    //print something like 'that was not a number'
}

Edit: I just checked you want to compare to .75, so it would be like this

private void textBox4_TextChanged(object sender, EventArgs e)
{

        double d;
        double yourValue = 0.75d; // or whatever value you want
        if(Double.TryParse(textBox4.Text,out d))
            if(d == yourValue)
                MessageBox.Show(d.ToString);
        else
            MessageBox.Show("Not a number"); // or do nothing, whatever you want 


}
David Zhou
  • 142
  • 7