从大学计科院毕业之后,你顺利加入了一家初创软件公司,该公司需要为客户开发一款公司内部的即时通讯软件。软件部经理决定将文字聊天工作交付由你完成。请根据相关需求说明及概要编码提示,结合桌面应用程序开发所学知识,顺利完成上述软件的开发工作:
软件需求及编码提示:
本窗体包含一个主窗体,窗体大小:855 * 652。
窗体上方放置一个FlowLayoutPanel控件,BorderStyle属性设置为FixedSingle。
该FlowLayoutPanel的其他属性设置为:BackColor设置为白色,AutoScroll设置为true。
在FlowLayoutPanel下方放置一个RichTextBox,设置字体为:微软雅黑, 12pt。
在RichTextBox下方左右分别放置两个按钮,一个标题为“清空”,另一个标题为“发送”。
编写代码, 当用户点击发送的时候,用代码的方式生成一个Label控件。例如Label lbl = new Label(); lbl.Text=…利用上述方式,设置该Label的文本为RichTextBox中的内容,合理设置其宽度和高度,利用new Font(“微软雅黑”, 15)设置字体为;利用flowLayoutPanel1.Controls.Add添加上述Label到flowLayoutPanel上。至此,你可看到,你想要发送的内容显示在了上方panel上。
利用DateTime.Now.ToLocalTime()可以获得系统时间字符串,将你的名字 + 该当前时间字符串 + 要发送的内容—组合成一条消息,加以显示。具体显示效果如上图所示。
编写代码,当RichTextBox中内容为空的时候,不做处理。
编写代码,当RichTextBox发送完毕后,RichTextBox被重新设置为空。
编写代码,当用户点击清空按钮的时候,清空flowLayoutPanel1中添加的所有Label控件。
代码部分:
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 pravtice6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.Font = new Font("微软雅黑", 12);
}
private void button2_Click(object sender, EventArgs e)
{
if (richTextBox1.Text!="")
{
Label time = new Label();
time.Text = DateTime.Now.ToString();
Label lbl = new Label();
lbl.Text = "Echo" + "(" + time.Text + "):" + richTextBox1.Text;
lbl.Font = new Font("微软雅黑", 15);
lbl.Width = 400;
lbl.Height = 30;
flowLayoutPanel1.Controls.Add(lbl);
richTextBox1.Text = "";
}
}
private void button1_Click(object sender, EventArgs e)
{
flowLayoutPanel1.Controls.Clear();
}
}
}
运行结果: