2021.11.16 .net——Windows应用程序开发
一、今日学习内容
今天完成了实验三
1. 掌握窗口控件的使用方法;
2. 掌握Windows 的编程基础。
1.编写一个计算器,练习在窗体上添加控件、调整控件的布局,设置或修改控件属性,
编写事件处理程序的方法。
(1)新建 windows 应用程序。在窗体 Form 上拖放一个 TextBox 控件、十六个 Button 控
件,整个窗体布局如下图所示。
在计算器中,增加四个功能键:x2,sqrt,log, ln 四个键,分别计算求平方,开方,
log,ln 值
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { richTextBox1.Text += "专业:" + listBox1.SelectedItem + "\n"; } private void groupBox1_Enter(object sender, EventArgs e) { } private void groupBox2_Enter(object sender, EventArgs e) { } private void richTextBox1_TextChanged(object sender, EventArgs e) { } private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked == true) richTextBox1.Text += "性别:" + radioButton1.Text + "\n"; } private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked == true) richTextBox1.Text += "性别:" + radioButton1.Text + "\n"; } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) //如果键入回车键,则向richTextBox1中添加姓名 richTextBox1.Text += "姓名:" + textBox1.Text + "\n"; } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) //如果键入回车键,则向richTextBox1中添加学号 richTextBox1.Text += "学号:" + textBox2.Text + "\n"; } private void comboBox1_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar == 13) && comboBox1.Text != "") richTextBox1.Text += "班级:" + comboBox1.Text + "\n"; } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked == true) richTextBox1.Text += "爱好:" + checkBox1.Text + "\n"; } private void checkBox2_CheckedChanged(object sender, EventArgs e) { if (checkBox2.Checked == true) richTextBox1.Text += "爱好:" + checkBox2.Text + "\n"; } private void checkBox3_CheckedChanged(object sender, EventArgs e) { if (checkBox3.Checked == true) richTextBox1.Text += "爱好:" + checkBox3.Text + "\n"; } private void checkBox4_CheckedChanged(object sender, EventArgs e) { if (checkBox4.Checked == true) richTextBox1.Text += "爱好:" + checkBox4.Text + "\n"; } private void checkBox5_CheckedChanged(object sender, EventArgs e) { if (checkBox5.Checked == true) richTextBox1.Text += "爱好:" + checkBox5.Text + "\n"; } private void button1_Click(object sender, EventArgs e) { //将文件以RTF格式保存到指定路径下 richTextBox1.SaveFile(@"F:\.net实验三\xinxi.rtf", RichTextBoxStreamType.RichText); } private void button2_Click(object sender, EventArgs e) { richTextBox1.Clear(); } private void button3_Click(object sender, EventArgs e) { //将指定文件加载到RichTextBox中 richTextBox1.LoadFile(@"F:\.net实验三\xinxi.rtf", RichTextBoxStreamType.RichText); } private void Form3_Load(object sender, EventArgs e) { } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } } }
2.自己设计并编写一个 Windows 应用程序,要求用到 TextBox、GroupBox、
RadioButton、CheckBox、ComboBox、ListBox 控件。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { richTextBox1.Text += "专业:" + listBox1.SelectedItem + "\n"; } private void groupBox1_Enter(object sender, EventArgs e) { } private void groupBox2_Enter(object sender, EventArgs e) { } private void richTextBox1_TextChanged(object sender, EventArgs e) { } private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked == true) richTextBox1.Text += "性别:" + radioButton1.Text + "\n"; } private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked == true) richTextBox1.Text += "性别:" + radioButton1.Text + "\n"; } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) //如果键入回车键,则向richTextBox1中添加姓名 richTextBox1.Text += "姓名:" + textBox1.Text + "\n"; } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) //如果键入回车键,则向richTextBox1中添加学号 richTextBox1.Text += "学号:" + textBox2.Text + "\n"; } private void comboBox1_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar == 13) && comboBox1.Text != "") richTextBox1.Text += "班级:" + comboBox1.Text + "\n"; } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked == true) richTextBox1.Text += "爱好:" + checkBox1.Text + "\n"; } private void checkBox2_CheckedChanged(object sender, EventArgs e) { if (checkBox2.Checked == true) richTextBox1.Text += "爱好:" + checkBox2.Text + "\n"; } private void checkBox3_CheckedChanged(object sender, EventArgs e) { if (checkBox3.Checked == true) richTextBox1.Text += "爱好:" + checkBox3.Text + "\n"; } private void checkBox4_CheckedChanged(object sender, EventArgs e) { if (checkBox4.Checked == true) richTextBox1.Text += "爱好:" + checkBox4.Text + "\n"; } private void checkBox5_CheckedChanged(object sender, EventArgs e) { if (checkBox5.Checked == true) richTextBox1.Text += "爱好:" + checkBox5.Text + "\n"; } private void button1_Click(object sender, EventArgs e) { //将文件以RTF格式保存到指定路径下 richTextBox1.SaveFile(@"F:\.net实验三\xinxi.rtf", RichTextBoxStreamType.RichText); } private void button2_Click(object sender, EventArgs e) { richTextBox1.Clear(); } private void button3_Click(object sender, EventArgs e) { //将指定文件加载到RichTextBox中 richTextBox1.LoadFile(@"F:\.net实验三\xinxi.rtf", RichTextBoxStreamType.RichText); } } }
二、遇到的问题
对于代码不熟悉,一些事件的匹配没有对上,导致运行不对,通过修改成功运行。