重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
先把所有电话名单放在一个List泛型集合中,取出一个就Remove掉一个,这样就不会重复了。
我们是从2013年开始的成都网站建设公司,提供网站建设,电商网站设计开发,外贸营销网站建设,响应式网页设计,成都微信小程序、等服务。为客户创造有价值的品牌营销体验,让互联网提升企业的竞争力!
几等奖几个按照你的需求来循环即可。
给文本框添加内容:TextBox1.Text = 字符串
换行用vbCrLf常量表示。
详细的可以私信
我用多线程写的,你可以参考下。
系统界面图如下:(注:重置功能为清除已经中奖号码,回归1-100都可以中奖状态。)
代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Threading.Thread g_Thread = null;
Listint g_list = new Listint();
int g_Num = 0;
delegate void g_delegate();
/// summary
/// 开始
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void button2_Click(object sender, EventArgs e)
{
if(g_list.Count==100)
{
MessageBox.Show("所有数字已使用,请重置!");
return;
}
button3.Enabled = true;
Random r = new Random();
g_delegate dele= ChangeTxt;
g_Thread = new System.Threading.Thread(() =
{
while(this.IsDisposed==false)
{
g_Num = r.Next(1, 100);
if(g_list.Contains(g_Num))
{
g_Num = 1;
}
while(g_list.Contains(g_Num))
{
g_Num++;
}
this.Invoke(dele);
System.Threading.Thread.Sleep(50);
}
});
g_Thread.Start();
}
void ChangeTxt()
{
label2.Text = g_Num.ToString();
}
/// summary
/// 停止
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void button3_Click(object sender, EventArgs e)
{
//if(g_Thread.ThreadState==System.Threading.ThreadState.Running)
{
g_Thread.Abort();
g_list.Add(Convert.ToInt32(label2.Text));
}
button3.Enabled = false;
}
/// summary
/// 重置
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void button1_Click(object sender, EventArgs e)
{
g_list.Clear();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(0);
}
}
Dim n As Integer
Dim a()
Private Sub Command1_Click()
If n = 0 Then '第一次点击时执行
m = Val(Text1)
ReDim a(1 To m)
For i = 1 To m '生成数组a,元素为1至m这m个数
a(i) = i
Next
For i = 1 To m - 1 '随机打乱数组a中元素
r = Int(Rnd * m + 1)
tmp = a(i)
a(i) = a(r)
a(r) = tmp
Next
End If
n = n + 1 '计数点击次数
If n Val(Text1) Then '次数超过指定次数,抽奖结束
MsgBox "抽奖结束!"
Exit Sub
End If
Text2 = a(n) '从数组中抽出第n个数,由于数组中是随机排列,所以相当于生成一个随机数,并且不会重复
End Sub
主要利用VB中的Rnd函数,来实现随机查找和打乱排序的功能,从而实现随机抽奖的目的。Rnd函数的语法结构是Rnd[(number)],可选的number参数是
single或任何有效的数值表达式。Rnd函数返回小于1但大于或等于0的值。number
的值决定了
Rnd
生成随机数的方式。为了生成某个范围内的随机整数,可使用以下公式:
Int((upperbound
-
lowerbound
+
1)
×
Rnd
+
lowerbound)
这里,upperbound
是随机数范围的上限,而
lowerbound
则是随机数范围的下限。
另外,程序中还使用了INI文件,Windows
INI文件,可解释为Windows初始化文件。它是一种专门用来保存应用程序初始化信息和运行环境信息的文本文件。ini文件是一种文本文件,它可以通过Notepad等文本编辑器进行编辑。ini文件具有特定的格式。一个INI文件是由若干个段(section)组成的,每个段中包含若干关键字(key)及相应的值(value)。创建应用程序自己的INI文件,通过INI文件保存应用程序的一些运行环境信息,然后在程序中读取INI文件中的设置信息并据以处理。一旦程序的运行环境需要变更,则可以通过直接修改INI文件,或在程序中提供专门的界面间接地修改INI文件来保证程序的可用性。
源程序及注释如下:
'窗体源程序
Option
Explicit
Dim
m_strNameArray()
As
MyName
Dim
m_bIsStart
As
Boolean
Dim
m_nNameIndex
As
Integer
Dim
MAX_INDEX
As
Integer
Dim
m_nSelectNum
As
Integer
'被选定数
Dim
nScrollStep
As
Integer
Dim
nScrollWidth
As
Integer
Dim
bScrollState
As
Boolean
Dim
nEnableSecond
As
Integer
Dim
m_strTitle
As
String
Dim
m_strAppTitle
As
String
Dim
m_strScrollTitleLeft
As
String
Dim
m_strScrollTitleRight
As
String
Private
Sub
Command_Start_Stop_Click()
If
m_bIsStart
=
True
Then
'按停止钮
m_bIsStart
=
False
Command_Start_Stop.Caption
=
“开始"
Label_FlashName.Visible
=
True
Timer_FlashName.Enabled
=
True
Timer_ScrollName.Enabled
=
False
Label_FlashName
=
m_strNameArray(m_nNameIndex).strName
+
“中奖了!"
m_strNameArray(m_nNameIndex).bIsSelect
=
True
m_nSelectNum
=
m_nSelectNum
+
1
Dim
Temp
As
MyName
Temp
=m_strNameArray(MAX_INDEX)
m_str
Name
Array(MAX-INDEX)
=
m_strNameArray(m_nNameIndex)
m_strNameArray(m_nNameIndex)
=Temp
MAX_INDEX
=
MAX_INDEX
-
1
If
MAX_INDEX
=
Then
MsgBox
“非常感谢您使用本软件"
End
If
Else
'按开始钮
m_bIsStart
=
True
Command_Start_Stop.Caption
=
“停止"
Command_Start_Stop.Enabled
=
False
Timer_ScrollName.Enabled
=
True
Timer_FlashName.Enabled
=
False
Label_FlashName.Caption
=
“"
End
If
End
Sub
Private
Sub
Form_Load()
Form_Bouns.ScaleMode
=
3
m_nNameIndex
=
m_bIsStart
=
False
Timer_ScrollName.Enabled
=
True
Timer_ScrollTitle.Enabled
=
True
Label_FlashName.Visible
=
False
Label_ScrollName.Caption
=
“"
nEnableSecond
=
'定义起始秒数
ReDimNameArray
'获得文本中的名字和打乱名字顺序
nScrollStep
=
5
'设定滚动字的步长
nScrollWidth
=
Label_Congruation.Left
'设定title的移动宽度
bScrollState
=
False
'设定缺省的开始滚动方向为向左
m_nSelectNum
=
'初始化被选定数为0
Init
'初始化本程序的界面
End
Sub
Private
Sub
Timer_FlashName_Timer()
'闪动中奖者姓名
If
Label_FlashName.Visible
=
True
Then
Label_FlashName.Visible
=
False
Else
Label_FlashName.Visible
=
True
End
If
End
Sub
Private
Sub
Timer_ScrollName_Timer()
'滚动出现名字
If
m_bIsStart
=
True
Then
If
m_nNameIndex
=
MAX_INDEX
Then
m_nNameIndex
=
End
If
m_nNameIndex
=m_nNameIndex
+
1
If
m_strNameArray(m_nNameIndex).bIsSelect
=
True
Then
If
m_nNameIndex
MAX-INDEX
Then
m_nNameIndex
=
m_nNameIndex
+
1
Else
m_nNameIndex
=
End
If
End
If
Label_ScrollName.Caption
=
m_str
NameArray(m_nNameIndex).strName
'End
If
End
If
End
Sub
Private
Sub
Timer_ScrollTitle_Timer()
'滚动“恭喜发财"字样
If
bScrollState
=
False
Then
'向左滚
nScrollStep
=
10
Label_Congruation.Caption
=
m_strScrollTitleLeft
If
nScrollWidth
Then
nScrollWidth
=
nScrollWidth
-
nScrollStep
Else
bScrollState
=
True
End
If
Else
'向右滚
nScrollStep
=
-10
Label_Congruation.Caption
=
m_strScrollTitleRight
If
nScrollWidth
Form_Bouns.ScaleWidth
-
Label_Congruation.Width
Then
nScrollWidth
=
nScrollWidth
-
nScrollStep
Else
bScrollState
=
False
End
If
End
If
Label_Congruation.Left
=
nScrollWidth
'以下为8秒钟内使“停止"按钮有效
If
nEnableSecond
=
49
Then
If
m_bIsStart
=
True
Then
nEnableSecond
=nEnableSecond
+
1
End
If
Else
If
m_bIsStart
=
True
Then
Command_Start_Stop.Enabled
=
True
nEnableSecond
=
End
If
End
If
End
Sub
'动态定义数组
Private
Sub
ReDimNameArray()
Dim
nMaxIndex
As
Integer
Dim
strMaxIndex
As
String
Dim
nIndex
As
Integer
Dim
bIsBegin
As
Boolean
bIsBegin
=
False
nIndex
=
Open
App.Path
+
“\name.txt"
For
Input
As
#1
'读文件
Do
Until
EOF(1)
If
bIsBegin
=
False
Then
Line
Input
#1,
strMaxIndex
nMaxIndex
=
Val(strMaxIndex)
MAX_INDEX
=
nMaxIndex
-
1
ReDim
m_strNameArray(0
To
nMaxIndex
-
1)
bIsBegin
=
True
Else
Line
Input
#1,
m_strNameArray(nIndex).strName
m_strNameArray(nIndex).bIsSelect
=
False
nIndex
=
nIndex
+
1
End
If
Loop
'以下为打乱人员顺序10次
Dim
i
As
Integer
Dim
j
As
Integer
Dim
Temp
As
String
Dim
nRandomNum
As
Integer
For
j
=
To
10
For
i
=
To
nMaxIndex
-
1
nRandomNum
=
((nMaxIndex
-
1)
×
Rnd)
'利用Rnd函数
Temp
=
m_strNameArray(i).strName
m_strNameArray(i).strName
=
m_strNameArray(nRandomNum).strName
m_strNameArray(nRandomNum).strName
=
Temp
Next
i
Next
j
End
Sub
Private
Sub
Init()
'读取INI文件
Dim
X
As
Long
Dim
lpFileName
Dim
Temp
As
String
×
50
lpFileName
=
App.Path
+
“\Sortition.ini"
X
=
GetPrivateProfileString(“SYSTEM",“AppTitle",“抽奖程序",
Temp,
Len(Temp),
lpFileName)
m_strAppTitle
=
Trim(Temp)
Temp
=“"
X
=
GetPrivateProfileString(“SYSTEM",
"Title",
"欢迎使用抽奖程序",
Temp,
Len(Temp),
lpFileName)
m_strTitle
=
Trim(Temp)
Temp
=
“"
X
=
GetPrivateProfileString(“SYSTEM",“ScrollTitleRight",
“恭喜发财!!!",
Temp,
Len(Temp),
lpFileName)
m_strScrollTitleRight
=
Trim(Temp)
X
=
GetPrivateProfileString(“SYSTEM",“ScrollTitleLeft",
“龙年大发!!!",
Temp,
Len(Temp),
lpFileName)
m_strScrollTitleLeft
=
Trim(Temp)
Form_Bouns.Caption
=
m_strAppTitle
Label_CompanyTitle.Caption
=
m_strTitle
End
Sub
模块源程序:
'用于读取ini文件的API函数
Declare
Function
GetPrivateProfileString
Lib
“kernel32"
Alias
“GetPrivateProfileStringA"
(ByVal
lpApplicationname
As
String,
ByVal
lpKeyName
As
String,
ByVal
lpDefault
As
String,
ByVal
lpReturnedString
As
String,
ByVal
nSize
As
Long,
ByVal
lpFileName
As
String)
As
Long
Public
Type
MyName
strName
As
String
bIsSelect
As
Boolean
End
Type
由于程序利用的windows
ini文件保存一些标题信息,因而可以方便的修改使用环境,及标题内容。
见ini文件内容:
[SYSTEM]
;应用程序的form名称
AppTitle=“风云电脑抽奖Test"
;窗口的内的标题(限9个字)
Title=“大抽奖"
;右滚动的文字(仅能为如下格式:XXXX!!!)
ScrollTitleRight=“恭喜发财!!!"
;左滚动的文字(仅能为如下格式:XXXX!!!)
ScrollTitleLeft=“祝您好运!!!"