C#委托与事件_随机数与进度条练习题
题目:计算一个数的三角函数,绝对值和平方值
要求:
1.在窗体中的TextBox中输入一个数字。
2.把输入转换为double型,计算四个三角函数,返回值为double值,
用委托完成。
3.把刚才的数字转为int型,计算绝对值和平方,返回值为int型,
用事件完成。
4.点击按钮开始计算,结果在RichTextBox中显示,并记录生成时间。
5.第一次输入后的数是随机生成的,范围是[-180,360].进行上述计算。
6.每隔随机秒[1,7]计算一次。记录下生成的时间,年月日时分秒。
7.用进度条显示进度。
知识点:委托,事件,进度条,随机数
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
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();//初始化
}
static double sin;
static double cos;
static double tan;
static double cot;
int num1;
double num2;
double num3;
static int jdz;
static int pf;
public class Method
{
public double changeToDouble(String strNumber)//转换为double
{
double doubleNumber;
doubleNumber = double.Parse(strNumber);
return doubleNumber;
}
public void sanjiaoFunction(double doubleNumber)//得到三角函数
{
sin = Math.Sin(Math.PI / doubleNumber);
sin = Math.Round(sin, 2);
cos = Math.Cos(Math.PI / doubleNumber);
cos = Math.Round(cos, 2);
tan = Math.Tan(Math.PI / doubleNumber);
tan = Math.Round(tan, 2);
cot = 1 / tan;
cot = Math.Round(cot, 2);
}
public void jp(double doubleNumber)//计算绝对值,平方
{
int intNum = (int)doubleNumber;
jdz = Math.Abs(intNum);
pf = intNum * intNum;
}
}
//以下定义委托和事件
public delegate double toDoubleDelegate(String stringNumber);
static toDoubleDelegate tDD;
public delegate void getSanjiaoFunction(double doubleNumber);
static getSanjiaoFunction gSF;
public delegate void getjp(double doubleNumber);
static event getjp MyEvent;
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)//计算三角函数
{
if (int.TryParse(textBox1.Text, out num1) || double.TryParse(textBox1.Text, out num2))
{
num3 = double.Parse(textBox1.Text);
tDD= new toDoubleDelegate(new Method().changeToDouble);
}
else
{
MessageBox.Show("输入有误");
}
gSF = new getSanjiaoFunction(new Method().sanjiaoFunction);
try
{
gSF(tDD(textBox1.Text));
string time1=DateTime.Now.ToString();
richTextBox1.Text = " Sin:" + sin + " Cos:" + cos + " Tan:" + tan + " Cot:" + cot+" "+"NowTime:"+time1;
}
catch
{
string time2 = DateTime.Now.ToString();
richTextBox1.Text = "你个大傻子这也能输错" + " " + "NowTime:" + time2;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (int.TryParse(textBox1.Text, out num1) || double.TryParse(textBox1.Text, out num2))
{
num3 = double.Parse(textBox1.Text);
tDD = new toDoubleDelegate(new Method().changeToDouble);
}
else
{
MessageBox.Show("输入有误");
}
try
{
if (MyEvent == null)
{
MyEvent += new getjp(new Method().jp);
}
string time3 = DateTime.Now.ToString();
MyEvent(tDD(textBox1.Text));
richTextBox2.Text = "绝对值:" + jdz + " 平方:" + pf + " " + "NowTime:" + time3;
}
catch
{
string time4 = DateTime.Now.ToString();
richTextBox2.Text = "你个大傻子这也能输错" + " " + "NowTime:" + time4;
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
Random _random=new Random();
Stopwatch _stopwatch = new Stopwatch();
int _time = 2;
public void button3_Click(object sender, EventArgs e)
{
this.timer1.Enabled = !this.timer1.Enabled;
this.timer2.Enabled = this.timer1.Enabled;
if (this.timer1.Enabled == true)
{
this.button3.Text = "点击停止![在这里插入图片描述](https://img-blog.csdnimg.cn/20191027000302963.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NzQ5MDUz,size_16,color_FFFFFF,t_70)";
_stopwatch.Restart();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = _random.Next(-180, 360).ToString();
button1.PerformClick();
button2.PerformClick();
_time = _random.Next(1, 7);
this.timer1.Interval = _time * 1000;
_stopwatch.Restart();
}
private void timer2_Tick(object sender, EventArgs e)
{
this.progressBar1.Value = (int)_stopwatch.ElapsedMilliseconds / (_time * 10);
this.progressBar1.PerformStep();
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form1.Designer.cs
namespace WindowsFormsApp1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.button3 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer2 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(341, 83);
this.textBox1.Margin = new System.Windows.Forms.Padding(5);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(721, 36);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(139, 86);
this.label1.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(162, 25);
this.label1.TabIndex = 1;
this.label1.Text = "输入一个数:";
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 15F);
this.button1.Location = new System.Drawing.Point(135, 137);
this.button1.Margin = new System.Windows.Forms.Padding(5);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(177, 37);
this.button1.TabIndex = 2;
this.button1.Text = "三 角 函 数";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(135, 197);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(177, 37);
this.button2.TabIndex = 5;
this.button2.Text = "绝对值和平方";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(341, 143);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(721, 31);
this.richTextBox1.TabIndex = 7;
this.richTextBox1.Text = "";
this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged);
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(341, 203);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(721, 31);
this.richTextBox2.TabIndex = 8;
this.richTextBox2.Text = "";
//
// button3
//
this.button3.Location = new System.Drawing.Point(801, 349);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(261, 34);
this.button3.TabIndex = 9;
this.button3.Text = "自 动 生 成";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(135, 349);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(578, 36);
this.progressBar1.TabIndex = 11;
this.progressBar1.Click += new System.EventHandler(this.progressBar1_Click);
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// timer2
//
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(13F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1300, 750);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.button3);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Font = new System.Drawing.Font("宋体", 15F);
this.Margin = new System.Windows.Forms.Padding(5);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Timer timer2;
}
}
特别注意:
仅供参考学习,转载请附上原文链接
分享学习心得,如有侵权,望联系本人处理
还在读大学的程序员,项目经验少,如有纰漏,感谢指正
需要源代码请联系本人
谢谢配合
如果这篇文章对您有帮助,小小的点个赞,算是给小学弟的鼓励吧!谢谢大佬!!/呱呱.jpg