【C#】上机实验七
1、开发一个窗体应用程序,窗体上能接收华氏温度或者摄氏温度,点击相应按钮可以相互转换。 要求转换后的华氏温度或者摄氏温度保留小数点后3位,程序中要有异常处理结构。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Myform1 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void label1_Click(object sender, EventArgs e) 20 { 21 22 } 23 24 private void button1_Click(object sender, EventArgs e) 25 { 26 try 27 { 28 textBox2.Text = htc(textBox1.Text).ToString("f3"); 29 } 30 catch (Exception ex) 31 { 32 Text = ex.Message; 33 } 34 } 35 36 private void button2_Click(object sender, EventArgs e) 37 { 38 try 39 { 40 textBox1.Text = cth(textBox2.Text).ToString("f3"); 41 } 42 catch (Exception ex) 43 { 44 Text = ex.Message; 45 } 46 } 47 48 public double htc( double x ){ 49 return (x - 32) * 5 / 9; 50 } 51 52 public double htc(string s) 53 { 54 double x = double.Parse(s); 55 return (x - 32) * 5 / 9; 56 } 57 58 public double cth(double x) 59 { 60 return (x * 9 / 5) + 32; 61 } 62 public double cth(string s) 63 { 64 double x = double.Parse(s); 65 return (x * 9 / 5) + 32; 66 } 67 68 } 69 }
2、设计一个窗体应用程序,输入一个字符,判定此字符是数字、大写字母、小写字母,还是其它字符。 要求: (1)类中定义公有方法(判断字符归属) (2)在按钮单击事件中调用该方法及返回值完成判断。 (3)程序中要有异常处理结构。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Myproject2 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void button1_Click(object sender, EventArgs e) 20 { 21 string s = textBox1.Text; 22 if (s.Length > 1) 23 { 24 Text = "请输入一个字符"; 25 textBox1.Text = ""; 26 textBox2.Text = ""; 27 MessageBox.Show("请输入一个字符", "Warming", MessageBoxButtons.OK); 28 } 29 30 if( s.Length == 1 ) 31 try 32 { 33 char ch = s[0]; 34 if ('a' <= ch && ch <= 'z') { 35 textBox2.Text = "小写字母"; 36 } 37 else if ('A' <= ch && ch <= 'Z') { 38 textBox2.Text = "大写字母"; 39 } 40 else if ('0' <= ch && ch <= '9') 41 { 42 textBox2.Text = "数字"; 43 } 44 else { 45 textBox2.Text = "其他字符"; 46 } 47 } 48 catch (Exception ex) 49 { 50 Text = ex.Message; 51 } 52 } 53 54 private void button2_Click(object sender, EventArgs e) 55 { 56 textBox1.Text = ""; 57 textBox2.Text = ""; 58 } 59 60 } 61 }
3、开发一个窗体应用程序,可以根据圆的半径计算圆的面积。具体要求如下: (1)设计一个方法求圆的面积。 (2)求圆面积的方法要定义成重载方法,既能接收整形参数,也能接收单精度类型参数。 (3)程序中要有异常处理结构。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Myproject3 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void button1_Click(object sender, EventArgs e) 20 { 21 string str = textBox1.Text; 22 double d = 0; 23 int x = 0; 24 bool Is_int = false; 25 bool Is_double = false; 26 27 if (Is_int = int.TryParse(str, out x)){} 28 else if ( Is_double = double.TryParse(str, out d)){} 29 30 if (Is_int || Is_double) 31 { 32 try 33 { 34 if (Is_int) 35 { 36 textBox2.Text = (Math.PI * x * x).ToString("f2"); 37 } 38 else 39 { 40 textBox2.Text = (Math.PI * d * d).ToString("f2"); 41 } 42 43 } 44 catch (Exception ex) 45 { 46 Text = ex.Message; 47 } 48 } 49 else 50 { 51 MessageBox.Show("请输入数字", "Warning", MessageBoxButtons.OK); 52 textBox1.Text = textBox2.Text = ""; 53 54 } 55 } 56 } 57 }
4、设计一个窗体应用程序,练习最基本的控件使用,实现单选按钮、复选按钮和命令按钮的相关功能。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Myproject4 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void groupBox1_Enter(object sender, EventArgs e) 20 { 21 22 } 23 private FontFamily fontFamily = new FontFamily("宋体"); 24 private FontStyle Style = FontStyle.Regular; 25 private float Size = 10 ; 26 private void radioButton1_CheckedChanged(object sender, EventArgs e) 27 { 28 fontFamily = new FontFamily("黑体"); 29 textBox1.Font = new Font(fontFamily, Size, Style); 30 } 31 32 private void radioButton2_CheckedChanged(object sender, EventArgs e) 33 { 34 fontFamily = new FontFamily("楷体"); 35 textBox1.Font = new Font(fontFamily, Size, Style); 36 } 37 38 private void checkBox1_CheckedChanged(object sender, EventArgs e) 39 { 40 Style ^= FontStyle.Bold; 41 textBox1.Font = new Font(fontFamily, Size, Style); 42 } 43 44 private void checkBox2_CheckedChanged(object sender, EventArgs e) 45 { 46 Style ^= FontStyle.Italic; 47 textBox1.Font = new Font(fontFamily, Size, Style); 48 } 49 50 private void radioButton4_CheckedChanged(object sender, EventArgs e) 51 { 52 Size = 15; 53 textBox1.Font = new Font(fontFamily, Size, Style); 54 } 55 56 private void radioButton3_CheckedChanged(object sender, EventArgs e) 57 { 58 Size = 30; 59 textBox1.Font = new Font(fontFamily, Size, Style); 60 } 61 62 private void radioButton6_CheckedChanged(object sender, EventArgs e) 63 { 64 textBox1.ForeColor = Color.Red; 65 } 66 67 private void radioButton5_CheckedChanged(object sender, EventArgs e) 68 { 69 textBox1.ForeColor = Color.Blue ; 70 } 71 72 private void button1_Click(object sender, EventArgs e) 73 { 74 textBox1.Copy(); 75 } 76 77 private void button2_Click(object sender, EventArgs e) 78 { 79 textBox1.Cut(); 80 } 81 82 private void button3_Click(object sender, EventArgs e) 83 { 84 textBox1.Paste(); 85 } 86 87 private void button4_Click(object sender, EventArgs e) 88 { 89 textBox1.Undo(); 90 } 91 92 } 93 }