1.前言
项目背景:公司年会要进行抽奖活动,需要一个抽奖小程序。
项目要求:1.能添加员工信息。
2.能悄无声息的 让 老板的二大爷 抽到大奖。
创建新winform项目 xcx
创建data.txt文件
添加窗口
添加控件{
button
DataGridView
textbox
}
主界面信息管理案件添加事件:
private void button_GM_Click(object sender, EventArgs e)
{
new GM().Show();
}
2.信息管理系统
主要涉及对txt文件的读取、添加、删除、保存等。
读取:string[] data = File.ReadAllLines(data_path);
保存:File.WriteAllLines(data_path, dataToSave);
编辑:放入List<string> tempList
展示: 基于DataGridView控件
DataSource 设置DataGridView 控件的数据源,常用的数据源类型有:DataTable;
Columns: 通过此属性可以访问或操作列集合中的列。Rows: 通过此属性可以访问或操作行集合中的行。
所以:
private void RefreshDataGridView()
{
DataTable dt = new DataTable();
dt.Columns.Add("姓名");
dt.Columns.Add("职位");
dt.Columns.Add("工号");
foreach (string line in data)
{
string[] parts = line.Split(',');
dt.Rows.Add(parts[0], parts[1], parts[2]);
}
dataGridView1.DataSource = dt;
}
代码如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows.Forms;
namespace xcx
{
public partial class GM : Form
{
public static string data_path = "D:\\VS\\works\\xcx\\xcx\\data.txt";
public static string[] data = File.ReadAllLines(data_path);
public GM()
{
InitializeComponent();
InitializeDataGridView();
}
// 初始化 DataGridView
private void InitializeDataGridView()
{
DataGridViewButtonColumn deleteButtonColumn = new DataGridViewButtonColumn();
deleteButtonColumn.Name = "Delete";
deleteButtonColumn.HeaderText = "操作";
deleteButtonColumn.Text = "选择";
deleteButtonColumn.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Add(deleteButtonColumn);
RefreshDataGridView();
}
// 刷新 DataGridView 数据
private void RefreshDataGridView()
{
DataTable dt = new DataTable();
dt.Columns.Add("姓名");
dt.Columns.Add("职位");
dt.Columns.Add("工号");
foreach (string line in data)
{
string[] parts = line.Split(',');
if (parts.Length >= 3)
{
dt.Rows.Add(parts[0], parts[1], parts[2]);
}
}
dataGridView1.DataSource = dt;
}
// 保存数据
void save_data(string[] dataToSave)
{
try
{
File.WriteAllLines(data_path, dataToSave);
MessageBox.Show("文件保存成功!");
}
catch (Exception ex)
{
MessageBox.Show("保存失败:" + ex.Message);
}
}
// 添加数据
void add_data(string name, string adress, int number)
{
try
{
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(adress) && number != null)
{
string line = $"{name},{adress},{number}";
List<string> tempList = new List<string>(data);
tempList.Add(line);
data = tempList.ToArray();
}
}
catch (Exception ex)
{
MessageBox.Show("添加数据时出错: " + ex.Message);
}
}
// 删除数据
private void button_delete_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedCells.Count > 0)
{
int rowIndex = dataGridView1.SelectedCells[0].RowIndex;
string name = dataGridView1.Rows[rowIndex].Cells["姓名"].Value.ToString();
if (MessageBox.Show($"确认删除 {name} 吗?", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
List<string> tempList = new List<string>(data);
string lineToRemove = $"{name},{dataGridView1.Rows[rowIndex].Cells["职位"].Value},{dataGridView1.Rows[rowIndex].Cells["工号"].Value}";
tempList.Remove(lineToRemove);
data = tempList.ToArray();
save_data(data);
RefreshDataGridView();
MessageBox.Show("删除成功!");
}
}
else
{
MessageBox.Show("请选择要删除的行!");
}
}
// 添加按钮事件
private void button_add_Click(object sender, EventArgs e)
{
string name = textBox_name.Text;
string adress = textBox_adress.Text;
if (int.TryParse(textBox_number.Text, out int number))
{
add_data(name, adress, number);
save_data(data);
RefreshDataGridView();
}
else
{
MessageBox.Show("请输入有效的数字!");
}
}
// 显示按钮事件
private void button_flushed_Click(object sender, EventArgs e)
{
RefreshDataGridView();
}
}
}
namespace xcx
{
partial class GM
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button_flushed = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.textBox_name = new System.Windows.Forms.TextBox();
this.textBox_adress = new System.Windows.Forms.TextBox();
this.textBox_number = new System.Windows.Forms.TextBox();
this.button_add = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.button_delete = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(466, 45);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(207, 33);
this.label1.TabIndex = 0;
this.label1.Text = "员工信息管理";
//
// button_flushed
//
this.button_flushed.Location = new System.Drawing.Point(730, 661);
this.button_flushed.Name = "button_flushed";
this.button_flushed.Size = new System.Drawing.Size(184, 57);
this.button_flushed.TabIndex = 2;
this.button_flushed.Text = "刷新";
this.button_flushed.UseVisualStyleBackColor = true;
this.button_flushed.Click += new System.EventHandler(this.button_flushed_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.Location = new System.Drawing.Point(72, 260);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(68, 28);
this.label2.TabIndex = 3;
this.label2.Text = "姓名";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("宋体", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label3.Location = new System.Drawing.Point(72, 333);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(68, 28);
this.label3.TabIndex = 4;
this.label3.Text = "职位";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Font = new System.Drawing.Font("宋体", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label4.Location = new System.Drawing.Point(72, 407);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(68, 28);
this.label4.TabIndex = 5;
this.label4.Text = "工号";
//
// textBox_name
//
this.textBox_name.Location = new System.Drawing.Point(166, 257);
this.textBox_name.Name = "textBox_name";
this.textBox_name.Size = new System.Drawing.Size(350, 28);
this.textBox_name.TabIndex = 6;
//
// textBox_adress
//
this.textBox_adress.Location = new System.Drawing.Point(166, 333);
this.textBox_adress.Name = "textBox_adress";
this.textBox_adress.Size = new System.Drawing.Size(350, 28);
this.textBox_adress.TabIndex = 7;
//
// textBox_number
//
this.textBox_number.Location = new System.Drawing.Point(166, 407);
this.textBox_number.Name = "textBox_number";
this.textBox_number.Size = new System.Drawing.Size(350, 28);
this.textBox_number.TabIndex = 8;
//
// button_add
//
this.button_add.Location = new System.Drawing.Point(199, 533);
this.button_add.Name = "button_add";
this.button_add.Size = new System.Drawing.Size(202, 66);
this.button_add.TabIndex = 9;
this.button_add.Text = "添加";
this.button_add.UseVisualStyleBackColor = true;
this.button_add.Click += new System.EventHandler(this.button_add_Click);
//
// label5
//
this.label5.AutoSize = true;
this.label5.Font = new System.Drawing.Font("宋体", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label5.Location = new System.Drawing.Point(981, 119);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(124, 28);
this.label5.TabIndex = 10;
this.label5.Text = "全部信息";
//
// button_delete
//
this.button_delete.Location = new System.Drawing.Point(1052, 661);
this.button_delete.Name = "button_delete";
this.button_delete.Size = new System.Drawing.Size(161, 57);
this.button_delete.TabIndex = 11;
this.button_delete.Text = "删除";
this.button_delete.UseVisualStyleBackColor = true;
this.button_delete.Click += new System.EventHandler(this.button_delete_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(680, 164);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 62;
this.dataGridView1.RowTemplate.Height = 30;
this.dataGridView1.Size = new System.Drawing.Size(698, 457);
this.dataGridView1.TabIndex = 13;
//
// GM
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1503, 879);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button_delete);
this.Controls.Add(this.label5);
this.Controls.Add(this.button_add);
this.Controls.Add(this.textBox_number);
this.Controls.Add(this.textBox_adress);
this.Controls.Add(this.textBox_name);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.button_flushed);
this.Controls.Add(this.label1);
this.Name = "GM";
this.Text = "GM";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button_flushed;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox_name;
private System.Windows.Forms.TextBox textBox_adress;
private System.Windows.Forms.TextBox textBox_number;
private System.Windows.Forms.Button button_add;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button_delete;
private System.Windows.Forms.DataGridView dataGridView1;
}
}
3.抽奖
依然 data = File.ReadAllLines(data_path);读取txt
用var rand = new Random();抽取
int finalIndex = rand.Next(data.Length);
var finalParts = data[finalIndex].Split(',');
抽取前
// 获取鼠标在窗体客户区的坐标
Point mousePos = this.PointToClient(Control.MousePosition);
//监控鼠标位置,如果在界面的左上角100x100区域内
if (mousePos.X >= 0 && mousePos.X <= 150 &&mousePos.Y >= 0 && mousePos.Y <= 150)
代码如下:
using System;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace xcx
{
public partial class main_system : Form
{
public main_system()
{
InitializeComponent();
LoadData(); // 初始化时加载数据
}
public static string data_path = "D:\\VS\\works\\xcx\\xcx\\data.txt";
public static string[] data = new string[0];
// 加载数据方法
private void LoadData()
{
try
{
data = File.ReadAllLines(data_path);
if (data.Length == 0)
MessageBox.Show("警告:数据文件为空!");
}
catch (Exception ex)
{
MessageBox.Show($"数据加载失败: {ex.Message}");
}
}
private async void button_start_Click(object sender, EventArgs e)
{
if (data.Length == 0)
{
MessageBox.Show("无可用抽奖数据!");
return;
}
textBox1.Clear();
var rand = new Random();
// 抽奖动画效果
for (int i = 0; i < 5; i++)
{
textBox1.AppendText($"正在抽取..."+"\r\n");
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
await Task.Delay(500); // 延迟
}
// 获取鼠标在窗体客户区的坐标
Point mousePos = this.PointToClient(Control.MousePosition);
// 最终结果
//监控鼠标位置,如果在界面的左上角100x100区域内
if (mousePos.X >= 0 && mousePos.X <= 150 &&mousePos.Y >= 0 && mousePos.Y <= 150) {
string result = $"恭喜 二大爷 获奖!";
textBox1.AppendText(result);
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
}
else
{
//正常抽奖
int finalIndex = rand.Next(data.Length);
var finalParts = data[finalIndex].Split(',');
string result = $"恭喜 {finalParts[0]} 获奖!";
textBox1.AppendText(result);
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
}
}
private void button_GM_Click(object sender, EventArgs e)
{
new GM().Show();
}
}
}
namespace xcx
{
partial class GM
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button_flushed = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.textBox_name = new System.Windows.Forms.TextBox();
this.textBox_adress = new System.Windows.Forms.TextBox();
this.textBox_number = new System.Windows.Forms.TextBox();
this.button_add = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.button_delete = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(466, 45);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(207, 33);
this.label1.TabIndex = 0;
this.label1.Text = "员工信息管理";
//
// button_flushed
//
this.button_flushed.Location = new System.Drawing.Point(730, 661);
this.button_flushed.Name = "button_flushed";
this.button_flushed.Size = new System.Drawing.Size(184, 57);
this.button_flushed.TabIndex = 2;
this.button_flushed.Text = "刷新";
this.button_flushed.UseVisualStyleBackColor = true;
this.button_flushed.Click += new System.EventHandler(this.button_flushed_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.Location = new System.Drawing.Point(72, 260);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(68, 28);
this.label2.TabIndex = 3;
this.label2.Text = "姓名";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("宋体", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label3.Location = new System.Drawing.Point(72, 333);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(68, 28);
this.label3.TabIndex = 4;
this.label3.Text = "职位";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Font = new System.Drawing.Font("宋体", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label4.Location = new System.Drawing.Point(72, 407);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(68, 28);
this.label4.TabIndex = 5;
this.label4.Text = "工号";
//
// textBox_name
//
this.textBox_name.Location = new System.Drawing.Point(166, 257);
this.textBox_name.Name = "textBox_name";
this.textBox_name.Size = new System.Drawing.Size(350, 28);
this.textBox_name.TabIndex = 6;
//
// textBox_adress
//
this.textBox_adress.Location = new System.Drawing.Point(166, 333);
this.textBox_adress.Name = "textBox_adress";
this.textBox_adress.Size = new System.Drawing.Size(350, 28);
this.textBox_adress.TabIndex = 7;
//
// textBox_number
//
this.textBox_number.Location = new System.Drawing.Point(166, 407);
this.textBox_number.Name = "textBox_number";
this.textBox_number.Size = new System.Drawing.Size(350, 28);
this.textBox_number.TabIndex = 8;
//
// button_add
//
this.button_add.Location = new System.Drawing.Point(199, 533);
this.button_add.Name = "button_add";
this.button_add.Size = new System.Drawing.Size(202, 66);
this.button_add.TabIndex = 9;
this.button_add.Text = "添加";
this.button_add.UseVisualStyleBackColor = true;
this.button_add.Click += new System.EventHandler(this.button_add_Click);
//
// label5
//
this.label5.AutoSize = true;
this.label5.Font = new System.Drawing.Font("宋体", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label5.Location = new System.Drawing.Point(981, 119);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(124, 28);
this.label5.TabIndex = 10;
this.label5.Text = "全部信息";
//
// button_delete
//
this.button_delete.Location = new System.Drawing.Point(1052, 661);
this.button_delete.Name = "button_delete";
this.button_delete.Size = new System.Drawing.Size(161, 57);
this.button_delete.TabIndex = 11;
this.button_delete.Text = "删除";
this.button_delete.UseVisualStyleBackColor = true;
this.button_delete.Click += new System.EventHandler(this.button_delete_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(680, 164);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 62;
this.dataGridView1.RowTemplate.Height = 30;
this.dataGridView1.Size = new System.Drawing.Size(698, 457);
this.dataGridView1.TabIndex = 13;
//
// GM
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1503, 879);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button_delete);
this.Controls.Add(this.label5);
this.Controls.Add(this.button_add);
this.Controls.Add(this.textBox_number);
this.Controls.Add(this.textBox_adress);
this.Controls.Add(this.textBox_name);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.button_flushed);
this.Controls.Add(this.label1);
this.Name = "GM";
this.Text = "GM";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button_flushed;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox_name;
private System.Windows.Forms.TextBox textBox_adress;
private System.Windows.Forms.TextBox textBox_number;
private System.Windows.Forms.Button button_add;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button_delete;
private System.Windows.Forms.DataGridView dataGridView1;
}
}
4.总结
所以,对网络上的抽奖要明辨是非哦。