开发者

Easy Variable Question in Visual Studio 2010 Express Please help

开发者 https://www.devze.com 2023-03-05 19:54 出处:网络
I\'m trying to make a unit converter and what I have is one button and two textboxes. What I want the button when pressed to do is convert whatever is entered to the other side (what isnt entered)

I'm trying to make a unit converter and what I have is one button and two textboxes. What I want the button when pressed to do is convert whatever is entered to the other side (what isnt entered)

ALSO! Another thing that would be great would be how do I implement a drop down menu so I dont need to do so many buttons and fields? Thanks <3

TEXTBOX BUTTON TEXTBOX2 (say TEXTBOX is Fahrenheit and TEXTBOX2 is Celsius) I enter in the Fahrenheit and leave TEXTBOX2 and hit the button I should get the Celsius now. Simple enough right?

Here is what I have:

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 UnitConverter
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }



        private void button1_Click(object sender, RoutedEventArgs e)
        {

            if (boxc.Text=="")
                boxc.Text = (Convert.ToDou开发者_JS百科ble(boxf.Text) * 9 /5) +32;
        }


        }
    }

Sorry when it comes to numbers and strings I always get thrown for a loop :\ Thanks in advance for any help.


This line is wrong:

boxc.Text = (Convert.ToDouble(boxf.Text) * 9 / 5) + 32;

You're trying to put a double back into the textbox; you need to convert the result back to a string:

boxc.Text = ((Convert.ToDouble(boxf.Text) * 9 / 5) + 32).ToString();

Also, you mixed up the formulas; I believe you're trying to convert from Fahrenheit (the value in boxf) to Celsius (the value in boxc), but you're using the formula to convert from Celsius to Fahrenheit.


Replace

boxc.Text = (Convert.ToDouble(boxf.Text) * 9 /5) +32; 

With

boxc.Text = Convert.ToString((Convert.ToDouble(boxf.Text) * 9 /5) +32); 


    double c ;
try{
 c= double.Parse(boxf.Text);
}catch{
MessageBox.Show("Error!Invalid Data");
return;
}

    if (boxc.Text.Trim().length==0)
                    boxc.Text =( (c * 9 /5) +32).ToString();


Use this: boxc.Text = ((Convert.ToDouble(boxf.Text) * 9 / 5) + 32).ToString();

instead of boxc.Text = (Convert.ToDouble(boxf.Text) * 9 /5) +32;

0

精彩评论

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

关注公众号