窗体功能有每次选择人数的选择及重置等,具体功能见下
public partial class Form1 : Form
{
public int N1 = 0;//一次抽取数量
public int N2 = 0;//总共抽取数量
public int N3 = 0;//当前抽取数
public int N4 = 0;//总数
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
N1 = int.Parse(number1.Value.ToString());
N2 = int.Parse(number2.Value.ToString());
if (N2 < N1)
{
MessageBox.Show("抽取总数不得小于每次抽取数!");
}
else
{
lbl5需求.Text = N1.ToString();
lbl6输出.Text = N2.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
listBox2结果.Items.Clear();
lbl5需求.Text = "";
lbl6输出.Text = "";
N1 = 0;
N2 = 0;
N3 = 0;
}
private void button3_Click(object sender, EventArgs e)
{
N1 = 0;
N2 = 0;
N3 = 0;
N4 = 0;
listBox2结果.Items.Clear();
listBox1名单.Items.Clear();
lbl5需求.Text = "";
lbl6输出.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
if (N4 < 1)
MessageBox.Show("请确认抽签名单!");
else if (N1 < 1)
MessageBox.Show("请确认抽签数量!");
else if (N2 < 1)
MessageBox.Show("请确认抽签总数!");
else if (N3 >= N2)
MessageBox.Show("已经抽完了!");
else
{
if (N2 > N4)
{
MessageBox.Show("输出总数不得大于参与人数!");
}
else
{
int i;
Random ran = new Random();
int RandKey;
for (i = 1; i <= N1 & N3 < N2;)
{
RandKey = ran.Next(0, N4);
if (!listBox2结果.Items.Contains(listBox1名单.Items[RandKey]))
{
listBox2结果.Items.Add(listBox1名单.Items[RandKey]);
i++;
N3 ++;
}
}
}
}
}
private void button5_Click(object sender, EventArgs e)
{
int index = listBox1名单.Items.Count;
Read1(listBox1名单);
N4 = listBox1名单.Items.Count;
listBox1名单.SelectedIndex = listBox1名单.Items.Count - 1;
lbl7总数.Text = N4.ToString();
}
private void Read1(System.Windows.Forms.ListBox lst)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Excel 文件(*.xlsx; *.xls)|*.xlsx; *.xls";
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog1.FileName;
ReadExcelData(filePath);
}
}
private void ReadExcelData(string filePath)
{
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = excelApp.Workbooks.Open(filePath);
Excel.Worksheet worksheet = workbook.ActiveSheet;
int rowCount = worksheet.UsedRange.Rows.Count;
int columnCount = worksheet.UsedRange.Columns.Count;
for (int i = 1; i <= rowCount; i++)
{
string item = "";
for (int j = 1; j <= columnCount; j++)
{
item += worksheet.Cells[i, j].Value.ToString() + " ";
}
listBox1名单.Items.Add(item.Trim());
}
// 关闭 Excel
workbook.Close();
excelApp.Quit();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
MessageBox.Show("本程序仅可在excel关闭时所在的sheet!表,暂未研究出怎样选择sheet表", "使用说明");
}
}
标签:MessageBox,C#,Excel,else,int,窗体,N1,N2,public
From: https://blog.csdn.net/qq_42858292/article/details/143372730