 |
|
สอบถามเรื่องการ Random ตัวเลขเมื่อเรากดปุ่มหยุดแล้วให้ Random ช้าลงเรื่อยๆ แล้วก็หยุดไป |
|
 |
|
|
 |
 |
|

Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsRandom
{
public partial class Form1 : Form
{
private int _duration;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
buttonStart.Enabled = true;
buttonStop.Enabled = false;
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox1.BackColor = System.Drawing.Color.White;
textBox2.BackColor = System.Drawing.Color.White;
textBox3.BackColor = System.Drawing.Color.White;
textBox4.BackColor = System.Drawing.Color.White;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while(_duration > 0)
{
Random random = new Random();
int[] r = new int[] { random.Next(0, 9), random.Next(0, 9), random.Next(0, 9), random.Next(0, 9) };
backgroundWorker1.ReportProgress(0, r);
System.Threading.Thread.Sleep((1000 - _duration == 0) ? 100 : 1000 - _duration);
if (_duration < 1000)
{
_duration -= 200;
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int[] r = (int[])e.UserState;
textBox1.Text = r[0].ToString();
textBox2.Text = r[1].ToString();
textBox3.Text = r[2].ToString();
textBox4.Text = r[3].ToString();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
buttonStart.Enabled = true;
buttonStop.Enabled = false;
textBox1.BackColor = System.Drawing.Color.White;
textBox2.BackColor = System.Drawing.Color.White;
textBox3.BackColor = System.Drawing.Color.White;
textBox4.BackColor = System.Drawing.Color.White;
}
private void buttonStart_Click(object sender, EventArgs e)
{
_duration = 1000;
buttonStart.Enabled = false;
buttonStop.Enabled = true;
textBox1.BackColor = System.Drawing.Color.LightGray;
textBox2.BackColor = System.Drawing.Color.LightGray;
textBox3.BackColor = System.Drawing.Color.LightGray;
textBox4.BackColor = System.Drawing.Color.LightGray;
backgroundWorker1.RunWorkerAsync();
}
private void buttonStop_Click(object sender, EventArgs e)
{
_duration = 800;
buttonStart.Enabled = false;
buttonStop.Enabled = false;
}
}
}
|
 |
 |
 |
 |
Date :
2012-12-13 14:07:05 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2012-12-13 14:08:48 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ คุณ Gust Code ของ VB มีหรือเปล่าครับ รบกวนด้วยครับ
|
 |
 |
 |
 |
Date :
2012-12-13 14:22:20 |
By :
s_oil_p |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับคุณ Gust ตอนนี้ทำได้แล้วครับ
|
 |
 |
 |
 |
Date :
2012-12-14 08:38:38 |
By :
s_oil_p |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|