dimanche 22 novembre 2015

C# Android Xamarin - if-else statement not working

I have a problem with an if-else statement I'm using in my C# code. Currently, I'm building an app with Xamarin in MS Visual Studio. I want the event "uitrekenen" to check whether the three edit objects actually have a value. If not, I want the object "totaalTekst" to show a message like: "Please fill everything in.", or something. When I debug the code on my phone, I get an error. The error must have something to do with the event "uitrekenen", because without this event (as you see it here) I don't get any errors. The code might be a bit confusing, since I used Dutch names for the objects. I hope anyone here knows what the problem could be! I appreciate your help and if anything isn't clear about my question; please ask!

Here is the code: My question especially concerns the last part, containing the event "uitrekenen".

using Android.OS;       // vanwege Bundle
using Android.App;      // vanwege Activity
using Android.Widget;   // vanwege TextView
using Android.Graphics; // vanwege Color
using System;

[ActivityAttribute(Label = "Leeftijd", MainLauncher = true)]
public class LeeftijdApp : Activity
{
EditText dagEdit;
EditText maandEdit;
EditText jaarEdit;
TextView totaalTekst;

protected override void OnCreate(Bundle b)
{
    base.OnCreate(b);

    TextView dagVerzoek;
    dagVerzoek = new TextView(this);
    dagVerzoek.Text = "DD:";
    TextView maandVerzoek;
    maandVerzoek = new TextView(this);
    maandVerzoek.Text = "MM:";
    TextView jaarVerzoek;
    jaarVerzoek = new TextView(this);
    jaarVerzoek.Text = "JJJJ:";

    dagEdit = new EditText(this);
    maandEdit = new EditText(this);
    jaarEdit = new EditText(this);

    Button knop;
    knop = new Button(this);
    knop.Text = "Klik hier!";

    totaalTekst = new TextView(this);
    totaalTekst.Text = "Hallo daar!";

    knop.Click += uitrekenen;

    LinearLayout stapel;
    stapel = new LinearLayout(this);
    stapel.Orientation = Orientation.Vertical;
    stapel.AddView(dagVerzoek);
    stapel.AddView(dagEdit);
    stapel.AddView(maandVerzoek);
    stapel.AddView(maandEdit);
    stapel.AddView(jaarVerzoek);
    stapel.AddView(jaarEdit);
    stapel.AddView(knop);
    stapel.AddView(totaalTekst);

    this.SetContentView(stapel);
}
protected void uitrekenen(object o, EventArgs ea)
{
    if (dagEdit != null && maandEdit != null && jaarEdit != null)
    {
        int DD = int.Parse(dagEdit.Text);
        int MM = int.Parse(maandEdit.Text);
        int JJJJ = int.Parse(jaarEdit.Text);
    }
    else
    {
        totaalTekst.Text = "Vul alle waarden in!";
    }
}
}

Aucun commentaire:

Enregistrer un commentaire