c#:基础杂项
ylbtech_c#
1,C#弹出对话框
MessageBox.Show(this, "请输入一个数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop); txt1.Focus(); txt1.SelectAll();//文本框中的内容全选 MessageBox.Show(this, "留言记录添加成功,要查看留言吗?", "选择一个选项", MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question); MessageBox.Show("这里是信息"); MessageBox.Show(this, "显示的内容", "标题", MessageBoxButtons.YesNoCancel); MessageBox.Show(this, "显示的内容", "标题", MessageBoxButtons.YesNoCancel,MessageBoxIcon.Information);
2,C#多文档窗体
MDI窗体(多文档窗体) 是否为多文档窗体 Form属性:IsMdiContainer true|false Form2 frm = new Form2(); frm.MdiParent = this;//设置MDI的父窗体 frm.Show();
3,C#枚举
ArrayList list = new ArrayList(); list.Add("第一"); list.Add("第二"); //获得一个ArrayList的枚举对象 IEnumerator ie = list.GetEnumerator(); while (ie.MoveNext()) { //ie.Current;//返回当前枚举的对象 Console.WriteLine(ie.Current.ToString()); }
4,C#默认画笔
Graphics g = this.CreateGraphics();//画笔 Font f = new Font("隶书", 30, FontStyle.Bold);//字体 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, 10, 10), Color.Blue, Color.Blue, 1.3f);//笔刷 g.DrawString("你好", f, brush, 100, 200);//画字符串 g.FillEllipse(brush, 100, 100, 200, 50);//椭圆 g.FillRectangle(brush, 200, 200, 200, 50);//矩形 g.DrawLine(new Pen(Color.Red), 10, 10, 300, 300);//线段
5,C#随机数
Random r = new Random(); int num = 0;//要猜的数
6,C#-图片动画
private void button1_Click(object sender, EventArgs e) { pb.Image = img.Images[0]; } private void button2_Click(object sender, EventArgs e) { pb.Image = img.Images[1]; } private void timer1_Tick(object sender, EventArgs e) { pb.Image = imgList.Images[index]; index++; if (index > 6) index = 0; } private void Form1_Load(object sender, EventArgs e) { pb.Image = imgList.Images[index]; index++; if (index > 6) index = 0; }
7,C#线程
using System.Threading;//线程 private void Form1_Load(object sender, EventArgs e) { t.Start();//启动线程 } public static void hello() { while (true) { Thread.Sleep(1000); } }
8,C#右键菜单事件
//ContextMenuStrip private void Form1_MouseUp(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Right) cm.Show(this, e.Location); }
9,C#自动画笔
public partial class Form1 : Form { LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, 10, 10), Color.Blue, Color.Blue, 1.3f); int x, y, width, height; bool isPress = false;//是否按下鼠标 public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { } private void Form1_MouseDown(object sender, MouseEventArgs e) { //获得起始点(鼠标坐标) x = e.X; y = e.Y; isPress = true; } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (isPress) { width = e.X - x; height = e.Y - y; Graphics g = this.CreateGraphics(); g.Clear(Color.White);//清空画板,并填充颜色 g.FillEllipse(brush, x, y, width, height); } } private void Form1_MouseUp(object sender, MouseEventArgs e) { isPress = false; } }
10,
11,