3

I'm getting this unhandled exception error:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll.

Additional information: Exception has been thrown by the target of an invocation.

This is my code in short. It's a calculator of two TextBoxes which should together become a new answer when the user press +, -, /, x button made in WPF.

public partial class MainWindow : Window
{
    public string numberInString
    {
        get { return TextDoos.Text; }
        set { TextDoos.Text = value; }
    }

    public MainWindow()
    {
        if(TextDoos.Text == "")
        {
            if(TextDoos2.Text == "")
            {
                RekenFunctie(TextDoos.Text, TextDoos2.Text);
            }
        }
    }
}

public int RekenFunctie(string numberInString, string numberInString)
{
    int antwoord;
    int getal = Convert.ToInt32(numberInString);
    int getal2 = Convert.ToInt32(numberInString2);

    if (Buttons.IsPressed) // This is the + button, there are also -,x,/ buttons.
    {
       antwoord = getal + getal2;
       return antwoord;
    }
}

I can't see why it isn't working...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oned Zair
  • 33
  • 1
  • 2
  • 7
  • 3
    TargetInvocationException always has an InnerException - you should look at that to find out the original cause. – Jon Skeet Oct 17 '12 at 09:57

2 Answers2

3

You missed the call of InitializeComponent() in MainWindow constructor;

public MainWindow() 
{ 
     InitializeComponent();
     button1.Click += button1_click; //'+' button
}

private void button1_click(object sender, RoutedEventArgs e)
{
    int antwoord;
    int getal = Convert.ToInt32(TextDoos.Text);
    int getal2 = Convert.ToInt32(TextDoos2.Text);

    antwoord = getal + getal2;
    resultTextBox.Text = antwoord ;
}

Anyway your code is strange. RekenFunctie makes some calculation, but you call it from the constructor. So, you run this code only once, but I think your user wants to interact with your calculator.

I think you should read something about Button.Click events.

Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39
  • 1
    No, this question and answer could hepl somebody with similar problem. – Anton Sizikov Oct 17 '12 at 10:35
  • But I can't execute RekenFunctie() in an event-based-method like Buttons_Clicked() because it give me another error, that's why i've put it like that... I still don't get all the restrictions/rules of constructors, events and other stuffs of C# yet. – Oned Zair Oct 17 '12 at 10:56
  • I've updated my answer with example how to treat with button events. – Anton Sizikov Oct 17 '12 at 11:07
  • Can you explain me why you have added "button1.Click += button1_Click;". I have removed that line to see what the different is.. – Oned Zair Oct 18 '12 at 07:44
  • You have a + button. I don't know it's name, so a called it button1. Also, I added the Click event handler. So, if user clicks the button the method button1_click will run. In this method I check the text in TextDoos and TextDoos2 (don't forget to check, if they are not digits(see Int32.TyParse method)). After I calculate the sum and write it to TesultTextBox. – Anton Sizikov Oct 18 '12 at 08:35
0

I was having a similar problem.

I was getting the same error when setting the text on a TextBlock from a property changed evet for a TextBox.

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        MainTextBlock.Text = ((TextBox)sender).Text;
    } 

I think it was being called at runtime before the MainTextBlock component was initialized.

In my case, just checking for null did the trick.

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (MainTextBlock != null)
            MainTextBlock.Text = ((TextBox)sender).Text;
    } 
bnieland
  • 6,047
  • 4
  • 40
  • 66