开发者

Easy VSExpress Question (IsPressed vs _Click) Converting units

开发者 https://www.devze.com 2023-03-05 06:07 出处:网络
I have this and it works but I am getting the same formula (_Click) one it looks like at the start. I want it to reuse the same boxes and button each time but with different formulas with each radio b

I have this and it works but I am getting the same formula (_Click) one it looks like at the start. I want it to reuse the same boxes and button each time but with different formulas with each radio button. Thanks in advance for any help. V/r

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace UnitConverterFinal
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (boxa.Text == "")
            boxa.Text = Convert.ToString((Convert.ToDouble(boxb.Text) * 9 / 5) + 32);
        else
            boxb.Text = Convert.ToString((Convert.ToDouble(boxa.Text) - 32)  *  5/9);
    }

    private void radioButton1_Checked(object sender, RoutedEventArgs e)
    {
        textBlock1.Text = "Farenheight to Celsius";
        boxb.Text = "";
        boxa.Text = "";

        if(button1.IsPressed)
        {
            if (boxa.Text == "")
                boxa.Text = Convert.ToString((Convert.ToDouble(boxb.Text) * 9 / 5) + 32);
            else
                boxb.Text = Convert.ToString((Convert.ToDouble(boxa.Text) - 32) * 5 / 9);
        }

    }

    private void radioButton2_Checked(object sender, RoutedEventArgs e)
    {
        textBlock1.Text = "Centimeters to Inches";
        boxa.Text = "";
        boxb.Text = "";

        if (button1.IsPressed)
        {
            if (boxa.Text == "")
                boxa.Text = Convert.ToString(Convert.ToDouble(boxb.Text) * 2.54);
            else
                boxb.Text = Convert.ToString(Convert.ToDouble(boxa.Text)  * 0.3937008);
        }

    }
 }
}

Thank you :) it works now

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using开发者_如何学Python System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls;

namespace UnitConverterFinal { public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); }

    private void button1_Click(object sender, RoutedEventArgs e)
    {

      if (radioButton1.IsChecked == true) { CalcDegrees(); }
      if (radioButton2.IsChecked == true) { CalcDistance(); }

    }


    private void radioDegrees_checked(object sender, RoutedEventArgs e)
    {
    CalcDegrees();
    }

    private void radioDistance_checked(object sender, RoutedEventArgs e)
    {
    CalcDistance();
    }

    private void CalcDegrees()
    {
     //Move your calc code here
        if (!string.IsNullOrEmpty(tbA.Text)) { FtoC(); return; }
        if (!string.IsNullOrEmpty(tbB.Text)) { CtoF(); return; }
    }

    private void CalcDistance()
    {
    //Move your calc code here
        if (!string.IsNullOrEmpty(tbA.Text)) { InchToCent(); return; }
        if (!string.IsNullOrEmpty(tbB.Text)) { CentToInch(); return; }

    }

    private void FtoC()
    {
         tbB.Text = Convert.ToString((Convert.ToDouble(tbA.Text) * 9 / 5) + 32); 
    }

    private void CtoF()
    {
        tbA.Text = Convert.ToString((Convert.ToDouble(tbB.Text) - 32) * 5 / 9); 
    }

    private void InchToCent()
    {
        tbB.Text = Convert.ToString(Convert.ToDouble(tbA.Text) * 2.54);             
    }

    private void CentToInch()
    {
        tbA.Text = Convert.ToString(Convert.ToDouble(tbB.Text) * 0.3937008); 
    }
 }

}


what you need is more like this (psuedo code btw). This is based on what I think you're UI logic is

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioDegrees.Checked) { CalcDegrees(); }
            if (radioDistance.Checked) { CalcDistance(); }
        }

        private void radioDegrees_CheckedChanged(object sender, EventArgs e)
        {
            CalcDegrees();
        }

        private void radioDistance_CheckedChanged(object sender, EventArgs e)
        {
            CalcDistance();
        }

        private void CalcDegrees()
        {
            if (!string.IsNullOrEmpty(tbA.Text)) { FtoC(); return; }
            if (!string.IsNullOrEmpty(tbB.Text)) { CtoF(); return; }
        }

        private void CalcDistance()
        {
            if (!string.IsNullOrEmpty(tbA.Text)) { InchToCent(); return; }
            if (!string.IsNullOrEmpty(tbB.Text)) { CentToInch(); return; }
        }

        private void FtoC()
        {
             tbB.Text = Convert.ToString((Convert.ToDouble(tbA.Text) * 9 / 5) + 32); 
        }

        private void CtoF()
        {
            tbA.Text = Convert.ToString((Convert.ToDouble(tbB.Text) - 32) * 5 / 9); 
        }

        private void InchToCent()
        {
            tbB.Text = Convert.ToString(Convert.ToDouble(tbA.Text) * 2.54);             

        }

        private void CentToInch()
        {
            tbA.Text = Convert.ToString(Convert.ToDouble(tbB.Text) * 0.3937008); 
        }
    }

Updated the code. I worked this in a WinForms project and it works. Try it this way.

0

精彩评论

暂无评论...
验证码 换一张
取 消