利用C#实现inch和cm的互换(含源码)
2009-12-02 11:21
441 查看
原理:1inch = 1/12foot = 2.54cm = 25.4mm
说明:inch转cm时可精确转换,cm转inch时约等。
不当之处望指教。
点击下载:inchEXcm--liong.rar
---------
直接贴出源码:
//利用:1inch = 1/12foot = 2.54cm = 25.4mm 实现。
namespace inchEXcm
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private double f = 0;
private double i = 0;
private double c = 0;
private double r = 0;
//字符串strIn若为空返回0;若为实数返回此实数的双精度值;若含字符串返回-1.
private double GetNum(string strIn)
{
if (strIn == "")
{
return 0;
}
else
{
try
{
double re = Convert.ToDouble(strIn);
return re;
}
catch
{
return -1;
}
}
}
private void btnConvert_Click(object sender, EventArgs e)
{
if (radinch.Checked)
{
//inch to cm
f = GetNum(txtFoot.Text);
if (f == -1)
{
MessageBox.Show("Number only!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
i = GetNum(txtInch.Text);
if (i == -1)
{
MessageBox.Show("Number only!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
r = f * 12 * 2.54 + i * 2.54;
txtCm.Text = r.ToString();
}
if (radcm.Checked)
{
//cm to inch
c = GetNum(txtCm.Text);
if (c == -1)
{
MessageBox.Show("Number only!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
r = c / 2.54;
txtFoot.Text = Convert.ToInt32(r / 12-r%12/12).ToString();//实现强制取整
txtInch.Text = Convert.ToInt32(r % 12).ToString();
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
string strCB = "";
if (radinch.Checked)
{
strCB = txtCm.Text + "cm";
}
if (radcm.Checked)
{
strCB = txtFoot.Text + "foot " + txtInch.Text + "inch";
}
Clipboard.SetText(strCB);//复制到剪切板
MessageBox.Show("Message '"+strCB+"' has been copied!", ";)",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void radinch_CheckedChanged(object sender, EventArgs e)
{
txtInch.Text = "0";
txtFoot.Text = "0";
txtCm.Text = "0";
txtCm.Enabled = false;
txtFoot.Enabled = true;
txtInch.Enabled = true;
}
private void radcm_CheckedChanged(object sender, EventArgs e)
{
txtInch.Text = "0";
txtFoot.Text = "0";
txtCm.Text = "0";
txtCm.Enabled = true;
txtFoot.Enabled = false;
txtInch.Enabled = false;
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("-= inchEXcm v.01 =-/n/nliong/n20080402", "inchEXcm v0.1",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
转贴地址:
说明:inch转cm时可精确转换,cm转inch时约等。
不当之处望指教。
点击下载:inchEXcm--liong.rar
---------
直接贴出源码:
//利用:1inch = 1/12foot = 2.54cm = 25.4mm 实现。
namespace inchEXcm
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private double f = 0;
private double i = 0;
private double c = 0;
private double r = 0;
//字符串strIn若为空返回0;若为实数返回此实数的双精度值;若含字符串返回-1.
private double GetNum(string strIn)
{
if (strIn == "")
{
return 0;
}
else
{
try
{
double re = Convert.ToDouble(strIn);
return re;
}
catch
{
return -1;
}
}
}
private void btnConvert_Click(object sender, EventArgs e)
{
if (radinch.Checked)
{
//inch to cm
f = GetNum(txtFoot.Text);
if (f == -1)
{
MessageBox.Show("Number only!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
i = GetNum(txtInch.Text);
if (i == -1)
{
MessageBox.Show("Number only!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
r = f * 12 * 2.54 + i * 2.54;
txtCm.Text = r.ToString();
}
if (radcm.Checked)
{
//cm to inch
c = GetNum(txtCm.Text);
if (c == -1)
{
MessageBox.Show("Number only!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
r = c / 2.54;
txtFoot.Text = Convert.ToInt32(r / 12-r%12/12).ToString();//实现强制取整
txtInch.Text = Convert.ToInt32(r % 12).ToString();
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
string strCB = "";
if (radinch.Checked)
{
strCB = txtCm.Text + "cm";
}
if (radcm.Checked)
{
strCB = txtFoot.Text + "foot " + txtInch.Text + "inch";
}
Clipboard.SetText(strCB);//复制到剪切板
MessageBox.Show("Message '"+strCB+"' has been copied!", ";)",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void radinch_CheckedChanged(object sender, EventArgs e)
{
txtInch.Text = "0";
txtFoot.Text = "0";
txtCm.Text = "0";
txtCm.Enabled = false;
txtFoot.Enabled = true;
txtInch.Enabled = true;
}
private void radcm_CheckedChanged(object sender, EventArgs e)
{
txtInch.Text = "0";
txtFoot.Text = "0";
txtCm.Text = "0";
txtCm.Enabled = true;
txtFoot.Enabled = false;
txtInch.Enabled = false;
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("-= inchEXcm v.01 =-/n/nliong/n20080402", "inchEXcm v0.1",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
转贴地址:
相关文章推荐
- 利用Emgu.CV实现人脸识别详解 (C#)--附源码
- c#利用反射+特性实现简单的实体映射数据库操作类实现自动增删改查(三)带源码写好的持久层
- 利用Emgu.CV实现人脸识别详解 (C#)--附源码
- 利用C#实现生成PDF电子书源码
- 利用Emgu.CV实现人脸识别详解 (C#)--附源码
- 利用Emgu.CV实现人脸识别详解 (C#)--附源码
- 利用C#实现生成PDF电子书源码
- 利用Emgu.CV实现人脸识别详解 (C#)--附源码
- C#利用Web Service实现短信发送(二)
- C# 利用 TTS 来朗读文本的实现方式:
- 利用C#实现分布式数据库查询
- C#实现打印机窗口测试DEMO源码
- C#利用PropertyInfo反射类实现二个类属性值的比较
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- 根据经纬度求两点间距离实现源码(C#)-非常精确
- C#利用反射实现两个类的对象之间相同属性的值的复制
- VS C# .NET WebBrowser控件 利用插入JS代码,实现控制弹出IE窗口
- 在C#中利用DirectX实现声音播放(winForm)
- 利用 C# 实现任务栏通知窗口
- 利用C#实现分布式数据库查询