简易VSExpress问题(IsPressed vs _Click)转换单位

时间:2011-05-12 18:15:32

标签: c# visual-studio-2010 visual-studio-express

我有这个并且它可以工作,但我得到的是相同的公式(_Click),它在开始时看起来像。我希望它每次重复使用相同的框和按钮,但每个单选按钮使用不同的公式。在此先感谢您的帮助。 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);
        }

    }
 }
}

谢谢:)它现在有效

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

名称空间UnitConverterFinal {     public partial class MainPage:PhoneApplicationPage     {         //构造函数         公共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); 
    }
 }

}

1 个答案:

答案 0 :(得分:0)

你需要的更像是这样(伪代码btw)。这基于我认为你是UI逻辑

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); 
        }
    }

更新了代码。我在一个WinForms项目中工作,它的工作原理。试试这种方式。