经过几天学习,写出了一个简单的winform应用程序,贴出源码,以备不时之需。

软件启动后的界面如下图所示:

如图,该程序由6个label、8个comboBox、8个textBox和4个button组成。右边4个textBox设置ReadOnly属性为true。

软件启动时,可以让comboBox显示默认项,需要用到comboBox.SelectedIndex语句,默认情况下,comboBox.SelectedIndex="-1"(即默认不显示任何项),将-1改为0即可显示第一项。将代码放到窗体的Load事件里。代码实例:

private void MainForm_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 1;
comboBox3.SelectedIndex = 0;
comboBox4.SelectedIndex = 1;
comboBox5.SelectedIndex = 0;
comboBox6.SelectedIndex = 1;
comboBox7.SelectedIndex = 0;
comboBox8.SelectedIndex = 1;
}

  

按下确定按钮,执行转换函数,计算结果转换为string类型,并将其赋值给textBox.Text,代码实例:

        private void button4_Click(object sender, EventArgs e)
{
string str1, str2;
str1=Convert.ToString(comboBox7.SelectedItem);
str2=Convert.ToString(comboBox8.SelectedItem);
double d1, d2;
if (textBox7.Text == "")
{
textBox7.Text = "";
d1 = ;
}
else
d1 = Convert.ToDouble(textBox7.Text);
if (str1 == str2)
{
d2 = d1;
textBox8.Text = Convert.ToString(d2);
}
else
{
if(str1 == "摄氏度" && str2 == "华氏度")
{
d2=1.8*d1+;
textBox8.Text = Convert.ToString(d2);
}
if(str1 == "摄氏度" && str2 == "开氏度")
{
d2=d1+273.15;
textBox8.Text = Convert.ToString(d2);
}
if(str1 == "华氏度" && str2 == "摄氏度")
{
d2=(d1-)/1.8;
textBox8.Text = Convert.ToString(d2);
}
if(str1 == "华氏度" && str2 == "开氏度")
{
d2=(d1-)/1.8+273.15;
textBox8.Text = Convert.ToString(d2);
}
if (str1 == "开氏度" && str2 == "摄氏度")
{
d2 = d1 - 273.15;
textBox8.Text = Convert.ToString(d2);
}
if (str1 == "开氏度" && str2 == "华氏度")
{
d2 = (d1 - 273.15) * 1.8 + ;
textBox8.Text = Convert.ToString(d2);
}
}
}

使输入框禁止输入除退格键、数字键和小数点键之外的按键(温度的转换可以输入负号),防止用户输入非数字字符使程序发生错误。在keypress事件中添加相关代码,代码实例:

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != '\b' && e.KeyChar != )//允许输入退格键和小数点键
{
if ((e.KeyChar < '') || (e.KeyChar > ''))//允许输入0-9数字
{
e.Handled = true;
}
}
}

c#简单案例--单位转换器的更多相关文章

  1. [Design Pattern] Adapter Pattern 简单案例

    Adapter Pattern, 即适配器模式,用于连接两个不兼容的接口,属于结构类的设计模式. 或者叫做,转换器模式. 下面是一个转换器模式简单案例. 假设已有 AudioPlayer 专门播放 m ...

  2. Servlet请求头response应用简单案例

    Servlet请求头response应用简单案例:访问AServlet重定向到BServlet,5秒后跳到CServlet,并显示图片: AServlet package cn.yzu; import ...

  3. winform 通过 html 与swf 交互 简单案例

    在上一篇 winform 与 html 交互 简单案例 中讲了winform与html之间的简单交互,接下来的内容是在winform中以html为中转站,实现将swf嵌入winform中并实现交互. ...

  4. [Design Pattern] Front Controller Pattern 简单案例

    Front Controller Pattern, 即前端控制器模式,用于集中化用户请求,使得所有请求都经过同一个前端控制器处理,处理内容有身份验证.权限验证.记录和追踪请求等,处理后再交由分发器把请 ...

  5. [Design Pattern] Observer Pattern 简单案例

    Observer Pattern,即观察者模式,当存在一对多关系,例如一个对象一有变动,就要自动通知被依赖的全部对象得场景,属于行为类的设计模式. 下面是一个观察者模式的简单案例. Observer ...

  6. [Design Pattern] Mediator Pattern 简单案例

    Meditor Pattern,即调解模式,用一个调解类类处理所有的沟通事件,使得降低多对象之间的沟通难度,属于行为类的设计模式.为了方便理解记忆,我也称其为,沟通模式. 下面是一个调解模式的简单案例 ...

  7. [Design Pattern] Iterator Pattern 简单案例

    Iterator Pattern,即迭代时模式,按照顺序依次遍历集合内的每一个元素,而不用了解集合的底层实现,属于行为类的设计模式.为了方便理解记忆,我也会称其为遍历模式. 下面是一个迭代器模式的简单 ...

  8. [Design Pattern] Command Pattern 简单案例

    Command Pattern, 即命令模式,把一个命令包裹在一个对象里面,将命令对象传递给命令的执行方,属于行为类的设计模式 下面是命令模式的一个简单案例. Stock 代表被操作的对象.Order ...

  9. [Design Pattern] Proxy Pattern 简单案例

    Proxy Pattern, 即代理模式,用一个类代表另一个类的功能,用于隐藏.解耦真正提供功能的类,属于结构类的设计模式. 下面是 代理模式的一个简单案例. Image 定义接口,RealImage ...

随机推荐

  1. HTop依赖包

    htop 是一个 Linux 下的交互式的进程浏览器,可以用来替换Linux下的top命令. 1.安装HTop时需要先安装依赖包:rpmforge-release-0.5.3-1.el6.rf.x86 ...

  2. MySQL数据类型--与MySQL零距离接触2-13MySQL唯一约束

    虽然字段可以为空值,但是如果存储多个空值,如何确保唯一性?其实只会存储一个空值. 提示错误:Tom已经存储过,所以唯一约束在起作用. 一张表中:主键约束只能有一个,唯一约束可以有多个.在创建索引的时候 ...

  3. obv15 案例4,待日后分析

    obv

  4. 使用Spark下的corr计算皮尔森相似度Pearson时,报错Can only zip RDDs with same number of elements in each partition....

    package com.huawei.bigdata.spark.examples import org.apache.spark.mllib.stat.Statistics import org.a ...

  5. C#学习入门第一篇

    1. using System; using System.Collections.Generic; using System.Ling; using System.Text; using Syste ...

  6. nginx配置文件优化

    nginx配置优化     #定义Nginx运行的用户和用户组user  www  www: #启动工作进程,通常设置成和cpu的数量相等worker_processes  8:   最多开启8个,8 ...

  7. 安装redis脚本

    #!/bin/bash#Function: Install redis#Author: DengYong#Date: 20151126 cd /root/packagewget http://down ...

  8. linux命令目录

    一.文件和目录.(文件目录的增删改查) ls pwd cd mkdir touch rmdir ln dd rm cp mv nl cat tac more less head tail stat # ...

  9. Chrome 扩展

    http://www.cnblogs.com/coco1s/p/8004510.html

  10. CALL与retn

    一.CALL 例如: 004013D9      CALL 00401C4C     //ESP = 0060F9C8 004013DE 相当于 sub esp,0x4; //ESP = 0060F9 ...