最近在学习C# 自己没事做的关于随机数和winform的练习 附上代码
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 EATRANDOM { public partial class Form1 : Form { int num_min, num_max,num_fina,num_fm2,num_sum; public Form1() { InitializeComponent(); this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; } public void num_j() { num_fm2--; } private void button1_Click(object sender, EventArgs e) { int start,end;//定义两个变量用于接收tryparese的返回值 if(!(int.TryParse(textBox1.Text,out start))){//判断文本框里输入的是否是数字和为空 MessageBox.Show("您输入的下限有误,请重新输入!"); textBox1.Focus(); textBox1.SelectAll(); return; } if (!(int.TryParse(textBox2.Text, out end))) { MessageBox.Show("您输入的上限有误,请重新输入!"); textBox2.Focus(); textBox2.SelectAll(); return; } if (start>end) { MessageBox.Show("您输入的下限应该小于上限,请重新输入!"); textBox1.Focus(); textBox1.SelectAll(); return; } num_min = int.Parse(textBox1.Text);//提取文本框的内容转换为int型 num_max = int.Parse(textBox2.Text)+1; Random rd = new Random();//创建随机数 num_fina = rd.Next(num_min, num_max); textBox3.Text = num_fina.ToString();//显示内容,注意转换成字符串,否则无法显示 } private void label4_Click(object sender, EventArgs e)//程序退出 { Application.Exit(); } private void textBox1_KeyPress(object sender, KeyPressEventArgs e)//阻止文本框输入除数字和退格键以外的任意字符 { if(e.KeyChar<48||e.KeyChar>57){ e.Handled = true;//表示不能输入 }if(e.KeyChar==8){//退格键可以输入 e.Handled = false; }if(textBox1.SelectionStart==0 && e.KeyChar=='0'){ e.Handled = true; } } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar < 48 || e.KeyChar > 57) { e.Handled = true; } if (e.KeyChar == 8) { e.Handled = false; } if (textBox1.SelectionStart == 0 && e.KeyChar == '0') { e.Handled = true; } } private void textBox4_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar < 48 || e.KeyChar > 57) { e.Handled = true; } if (e.KeyChar == 8) { e.Handled = false; } if (textBox1.SelectionStart == 0 && e.KeyChar == '0') { e.Handled = true; } } private void textBox4_Enter(object sender, EventArgs e) { int result; if (!(int.TryParse(textBox4.Text, out result))) { textBox4.Text = ""; return; } } private void btn2_Click(object sender, EventArgs e) { int start, end, result; if (!(int.TryParse(textBox1.Text, out start))) { MessageBox.Show("您输入的下限有误,请重新输入!"); textBox1.Focus(); textBox1.SelectAll(); return; } if (!(int.TryParse(textBox2.Text, out end))) { MessageBox.Show("您输入的上限有误,请重新输入!"); textBox2.Focus(); textBox2.SelectAll(); return; } if (start > end) { MessageBox.Show("您输入的下限应该小于上限,请重新输入!"); textBox1.Focus(); textBox1.SelectAll(); return; } if (num_fm2 >= 5) { MessageBox.Show("请不要同时生成太多随机数据表以免死机!!"); } if (!(int.TryParse(textBox4.Text, out result))) { MessageBox.Show("您输入的数量有误,请重新输入!"); textBox4.Focus(); textBox4.SelectAll(); return; } num_min = int.Parse(textBox1.Text); num_max = int.Parse(textBox2.Text) + 1; num_sum = int.Parse(textBox4.Text); num_fm2++; Form2 fm = new Form2(num_min,num_max,num_sum); fm.Owner = this; fm.Show(); } } }
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 EATRANDOM { public partial class Form2 : Form { bool beginMove = false; int currentXPosition = 0; int currentYPosition = 0; public Form2(int min,int max,int length) { InitializeComponent(); //this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; Random rd = new Random(); for(int i=0;i<length;i++){//循环在textbox中出插入数据 int num = rd.Next(min, max); textBox1.Text += num.ToString() +"\r\n"; } } private void label2_Click(object sender, EventArgs e) { this.DestroyHandle();//销毁本窗体程序,不能再用Application.Exit();全程序退出了,关于只退出子窗体的方法自己也不是很清楚,有更好的方法麻烦留个言告诉我一声 Form1 frm1; frm1 = (Form1) this.Owner; frm1.num_j(); this.Hide(); } //一下是在取消窗体样式的情况下移动窗体(即没有标题栏最小化,关闭等的时候移动) private void Form2_MouseMove(object sender, MouseEventArgs e) { if (beginMove) { this.Left +=MousePosition.X - currentXPosition; this.Top += MousePosition.Y - currentYPosition; currentXPosition = MousePosition.X; currentYPosition = MousePosition.Y; } } private void Form2_MouseDown(object sender, MouseEventArgs e) { beginMove = true; currentXPosition = MousePosition.X; currentYPosition = MousePosition.Y; } private void Form2_MouseUp(object sender, MouseEventArgs e) { beginMove = false; } } }
效果图两张
源码下载:http://pan.baidu.com/share/link?shareid=1219268688&uk=104008118