最近學C#和WINFORM,想著通過實例學習更能激發學習興趣,於是弄了這麼一個小程序,功能就是在自建的抽籤名單中隨機抽籤。
直接貼代碼。
有三個主要代碼文件:
1、program.cs 這個代碼功能就是設置了從第一個窗體啟動程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace sjs
{
static class Program
{
/// <summary>
/// 應用程式的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new reSetBtn());
}
}
}
2、setIn.cs,主窗體的代碼,功能是錄入,查詢,刪除,重置,顯示姓名,然後也有進入隨機抽籤的入口。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//**************************
// 隨機抽籤小軟體0.1
// 作者:匡星星
//**************************
namespace sjs
{
public partial class reSetBtn : Form
{
public reSetBtn()
{
InitializeComponent();
}
//定義變量
List<string> NameList = new List<string> {"張三","李四","王五","趙六"}; //創建一個存儲名單的空列表列表
bool isShow = false ; //判斷是否顯示列表名單,默認值是不顯示
//錄入按鈕
public void InSetBtn_Click(object sender, EventArgs e)
{
string strName = inSetText.Text;
//遍歷數組,查詢數組中是否有相同的名字
if (NameList.Count == 0) //當列表中為空的時候,直接將第一個名字添加進去
{
NameList.Add(strName); //新的姓名,存儲到列表中
MessageBox.Show("姓名添加成功!", "添加成功!");
}
else
{
foreach (string strTemp in NameList) //NameList.Count 獲取列表的元素的個數
{
if (strName == strTemp)
{
MessageBox.Show("系統中已經有相同的名字,請重新輸入!!", "姓名重複"); //姓名重複,要求重新輸入
break;
}
else
{
NameList.Add(strName); //新的姓名,存儲到列表中
MessageBox.Show("姓名添加成功!", "添加成功!");
break;
}
break;
}
}
}
//重置按鈕,清空列表並清空列出文本框內容
private void Button1_Click(object sender, EventArgs e)
{
NameList.Clear(); //清空列表
showText.Clear(); //清空列出文本框內容
MessageBox.Show("抽籤名單已清空!", "提示");
}
//列出所有名單按鈕
private void ShowListBtn_Click(object sender, EventArgs e)
{
isShow = true; //判斷是否顯示名單
string outList;
showText.Clear(); //清空當前顯示的左側名單
if (isShow == true)
{
if(NameList.Count == 0)
{
MessageBox.Show("名單為空!", "提示");
}
else
{
foreach (string temp in NameList)
{
outList = temp + " ";
showText.AppendText(outList + "\r\n");
}
isShow = false; //重置列出名單按鈕
}
}
}
//刪除某個特定的姓名
private void DelName_Click(object sender, EventArgs e)
{
string delTheName = inSetText.Text;
for(int i = NameList.Count; i >= 0; i-- )
{
if( NameList.Contains(delTheName))
{
string message;
NameList.Remove(delTheName);
message = "姓名「" + delTheName + "」刪除成功!";
MessageBox.Show(message, "提示");
break;
}
else
{
MessageBox.Show("名單中不存在這個姓名!", "提示");
break;
}
}
}
//跳轉到主窗體
private void DrawBtn_Click(object sender, EventArgs e)
{
this.Hide();
signWin newSignWin = new signWin();
newSignWin.Show();
newSignWin.mainNameList = this.NameList; //在窗口跳轉時,將列表對象的值傳輸到新窗體的列表中
}
}
}
3、signWin.cs 這是第二個窗體的代碼,功能就是隨機抽籤。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//**************************
// 隨機抽籤小軟體0.1
// 作者:匡星星
//**************************
namespace sjs
{
public partial class signWin : Form
{
public signWin()
{
InitializeComponent();
}
bool boolVal = false; //定義抽籤的標籤
//跳轉到數據錄入窗體
private void Button1_Click(object sender, EventArgs e)
{
this.Hide();
reSetBtn inset = new reSetBtn();
inset.Show();
}
public List<string> mainNameList = new List<string>();
//抽籤按鈕
private void SignStart_Click(object sender, EventArgs e)
{
int i = 0;
if (boolVal == false)
{
Random rd = new Random();
i = rd.Next(0, mainNameList.Count()); //產生隨機數
showNameBox.Text = mainNameList[i]; //將指定的列表中的名字傳給文本框
boolVal = true; //更新標籤,避免連續點擊,造成重複抽籤
}
}
private void SignStop_Click(object sender, EventArgs e)
{
showNameBox.Clear(); //清空文本框裡的名字
boolVal = false; //更新標籤,使抽籤可用
//showNameBox.Text = "姓名";
}
}
}
代碼到此為止。新手上路,肯定有不足之處,希望大神們多多指點!最後奉上軟體截圖和軟體的網盤地址。
網盤地址:https://pan.baidu.com/s/1fB5WnvsJu7oXz2_sCsAc5g
提取碼:ohcc