“吾日三省吾身”小应用
本小应用的名字叫做“吾日三省吾身”,其设计目的在于通过提问的方式让用户对自己一天的生活进行总结和反省,界面上出现的三个问题分别对应了孟子所言的“为人谋而不忠乎”“与朋友交而不信乎”“传不习乎”。用户通过小应用可以对自己进行每日三省,每天都激励自己努力成为一个仁人君子,奋发勇为。
本小应用需要用到两个窗体,第一个窗体中需要三个label(分别对应了每日提问的三个问题)、三个textBox(分别用于存放用户的回答结果)、一个dateTimePicker(显示当天时间)、一个groupBox(用于放置“自我评价”的三个选项)、三个radioButton(“自我评价”的三个表现)、一个richTextBox(将三个问题的自省结果以及自我评价总结在一起)、一个pictureBox(设计窗体背景)、两个Button(“输出”用于将自省结果和自我评价输出到richTextBox中,“今日总结”用于弹出第二个窗体)。
第二个窗体只需用到三个控件——dateTimePicker(显示当天时间)、Button(点击后可将第一个窗体中richTextBox传到此窗体的richTextBox中)、richTextBox(自省结果以及自我评价的总结)。
代码如下所示
//窗体1(Form1)代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)//将三个label的背景设为透明,与背景图片融为一体
{
label1.BackColor = Color.Transparent;
label1.Parent = pictureBox1;//将pictureBox1设为标签的父控件
//pictureBox1.Controls.Add(label1);
label1.Location = new Point(18, 35);//重新设定标签的位置,这个位置时相对于父控件的左上角
label2.BackColor = Color.Transparent;
label2.Parent = pictureBox1;//将pictureBox1设为标签的父控件
//pictureBox1.Controls.Add(label1);
label2.Location = new Point(18, 85);//重新设定标签的位置,这个位置时相对于父控件的左上角
label3.BackColor = Color.Transparent;
label3.Parent = pictureBox1;//将pictureBox1设为标签的父控件
//pictureBox1.Controls.Add(label1);
label3.Location = new Point(18, 140);//重新设定标签的位置,这个位置时相对于父控件的左上角
}
private void button1_Click(object sender, EventArgs e)//将自我反省结果和自我评价输出到下方的richTextBox控件中
{
if (radioButton1.Checked == true)
{
this.richTextBox1.Text = "今天全力以赴完成的任务是:" + this.textBox1.Text + "\r\n" + "今天和人交往的感触是:" + this.textBox2.Text + "\r\n"
+ "今天温习过的知识是:" + this.textBox3.Text + "\r\n" + "自我评价:" + this.radioButton1.Text;
}
if (radioButton2.Checked == true)
{
this.richTextBox1.Text = "今天全力以赴完成的任务是:" + this.textBox1.Text + "\r\n" + "今天和人交往的感触是:" + this.textBox2.Text + "\r\n"
+ "今天温习过的知识是:" + this.textBox3.Text + "\r\n" + "自我评价:" + this.radioButton2.Text;
}
if (radioButton3.Checked == true)
{
this.richTextBox1.Text = "今天全力以赴完成的任务是:" + this.textBox1.Text + "\r\n" + "今天和人交往的感触是:" + this.textBox2.Text + "\r\n"
+ "今天温习过的知识是:" + this.textBox3.Text + "\r\n" + "自我评价:" + this.radioButton3.Text;
}
}
private void button2_Click(object sender, EventArgs e)//点击后可打开第二个窗体
{
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
}
}
//窗体2(Form2)代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;
this.richTextBox1.Text = f1.richTextBox1.Text;
}
}
}
注意要将窗体1中richTextBox1的Modifiers属性设置为Public。
最后的效果图如下所示——
最后感谢您看到了这里,虽然这是一个极其简单的小应用,但也希望能对您有所启发,小应用中做得不足的地方还请大家多多批评指正,谢谢大家!