user2712192
user2712192

Reputation: 13

C# Calculation in Xamarin, issue displaying answer.

I am new to C# and Xamarin, I may be going about this solution incorrectly, however I am declaring values that will be obtained by user input 0-10 numbers no decimals no negative numbers. That will do a basic math operation a/b*c=answer... I then want to display the var C (answer) and use that eventually to change a timer interval. However for now, I am having a hard time making the code display my answer as text for the user to see.... Please see my code below.

[Activity(Label = "Infusion Calculator")]
public class infusionact : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Infusion);
        // Create your application here
        var volume = FindViewById<EditText>(Resource.Id.boxvolume);
        var drip = FindViewById<EditText>(Resource.Id.boxdrip);
        var dripmins = FindViewById<EditText>(Resource.Id.boxmins);
        var answermins = (Resource.Id.boxvolume / Resource.Id.boxmins * Resource.Id.boxdrip);

        Button button = FindViewById<Button>(Resource.Id.btncalculate);
        TextView textView1 = (TextView)FindViewById(Resource.Id.textView1);

        button.Click += delegate
        {
            // NEED TO FIGURE OUT HOW TO SET TXT LABEL WITH VAR ANSWERMINS ON CLICK  
            textView1.SetText(answermins);

        };



    }

}

Upvotes: 0

Views: 319

Answers (3)

yash darak
yash darak

Reputation: 368

this is the correct code -

[Activity(Label = "Infusion Calculator")]
public class infusionact : Activity
{
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    SetContentView(Resource.Layout.Infusion);
    // Create your application here
    var volume = FindViewById<EditText>(Resource.Id.boxvolume);
    var drip = FindViewById<EditText>(Resource.Id.boxdrip);
    var dripmins = FindViewById<EditText>(Resource.Id.boxmins);

    Button button = FindViewById<Button>(Resource.Id.btncalculate);
    TextView textView1 = FindViewById<TextView>(Resource.Id.textView1);

    button.Click += delegate
    {
        // NEED TO FIGURE OUT HOW TO SET TXT LABEL WITH VAR ANSWERMINS ON CLICK  
       var answermins = volume.Text/(dripmins.Text*drip.Text);
            textView1.Text=answermins.ToString();

    };



}
}

Upvotes: 0

lowleetak
lowleetak

Reputation: 1382

Remove the line because you are using the resource ID to do calculation and not the value in the EditText.

var answermins = (Resource.Id.boxvolume / Resource.Id.boxmins * Resource.Id.boxdrip);

Update the click event to do the calculation.

button.Click += delegate
    {
        var volumeValue = 0;
        var dripValue = 0;
        var dripMinsValue = 0;

        // Parse value in text to integer
        int.TryParse(volume.Text, out volumeValue);
        int.TryParse(drip.Text, out dripValue);
        int.TryParse(dripmins.Text, out dripMinsValue);

        var answermins = 0;
        if (dripMinsValue != 0)
        {
            answermins = volumeValue / dripMinsValue * dripValue;
        }

        textView1.SetText(answermins);
    };

Upvotes: 0

Mirel Vlad
Mirel Vlad

Reputation: 2082

I think you are misusing those variables. For example,

var volume = FindViewById<EditText>(Resource.Id.boxvolume);

returns the VIEW associated to the specified id, whereas,

var volumeValue = volume.Text;

would return the value you have entered as input for your EditText control. Its these values that you need to process and then display on your TextView.

Upvotes: 1

Related Questions