EN VI

C# - Why are there no results after clicking button even though event handler is initiated and button_click method created?

2024-03-14 06:00:05
C# - Why are there no results after clicking button even though event handler is initiated and button_click method created?

I'm having issues with a practice side project I am making within Android(Xamarin). Its a annual income side project and I simply have a "Calculate" button. Below is the method code to connect the data points within xml code and it has the calculateButton.Click event handler.

The next piece of code is the click method itself and it should return the items that are inputted by the user. I will include as well the EditText and TextView variables.

So to give more details on whats happening, I run the Android emulator, I type in the numbers in the 4 edit boxes, Click calculate(the button), and no results pop up next to the 5 Textview's. Is there something wrong within here that anyone sees? I don't have any errors so now my head hurts.

Side Notes, I had originally calculateButton.Click += CalculateButton_Click; Also, this is within MainActivity.cs lol

EditText incomePerHourEditText;
EditText workHoursEditText;
EditText taxRateEditText;
EditText savingsRateEditText;

TextView workSummaryTitle;
TextView annualIncomeTitle;
TextView annualTaxTitle;
TextView annualSavingsTitle;
TextView spendableIncomeTitle;

Button calculateButton;
RelativeLayout resultLayout;

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    
    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.activity_main);
}

void ConnectViews()
{
    incomePerHourEditText = FindViewById<EditText>(Resource.Id.incomePerHourEditText);
    workHoursEditText = FindViewById<EditText>(Resource.Id.workHoursEditText);
    taxRateEditText = FindViewById<EditText>(Resource.Id.taxRateEditText);
    savingsRateEditText = FindViewById<EditText>(Resource.Id.savingsRateEditText);

    workSummaryTitle = FindViewById<TextView>(Resource.Id.workSummaryTitle);
    annualIncomeTitle = FindViewById<TextView>(Resource.Id.annualIncomeTitle);
    annualTaxTitle = FindViewById<TextView>(Resource.Id.annualTaxTitle);
    annualSavingsTitle = FindViewById<TextView>(Resource.Id.annualSavingsTitle);
    spendableIncomeTitle = FindViewById<TextView>(Resource.Id.spendableIncomeTitle);

    calculateButton = FindViewById<Button>(Resource.Id.calculateButton);
    resultLayout = FindViewById<RelativeLayout>(Resource.Id.resultLayout);

    calculateButton.Click += new EventHandler(CalculateButton_Click);

}
protected void CalculateButton_Click(object sender, System.EventArgs e)
{
    //Take inputs from user
    double incomePerHour = double.Parse(incomePerHourEditText.Text);
    double workHourPerDay = double.Parse(workHoursEditText.Text);
    double taxRate = double.Parse(taxRateEditText.Text);
    double savingsRate = double.Parse(savingsRateEditText.Text);

    double annualWorkHoursSummary = workHourPerDay * 5 * 50; //50 because most get 2 weeks off
    double annualIncome = incomePerHour * workHourPerDay * 5 * 50;
    double taxPayable = (taxRate / 100) * annualIncome;
    double annualSavings = (savingsRate / 100) * annualIncome;
    double spendableIncome = annualIncome - annualSavings - taxPayable;

    //Display results of the calculations
    workSummaryTitle.Text = annualWorkHoursSummary.ToString()+" Hrs";
    annualIncomeTitle.Text = annualIncome.ToString()+ " USD";
    annualTaxTitle.Text = taxPayable.ToString() + " USD";
    annualSavingsTitle.Text = annualSavings.ToString() + " USD";
    spendableIncomeTitle.Text = spendableIncome.ToString() + " USD";

    //display the Resultlayout from invisible to visible
}

This is the results I am expecting. However, the numbers do not show up on the bottom right next to the Annual ____. Help ;-;

enter image description here

Solution:

You are not calling your ConnectViews method. Change this:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    
    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.activity_main);
}

To this:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    
    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.activity_main);

    ConnectViews();
}
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login