Check one Radio button by Default .NET MAUI

Kalyan A 245 Reputation points
2024-09-26T11:55:20.02+00:00

Here a simple calculator to perm Add/Sub/Div /Multiply between Whole number and Fractions.

I got help from this form to load the radio button group

{ Addition, Substraction ,Multiplication,Division}

But I want one Radio Button to be checked by default say add, please suggest.

namespace MauiAppMCQs.Models
{
    public class FractionEnums
    {
        public enum Calulation { Addition, Substraction ,Multiplication,Division}
    }
}
@page "/WholeFracCalc"
@using System.ComponentModel.DataAnnotations;
@using MauiAppMCQs.Models;
@using static MauiAppMCQs.Models.FractionEnums;
<h3>Whole Number (Divide/Multiply/Add/Substract )Fractions Calculator</h3>
 
 
<PageTitle>Calculator</PageTitle>
 
<div >
   
    <fieldset>
        <legend>Whole Number   </legend>
        <InputNumber @bind-Value="num1" disabled=@isdisabled />
        
     
    </fieldset>
         <fieldset disabled=@isdisabled>
        
        <InputRadioGroup @bind-Value="Calulation" >
            @foreach (var calculationItem in Enum.GetValues<Calulation>())
            {
                <div>
                    <label>
                        <InputRadio Value="calculationItem" />
                        @calculationItem
                    </label>
                </div>
            }
        </InputRadioGroup>
    </fieldset>
        <br />
   
    <br /> 
<div class="col-md-4">
        <table border="1" disabled=@isdisabled>
            <tr>
                <th colspan="2"> Fraction</th>
               
            </tr>
            <tr>
                 
                <td>
                    <input placeholder="Enter Numerator" disabled=@isdisabled @bind="@num2"> <br>
                    <input placeholder="Enter Denominator" disabled=@isdisabled @bind="@den2">
                </td>
            </tr>
        </table>
    </div>
        
    
    
    
    <legend>Step1 </legend>
        <br />
        <div>@operation</div>
        <br />
        
        <div>@tempresult</div>
        <br />
        <legend>Step2(HCF) </legend>
        <br />
        <div>@hcf</div>
        <br />
        <legend>Result</legend>
        <div>@result</div>
        <br />
        <div>@decresult</div>
    
    <br>
    <div>
        <button @onclick="Calculate">Calculate</button>
    </div>
    <div>
        @errmsg
    </div>
    <br>
    <button @onclick="Clear" >
        Clear
    </button>
    <br />
    @errmsg
</div>
@code {
    [Required, EnumDataType(typeof(Calulation))]
    public Calulation? Calulation { get; set; } = null; public string skip; public string type;
    private bool isdisabled = false; public string errmsg = "";
    public class FractionEnums
    {
        public enum Calulation { Addition, Substraction, Multiplication, Division }
    }
    public int num1; public int num2; public int den1=1; public int den2=0; public int resnum; public int resden;
    public int tempnum; public int tempden; public string result; public string tempresult;int hcf ; public string checkops; public string operation; public double decresult;
    private void Calculate()
    {
        den1 = 1;
        if ((num1 > -50) || (num1 <= -50))
        {
            isdisabled = true;
        }
        errmsg = "";
        skip = "N"; checkops = "N";
        if ((num1 <= -50))
        { skip = "Y"; isdisabled = false; }
        if ((num2 <= -50))
        { skip = "Y"; isdisabled = false; }
        if ((num1 > 50))
        { skip = "Y"; isdisabled = false; }
        if ((num2 > 50))
        { skip = "Y"; isdisabled = false; }
       
        if ((den2 > 50) || (den2 <= 1))
        { skip = "Y"; isdisabled = false; }
        if (skip == "N")
        {
            if (Calulation == MauiAppMCQs.Models.FractionEnums.Calulation.Addition)
            {
                checkops = "Y"; tempnum = num1 * den2 + num2 * den1; operation = "Addition   (numerator1 × denominator2 + numerator2 × 1)/( 1 × denominator2 )";
                tempden = den1 * den2;
                int i;
                int hcfin;int hcfout;
                hcfin = tempnum;
                hcfout = tempden;
                if(hcfin<0)
                { hcfin = hcfin * -1; }
                hcf=calchcf(hcfin, hcfout);
                tempresult = tempnum.ToString() + @"/" + tempden.ToString();
                resnum = tempnum / hcf; 
                resden = tempden / hcf;
                result = resnum.ToString() + @"/" + resden.ToString();
                if(resden==1)
                { result = resnum.ToString(); }
                double tempdec = (Convert.ToDouble(resnum) / Convert.ToDouble(resden));
                decresult = Math.Round(tempdec, 2);
            }
            if (Calulation == MauiAppMCQs.Models.FractionEnums.Calulation.Substraction)
            {
                checkops = "Y";
                tempnum = num1 * den2 - num2 * den1; operation = "Substraction (numerator1 × denominator2 - numerator2 x  1)/( 1 × denominator2 )";
                tempden = den1 * den2;
                int hcfin;
                int hcfout;
                hcfin = tempnum;
                hcfout = tempden;
                if (hcfin < 0)
                { hcfin = hcfin * -1; }
                int i;
                hcf = calchcf(hcfin, hcfout);
                resnum = tempnum / hcf;
                resden = tempden / hcf;
                tempresult = tempnum.ToString() + @"/" + tempden.ToString();
                result = resnum.ToString() + @"/" + resden.ToString();
                if (resden == 1)
                { result = resnum.ToString(); }
                double tempdec = (Convert.ToDouble(resnum) / Convert.ToDouble(resden));
                decresult = Math.Round(tempdec, 2);
            }
            if (Calulation == MauiAppMCQs.Models.FractionEnums.Calulation.Multiplication)
            {
                checkops = "Y";
                tempnum = num1 * num2; operation = "Multiplication ((numerator1 × numerator 2)/(1 × denominator2 ))";
                tempden = den1 * den2;
                int hcfin;
                int hcfout;
                hcfin = tempnum;
                hcfout = tempden;
                if (hcfin < 0)
                { hcfin = hcfin * -1; }
                int i;
                hcf = calchcf(hcfin, hcfout);
                resnum = tempnum / hcf;
                resden = tempden / hcf;
                tempresult = tempnum.ToString() + @"/" + tempden.ToString();
                result = resnum.ToString() + @"/" + resden.ToString();
                if (resden == 1)
                { result = resnum.ToString(); }
                double tempdec = (Convert.ToDouble(resnum) / Convert.ToDouble(resden));
                decresult = Math.Round(tempdec, 2);
            }
            if (Calulation == MauiAppMCQs.Models.FractionEnums.Calulation.Division)
            {
                checkops = "Y";
                tempnum = num1 * den2; operation = "Division ((numerator1 × denominator2)/( 1 × numerator 2 ))";
                tempden = den1 * num2;
                int hcfin;
                int hcfout;
                hcfin = tempnum;
                hcfout = tempden;
                if (hcfin < 0)
                { hcfin = hcfin * -1; }
                int i;
                hcf = calchcf(hcfin, hcfout);
                resnum = tempnum / hcf;
                resden = tempden / hcf;
                tempresult = tempnum.ToString() + @"/" + tempden.ToString();
                result = resnum.ToString() + @"/" + resden.ToString();
                if (resden == 1)
                { result = resnum.ToString(); }
                double tempdec = (Convert.ToDouble(resnum) / Convert.ToDouble(resden));
                decresult = Math.Round(tempdec, 2);
            }
        }
        else
        { errmsg = "Enter Whole Number or Numerator  between -50 and  50 and Denominator between 1 to 50 "; checkops = "Y"; }
        if(checkops == "N")
        { errmsg = "Check a Radio Button "; }
    } 
    private  int calchcf(int a, int b)
    { int i;
        int hcfin = a; int hcfout = b; int hcfres = 1;
        for (i = 1; i <= hcfin || i <= hcfout; ++i)
        {
            if (hcfin % i == 0 && hcfout % i == 0)
            {
                hcfres = i;
            }
        }
        return hcfres;
    }
    private void Clear()
    {
        num1 = 0; num2=0; den1 = 0;den2 = 0; isdisabled = false; resnum = 0; resden = 0;result = "";decresult = 0;
        tempnum = 0; tempden = 0;operation = "";
    }
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,518 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,746 Reputation points Microsoft Vendor
    2024-09-27T05:16:17.7633333+00:00

    Hello,

    Check one Radio button by Default .NET MAUI

    You can do this by setting Calulation? Calulation { get; set; } = MauiAppMCQs.Models.FractionEnums.Calulation.Addition like the following code.

    [Required, EnumDataType(typeof(Calulation))]
    public Calulation? Calulation { get; set; } = MauiAppMCQs.Models.FractionEnums.Calulation.Addition; public string skip; public string type;
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.