sp.Play(); }
///
///
private void button3_Click(object sender, EventArgs e) {
int index = listBox1.SelectedIndex; index++;
if(index==listBox1.Items.Count) {
index = 0; }
//将改变后的索引重新的赋值给我当前选中项的索引 listBox1.SelectedIndex = index; sp.SoundLocation = list[index]; sp.Play();
}
private void button2_Click(object sender, EventArgs e) {
int index = listBox1.SelectedIndex; index--; if(index<0) {
index = listBox1.Items.Count - 1; }
//将重新改变后的索引重新的赋值给当前的选中项 listBox1.SelectedIndex = index; sp.SoundLocation = list[index]; sp.Play(); } } }
//22.摇奖机
在窗体中拖入3个label控件和1个botton控件并分别设置其属性 botton控件
Text:开始
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms;
namespace 摇奖机 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
bool b = false; ///
///
private void button1_Click(object sender, EventArgs e) {
if(b==false)//判断程序加载时bool变量的值当为false时执行下面的程序 {
b = true;
button1.Text = \暂停\;
Thread th = new Thread(PlayGame); th.IsBackground = true; th.Start(); } else {
b = false;
button1.Text = \开始\; }
//PlayGame(); }
///
///
private void PlayGame() {
//生成一个随机数
Random r = new Random(); //将随机数加载给lable控件文本 while(b) {
label1.Text = r.Next(0, 10).ToString(); label2.Text = r.Next(0, 10).ToString(); label3.Text = r.Next(0, 10).ToString(); } }
///
/// 程序加载时不检查跨线程访问 ///
///
private void Form1_Load(object sender, EventArgs e) {
Control.CheckForIllegalCrossThreadCalls = false; } } }
//23.模拟服务器与客户端之间的通信 服务器端
在窗中拖入6个botton和5个textBox以及1个comboBox控件并分别设置其属性 textBox1
Name:
textBox2
Name:txtPort Name:btnStart Name:txtLog Name:txtMsg Name:txtPath Name:btnSelect
Text:选择 Text:开始监听
botton1 textBox3 textBox4 textBox5 botton2 botton3 botton4 botton5
Name:btnSendFile Text:发送文件 Name:btnSend Name:btnZD
Text:发送消息 Text:震动
comboBox Name:cboUsers
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; using System.Net.Sockets; using System.Net; using System.Threading; using System.IO;
namespace 服务器与客户端的通信监听 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
///
/// 程序加载时不检查线程复用 ///
///
private void Form1_Load(object sender, EventArgs e) {
Control.CheckForIllegalCrossThreadCalls = false; }
///
///
private void btnStart_Click(object sender, EventArgs e) { try {
//当点击开始监听的时候,在服务器端创建一个负责监听IP地址跟端口号的socket Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Any; //创建端口号对象
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text)); //监听
socketWatch.Bind(point); ShowMsg(\监听成功\); socketWatch.Listen(10);
Thread th = new Thread(Listen); th.IsBackground = true; th.Start(socketWatch); } catch { } }
///
/// 等待客户端的连接并且创建与之通信用的Socket /// Socket socketSend;
void Listen(object o) {
Socket socketWatch = o as Socket;
//等待客户端的连接 并且创建一个负责通信的Socket while (true) { try {
socketSend = socketWatch.Accept();
//将远程连接的客户端的IP地址和socket存入集合中
dicSocket.Add(socketSend.RemoteEndPoint.ToString(), socketSend);
//将远程连接的客户端的IP地址和端口号存储下拉框中
cboUsers.Items.Add(socketSend.RemoteEndPoint.ToString()); ShowMsg(socketSend.RemoteEndPoint.ToString() + \ + \连接成功\);
//开启一个新线程不停的接收客户端发来的消息 Thread th = new Thread(Recive); th.IsBackground = true; th.Start(socketSend); } catch { } }