77范文网 - 专业文章范例文档资料分享平台

C#winform练习(8)

来源:网络收集 时间:2018-12-20 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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 { } }

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库C#winform练习(8)在线全文阅读。

C#winform练习(8).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/378537.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: