0
private void txtdiscount_SelectionChanged(object sender, RoutedEventArgs e)
{
    try
    {
        string dis = txtdiscount.Text.ToString();
        double isid = double.Parse(dis);

        isid = isid + 10;

        MessageBox.Show(isid.ToString());

    }
    catch (Exception exp)
    {
        MessageBox.Show(exp.ToString());
    }
}

I want to take input(double type) in text box txtdiscount and on SelectionChanged event of a textbox, a MessageBox should display the entered value after increment of 10 in its value. But with the above code, i get an exception that:

"Input String was not correct format"

at line:

string dis = txtdiscount.Text.ToString()

What is wrong with this code in textbox SelectionChanged event as the same code works fine when performed in a button click event?

 <TextBox  x:Name="txtdiscount" HorizontalAlignment="Left" Height="33" Margin="831,97,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="154" SelectionChanged="txtdiscount_SelectionChanged"/>
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
shakeel ahmad
  • 129
  • 1
  • 7
  • 4
    What is the value of `dis`? If someone typed `sausage` into that TextBox then what would happen? Look at `double.TryParse`. – Equalsk Jun 22 '18 at 09:27
  • 1
    Why are people downvoting? This is a very well explained question for a new user – musefan Jun 22 '18 at 09:29
  • 1
    Possible duplicate of [Input string was not in a correct format](https://stackoverflow.com/questions/8321514/input-string-was-not-in-a-correct-format) – Freggar Jun 22 '18 at 09:30
  • From the `XAML` code you seem to use `WPF`. Why you don't use `Binding` instead? – Rickless Jun 22 '18 at 09:30
  • But the same code works fine in when performed through Button Click event – shakeel ahmad Jun 22 '18 at 09:31
  • 2
    `But the same code works fine in when performed through Button Click event` - Then find why `dis` is a valid double value in one circumstance and not in the other. – Equalsk Jun 22 '18 at 09:33
  • 1
    when exactly does the exception occur? which value is in the textbox at this time? might it be that the exception is thrown, as soon as you remove the last number from the textbox? – Mong Zhu Jun 22 '18 at 09:33
  • 1
    @shakeelahmad What I find interesting here is that you mention the error occurs at line `string dis = txtdiscount.Text.ToString()`. Are you sure this is were the error occurs? As a side remark, the `txtdiscount.Text` property already returns a value of type `string` so the extra call to `ToString()` is not necessary. – Maurits van Beusekom Jun 22 '18 at 09:39
  • @MongZhu ok I am no WPF user but according to MSDN: https://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.text(v=vs.110).aspx – Maurits van Beusekom Jun 22 '18 at 09:48
  • @MauritsvanBeusekom I was wrong. removed my comment again :) I confused it with `RichTextBox`. My fault – Mong Zhu Jun 22 '18 at 10:02
  • @MauritsvanBeusekom and apparently I confused Winforms with WPF because only the former has the `Text` property :D – Mong Zhu Jun 22 '18 at 10:11
  • @MongZhu, sorry I don't follow, both the WPF and WinForms versions of the `TextBox` class have a `Text` property of type `string`. See: https://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.text(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/a19tt6sk(v=vs.110).aspx – Maurits van Beusekom Jun 22 '18 at 10:15
  • @MauritsvanBeusekom , I was talking about `RichTextBox`. [richtextbox-wpf-does-not-have-string-property-text](https://stackoverflow.com/questions/957441/richtextbox-wpf-does-not-have-string-property-text) – Mong Zhu Jun 22 '18 at 10:22

2 Answers2

3

Use Double.TryParse() & instead of SelectionChanged use TextChanged event.

As per MSDN

SelectionChanged :- This event occurs whenever there is a change to a selection. A selection can be changed not only by user interaction but also by binding as well as other set values.

TextChanged :- This event is raised if the Text property is changed by either a programmatic modification or user interaction.

 string dis = txtBox.Text;
 double isId;
 if (Double.TryParse(dis, out isId))
 {
      isId = isId + 10;
      MessageBox.Show(isId.ToString());
 }
 else
 {
    MessageBox.Show("Please Only enter Number");
 }
Lucifer
  • 1,594
  • 2
  • 18
  • 32
  • 2
    No need to parse twice! – Mark PM Jun 22 '18 at 09:30
  • Code that won't compile, please fix it (even after edit). Reason is that the `isId` variable is not declared. Especially for new users this is an important detail. – Maurits van Beusekom Jun 22 '18 at 09:34
  • @Lucifer nice the code compiles now. What would make it extra nice is if you would explain (in short) the difference between the two events – Maurits van Beusekom Jun 22 '18 at 09:49
  • 1
    @MongZhu no it won't, it will try to parse the empty string and fail and thus showing the message box asking the user to enter a number (question is if you want the message box to show in this situation but that is something the OP should decide and can easily solve) – Maurits van Beusekom Jun 22 '18 at 10:23
0

Your problem is that the SelectionChanged event is fired as soon as you click on the TextBox. At this point there is no value inside so double.Parse() gets an empty string as input and throws this exception. It would also throw the exception when you remove the last digit from the TextBox.

To solve this situation you can check for empty values:

private void txtdiscount_SelectionChanged(object sender, RoutedEventArgs e)
{
    try
    {
        if (!string.IsNullOrWhiteSpace(txtdiscount.Text))
        {

            string dis = txtdiscount.Text;
            double isid = double.Parse(dis);

            isid = isid + 10;

            MessageBox.Show(isid.ToString());
        }

    }
    catch (Exception exp)
    {
        MessageBox.Show(exp.ToString());
    }
}

same code works fine when performed in a button click event

Because you click after you have entered a valid value. This is the difference

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Thanks dear Mong Zhu.. Well Explained and i got the solution – shakeel ahmad Jun 22 '18 at 09:50
  • If I enter "xyz" in your code will it still perform same as previous? – Lucifer Jun 22 '18 at 09:56
  • @Lucifer yes, I guess this is the reason, why OP wrote the try/catch clause. To inform the user that the format is wrong. I guess they did not want that the message box is shown without a reason at the first click – Mong Zhu Jun 22 '18 at 10:00
  • @ Lucifer Exception throws when entered "xyz" etc – shakeel ahmad Jun 22 '18 at 10:01
  • @shakeelahmad you should use `TryParse()` rather then depending on try catch.Code based on catch is generally not a good practice – Lucifer Jun 22 '18 at 10:04
  • @shakeelahmad Did you intend to inform the user about the wrong format? May be printing out the `Message` is enough for a user instead of the entire stack trace: `MessageBox.Show(exp.Message);` – Mong Zhu Jun 22 '18 at 10:06
  • @Lucifer "Code based on catch is generally not a good practice" would you care to elaborate why this is so? – Mong Zhu Jun 22 '18 at 10:07
  • @MongZhu Imo Exceptions means error condition and the code which user is trying to achieve can be done without case of exception. – Lucifer Jun 22 '18 at 10:10
  • @Lucifer sounds reasonable. So would you inform the user about the mistake he made by typing `xyz` ? – Mong Zhu Jun 22 '18 at 10:12
  • @ Lucifer i used TryParse() and now no exception even when entered "xyz" etc.thzks – shakeel ahmad Jun 22 '18 at 10:14