激活窗口和取消激活窗口

2008.06.23 / 标签: , / 分类: WinForm
Sofa

激活窗口和取消激活窗口
在MDI中打开多个文档时,知道哪个子窗口响应“键按下”等操作很重要。假定有两个打开的子窗口,各容纳一个文档,再按下A键时,只在一个MDI字窗口中显示字符A,而不是在两个窗口都显示。这里的问题是:“哪个文档接受按键操作?”答案是:“活动窗口”中的文档。有多个窗口打开时,在同一时间只有一个窗口是“活动”的。应用程序的活动窗口是响应所有操作的窗口,通常最上面的窗口是活动窗口。另外,活动窗口的标题栏颜色一般不同于非活动的窗口。
阅读全文>>

Winform主窗体和子窗体

2008.06.22 / 标签: ,, / 分类: WinForm
Sofa

Winform主窗体和子窗体
MDI应用程序由一个MDI父窗体和一个或多个子窗体构成。MDI父窗体是MDI应用程序的基础,它包含多个MDI子窗体,子窗口是用户与MDI应用程序进行交互的副窗口。
MDI父窗体有几个显著特点:
阅读全文>>

单文档和多文档应用程序简介

2008.06.22 / 标签: / 分类: WinForm
Sofa

“记事本”和“Internet Explorer”是两个常用的Windows应用程序。对于这两个应用程序,任何时候应用程序实例内部都只有一个文档打开。也就是说,当“记事本”或“Internet Explorer”中已有一个文件打开时,若要再打开另外一个新文件,就必须先关闭第一个文件才能打开新的文件。如果要同时查看两个文件,就必须要应用另外一个应用程序实例。这种应用程序的一个实例不允许用户同时打开多个文件或窗口,因此被成为SDI(Single Document Interface,单文档界面)应用程序。
阅读全文>>

SqlConnection,SqlDataAdapter数据库操作实例

2008.06.21 / 标签: , / 分类: WinForm
Sofa

程序设计要求:要能够查询mssql2000数据库中pubs库的jobs表中的数据显示出来,并可对数据库进行修改、删除等操作。 程序源码:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9.  
  10. namespace DataTest
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         SqlConnection conn;//数据库连接对象
  20.         SqlDataAdapter adpter;//适配器对象
  21.         DataSet ds;//数据集对象
  22.  
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             conn = new SqlConnection("server=.;uid=sa;pwd=;database=pubs");
  26.             adpter = new SqlDataAdapter("select * from jobs",conn);
  27.             ds = new DataSet();
  28.             adpter.Fill(ds,"jobs");
  29.             this.dataGridView1.DataSource = ds.Tables[0];
  30.         }
  31.  
  32.         private void button3_Click(object sender, EventArgs e)
  33.         {
  34.             ds.Tables[0].Rows[this.dataGridView1.CurrentRow.Index].Delete();//删除鼠标选定的数据
  35.             SqlCommandBuilder scb = new SqlCommandBuilder(adpter);
  36.             adpter.Update(ds,"jobs");//更新数据库
  37.         }
  38.  
  39.         private void button2_Click(object sender, EventArgs e)
  40.         {
  41.             //保存数据
  42.             SqlCommandBuilder scb = new SqlCommandBuilder(adpter);
  43.             adpter.Update(ds, "jobs");
  44.         }
  45.  
  46.     }
  47. }

 

DataTable的属性、方法和事件

2008.06.19 / 标签: / 分类: WinForm
Sofa

数据集中的每个DataTable对象都标示一个数据库检索到的表,每个表的列和约束用于定义DataTable得结构。第一次创建DataTable时,其中不含该结构,通过创建DataColumn对象并将其添加至表的Columns集合中来定义该表的结构。
DataTable的属性、方法和事件
阅读全文>>

C#数据集

2008.06.19 / 标签: / 分类: WinForm
Sofa

数据集
数据集是一个用于存储从数据库检索到的数据的对象,数据集可以简单的理解成为一个临时数据库,他是断开式、存储在内存中的。可以从任何有效数据源(如SQL Server、Oracle、文本文件或XML文件)将数据加载到数据集中。数据集是由数据行和列、约束和有关表对象中数据关系的信息组成的零个或多个表对象的集合。这些数据缓存在本地机上,不需要与数据库持续连接。
阅读全文>>

ado.net实现对数据库的添加、删除和查看操作

2008.06.17 / 标签: / 分类: WinForm
Sofa

ado.net实现对数据库的添加、删除和查看操作。提示:添加删除都不是查询所以我们可以使用SqlCommand 的ExecuteNonQuery完成。 程序源码:

 
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8.  
  9. namespace Infomation
  10. {
  11.     /// <summary>
  12.     /// Form1 的摘要说明。
  13.     /// </summary>
  14.     public class Form1 : System.Windows.Forms.Form
  15.     {
  16.         private System.Windows.Forms.TextBox txtFlightNum;
  17.         private System.Windows.Forms.TextBox txtName;
  18.         private System.Windows.Forms.TextBox txtCertificate;
  19.         private System.Windows.Forms.TextBox txtSeatId;
  20.         private System.Windows.Forms.TextBox txtAge;
  21.         private System.Windows.Forms.Button btnAdd;
  22.         private System.Windows.Forms.Button btnDelete;
  23.         private System.Windows.Forms.Button btnCancel;
  24.         private System.Windows.Forms.ComboBox cboSex;
  25.         private System.Windows.Forms.Label lblFlightNum;
  26.         private System.Windows.Forms.Label lblName;
  27.         private System.Windows.Forms.Label lblSex;
  28.         private System.Windows.Forms.Label lblCertificate;
  29.         private System.Windows.Forms.Label lblSeatId;
  30.         private System.Windows.Forms.Label lblAge;
  31.         /// <summary>
  32.         /// 必需的设计器变量。
  33.         /// </summary>
  34.         private System.ComponentModel.Container components = null;
  35.  
  36.         public Form1()
  37.         {
  38.             //
  39.             // Windows 窗体设计器支持所必需的
  40.             //
  41.             InitializeComponent();
  42.  
  43.             //
  44.             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  45.             //
  46.         }
  47.  
  48.         /// <summary>
  49.         /// 清理所有正在使用的资源。
  50.         /// </summary>
  51.         protected override void Dispose( bool disposing )
  52.         {
  53.             if( disposing )
  54.             {
  55.                 if (components != null
  56.                 {
  57.   &n
    bsp;                 components.Dispose();
  58.                 }
  59.             }
  60.             base.Dispose( disposing );
  61.         }
  62.  
  63.         #region Windows 窗体设计器生成的代码
  64.         /// <summary>
  65.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  66.         /// 此方法的内容。
  67.         /// </summary>
  68.         private void InitializeComponent()
  69.         {
  70.             this.lblFlightNum = new System.Windows.Forms.Label();
  71.             this.lblName = new System.Windows.Forms.Label();
  72.             this.lblSex = new System.Windows.Forms.Label();
  73.             this.lblCertificate = new System.Windows.Forms.Label();
  74.             this.lblSeatId = new System.Windows.Forms.Label();
  75.             this.lblAge = new System.Windows.Forms.Label();
  76.             this.txtFlightNum = new System.Windows.Forms.TextBox();
  77.             this.txtName = new System.Windows.Forms.TextBox();
  78.             this.txtCertificate = new System.Windows.Forms.TextBox();
  79.             this.txtSeatId = new System.Windows.Forms.TextBox();
  80.             this.txtAge = new System.Windows.Forms.TextBox();
  81.             this.btnAdd = new System.Windows.Forms.Button();
  82.             this.btnDelete = new System.Windows.Forms.Button();
  83.             this.btnCancel = new System.Windows.Forms.Button();
  84.             this.cboSex = new System.Windows.Forms.ComboBox();
  85.             this.SuspendLayout();
  86.             // 
  87.             // lblFlightNum
  88.             // 
  89.             this.lblFlightNum.Location = new System.Drawing.Point(24, 24);
  90.             this.lblFlightNum.Name = "lblFlightNum";
  91.             this.lblFlightNum.Size = new System.Drawing.Size(48, 23);
  92.             this.lblFlightNum.TabIndex = 0;
  93.             this.lblFlightNum.Text = "航班号:";
  94.             // 
  95.             // lblName
  96.             // 
  97.             this.lblName.Location = new System.Drawing.Point(24, 80);
  98.             this.lblName.Name&
    nbsp;= 
    "lblName";
  99.             this.lblName.Size = new System.Drawing.Size(40, 23);
  100.             this.lblName.TabIndex = 1;
  101.             this.lblName.Text = "姓名:";
  102.             // 
  103.             // lblSex
  104.             // 
  105.             this.lblSex.Location = new System.Drawing.Point(24, 136);
  106.             this.lblSex.Name = "lblSex";
  107.             this.lblSex.Size = new System.Drawing.Size(40, 23);
  108.             this.lblSex.TabIndex = 2;
  109.             this.lblSex.Text = "性别:";
  110.             // 
  111.             // lblCertificate
  112.             // 
  113.             this.lblCertificate.Location = new System.Drawing.Point(256, 24);
  114.             this.lblCertificate.Name = "lblCertificate";
  115.             this.lblCertificate.Size = new System.Drawing.Size(48, 23);
  116.             this.lblCertificate.TabIndex = 3;
  117.             this.lblCertificate.Text = "证件号:";
  118.             // 
  119.             // lblSeatId
  120.             // 
  121.             this.lblSeatId.Location = new System.Drawing.Point(256, 80);
  122.             this.lblSeatId.Name = "lblSeatId";
  123.             this.lblSeatId.Size = new System.Drawing.Size(48, 23);
  124.             this.lblSeatId.TabIndex = 4;
  125.             this.lblSeatId.Text = "座位号:";
  126.             // 
  127.             // lblAge
  128.             // 
  129.             this.lblAge.Location = new System.Drawing.Point(256, 136);
  130.             this.lblAge.Name = "lblAge";
  131.             this.lblAge.Size = new System.Drawing.Size(40, 23);
  132.             this.lblAge.TabIndex = 5;
  133.             this.lblAge.Text = "年龄:";
  134.             // 
  135.             // txtFlightNum
  136.             // 
  137.             this.txtFlightNum.Location = new System.Drawing.Point(88, 24);
  138.             this.txtFlightNum.Name =  pan class="string">"txtFlightNum";
  139.             this.txtFlightNum.TabIndex = 6;
  140.             this.txtFlightNum.Text = "";
  141.             // 
  142.             // txtName
  143.             // 
  144.             this.txtName.Location = new System.Drawing.Point(88, 80);
  145.             this.txtName.Name = "txtName";
  146.             this.txtName.Size = new System.Drawing.Size(136, 21);
  147.             this.txtName.TabIndex = 7;
  148.             this.txtName.Text = "";
  149.             // 
  150.             // txtCertificate
  151.             // 
  152.             this.txtCertificate.Location = new System.Drawing.Point(320, 24);
  153.             this.txtCertificate.Name = "txtCertificate";
  154.             this.txtCertificate.Size = new System.Drawing.Size(168, 21);
  155.             this.txtCertificate.TabIndex = 8;
  156.             this.txtCertificate.Text = "";
  157.             // 
  158.             // txtSeatId
  159.             // 
  160.             this.txtSeatId.Location = new System.Drawing.Point(320, 80);
  161.             this.txtSeatId.Name = "txtSeatId";
  162.             this.txtSeatId.Size = new System.Drawing.Size(168, 21);
  163.             this.txtSeatId.TabIndex = 9;
  164.             this.txtSeatId.Text = "";
  165.             // 
  166.             // txtAge
  167.             // 
  168.             this.txtAge.Location = new System.Drawing.Point(320, 136);
  169.             this.txtAge.Name = "txtAge";
  170.             this.txtAge.TabIndex = 10;
  171.             this.txtAge.Text = "";
  172.             // 
  173.             // btnAdd
  174.             // 
  175.             this.btnAdd.Location = new System.Drawing.Point(192, 216);
  176.             this.btnAdd.Name = "btnAdd";
  177.             this.btnAdd.TabIndex = 11;
  178.             this.btnAdd.Text = "添加";
  179.           &nbsp
    this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
  180.             // 
  181.             // btnDelete
  182.             // 
  183.             this.btnDelete.Location = new System.Drawing.Point(296, 216);
  184.             this.btnDelete.Name = "btnDelete";
  185.             this.btnDelete.TabIndex = 12;
  186.             this.btnDelete.Text = "删除";
  187.             this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
  188.             // 
  189.             // btnCancel
  190.             // 
  191.             this.btnCancel.Location = new System.Drawing.Point(400, 216);
  192.             this.btnCancel.Name = "btnCancel";
  193.             this.btnCancel.TabIndex = 13;
  194.             this.btnCancel.Text = "取消";
  195.             this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
  196.             // 
  197.             // cboSex
  198.             // 
  199.             this.cboSex.Items.AddRange(new object[] {
  200.                                                         "男",
  201.                                                         "女"});
  202.             this.cboSex.Location = new System.Drawing.Point(88, 136);
  203.             this.cboSex.Name = "cboSex";
  204.             this.cboSex.Size = new System.Drawing.Size(121, 20);
  205.             this.cboSex.TabIndex = 14;
  206.             // 
  207.             // Form1
  208.             // 
  209.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  210.             this.ClientSize = new System.Drawing.Size(512, 273);
  211.             this.Controls.Add(this.cboSex);
  212.             this.Controls.Add(this.btnCancel);
  213.             this.Controls.Add(this.btnDelete);
  214.             this.Controls.Add(this.btnAdd);
  215.             this.Controls.Add(this.txtAge);
  216. >            this.Controls.Add(this.txtSeatId);
  217.             this.Controls.Add(this.txtCertificate);
  218.             this.Controls.Add(this.txtName);
  219.             this.Controls.Add(this.txtFlightNum);
  220.             this.Controls.Add(this.lblAge);
  221.             this.Controls.Add(this.lblSeatId);
  222.             this.Controls.Add(this.lblCertificate);
  223.             this.Controls.Add(this.lblSex);
  224.             this.Controls.Add(this.lblName);
  225.             this.Controls.Add(this.lblFlightNum);
  226.             this.Name = "Form1";
  227.             this.Text = "乘客详细信息";
  228.             this.Load += new System.EventHandler(this.Form1_Load);
  229.             this.ResumeLayout(false);
  230.  
  231.         }
  232.         #endregion
  233.  
  234.         /// <summary>
  235.         /// 应用程序的主入口点。
  236.         /// </summary>
  237.         [STAThread]
  238.         static void Main() 
  239.         {
  240.             Application.Run(new Form1());
  241.         }
  242.         
  243.         //创建数据库连接对象及命令对象
  244.         private SqlConnection conn;
  245.         private SqlCommand cmd;
  246.  
  247.         private void btnAdd_Click(object sender, System.EventArgs e)
  248.         {
  249.             
  250.             string sql = "insert Flight values(‘"+this.txtFlightNum.Text+"’,'"+this.txtName.Text+"’,'"+this.cboSex.SelectedItem.ToString()+"’,'"+this.txtCertificate.Text+"’,'"+this.txtSeatId.Text+"’,"+int.Parse(this.txtAge.Text)+")";
  251.             try
  252.             {
  253.                 conn.Open();//打开数据库连接
  254.                 cmd = new SqlCommand(sql,conn);
  255.                 cmd.ExecuteNonQuery();//执行插入命令
  256.                 MessageBox.Show("数据添加成功");//提示执行结果
  257.             }
  258.             catch(SqlException ex)//捕获异常
  259.             {
  260.        &nbsp
    ;        MessageBox.Show(ex.Message);
  261.             }
  262.             finally
  263.             {
  264.                 conn.Close();//关闭连接
  265.             }
  266.         }
  267.  
  268.         private void Form1_Load(object sender, System.EventArgs e)
  269.         {
  270.             //程序载入时创建数据库连接
  271.             conn = new SqlConnection("server = .;uid = sa;pwd = ;database = pubs");
  272.         }
  273.  
  274.         private void btnDelete_Click(object sender, System.EventArgs e)
  275.         {
  276.             string sql = "delete from Flight where FlightNum =’"+this.txtFlightNum.Text+"’";
  277.             try
  278.             {
  279.                 conn.Open();//打开连接
  280.                 cmd = new SqlCommand(sql,conn);
  281.                 cmd.ExecuteNonQuery();//执行删除数据操作
  282.                 MessageBox.Show("数据删除成功");//提示删除结果
  283.             }
  284.             catch(SqlException ex)//捕获异常
  285.             {
  286.                 MessageBox.Show(ex.Message);
  287.             }
  288.             finally
  289.             {
  290.                 conn.Close();//关闭连接
  291.             }
  292.         }
  293.  
  294.         private void btnCancel_Click(object sender, System.EventArgs e)
  295.         {
  296.             Application.Exit();
  297.         }
  298.     }
  299. }

 

编程实现自制的DataSet 并在DataGird中显示

2008.06.17 / 标签: / 分类: WinForm
Sofa

编程实现自制的DataSet 并在DataGird中显示提示:根据我们熟悉的 建库 建表 建字段 建约束 添加数据行 显示 这个步骤来。 程序源码:

 
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8.  
  9. namespace DataSetTest
  10. {
  11.     /// <summary>
  12.     /// Form1 的摘要说明。
  13.     /// </summary>
  14.     public class Form1 : System.Windows.Forms.Form
  15.     {
  16.         private System.Windows.Forms.DataGrid dataGrid1;
  17.         private System.Windows.Forms.Button button1;
  18.         private System.Windows.Forms.Button button2;
  19.         /// <summary>
  20.         /// 必需的设计器变量。
  21.         /// </summary>
  22.         private System.ComponentModel.Container components = null;
  23.  
  24.         public Form1()
  25.         {
  26.             //
  27.             // Windows 窗体设计器支持所必需的
  28.             //
  29.             InitializeComponent();
  30.  
  31.             //
  32.             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  33.             //
  34.         }
  35.  
  36.         /// <summary>
  37.         /// 清理所有正在使用的资源。
  38.         /// </summary>
  39.         protected override void Dispose( bool disposing )
  40.         {
  41.             if( disposing )
  42.             {
  43.                 if (components != null
  44.                 {
  45.                     components.Dispose();
  46.                 }
  47.             }
  48.             base.Dispose( disposing );
  49.         }
  50.  
  51.         #region Windows 窗体设计器生成的代码
  52.         /// <summary>
  53.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  54.         /// 此方法的内容。
  55.         /// </summary>
  56.         private void InitializeComponent()
  57.         {
  58.             this.dataGrid1 = new System.Windows.Forms.DataGrid();
  59.             this.button1 = new span> System.Windows.Forms.Button();
  60.             this.button2 = new System.Windows.Forms.Button();
  61.             ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
  62.             this.SuspendLayout();
  63.             // 
  64.             // dataGrid1
  65.             // 
  66.             this.dataGrid1.DataMember = "";
  67.             this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  68.             this.dataGrid1.Location = new System.Drawing.Point(8, 8);
  69.             this.dataGrid1.Name = "dataGrid1";
  70.             this.dataGrid1.Size = new System.Drawing.Size(384, 168);
  71.             this.dataGrid1.TabIndex = 0;
  72.             // 
  73.             // button1
  74.             // 
  75.             this.button1.Location = new System.Drawing.Point(82, 184);
  76.             this.button1.Name = "button1";
  77.             this.button1.Size = new System.Drawing.Size(112, 23);
  78.             this.button1.TabIndex = 1;
  79.             this.button1.Text = "查看自建DataSet";
  80.             this.button1.Click += new System.EventHandler(this.button1_Click);
  81.             // 
  82.             // button2
  83.             // 
  84.             this.button2.Location = new System.Drawing.Point(207, 184);
  85.             this.button2.Name = "button2";
  86.             this.button2.Size = new System.Drawing.Size(116, 23);
  87.             this.button2.TabIndex = 2;
  88.             this.button2.Text = "查看数据库Jobs表";
  89.             this.button2.Click += new System.EventHandler(this.button2_Click);
  90.             // 
  91.             // Form1
  92.             // 
  93.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  94.             this.ClientSize = new System.Drawing.Size(400, 221);
  95.             this.Controls.Add(this.button2);
  96.             this.Controls.Add(this.button1);
  97.             this.Controls.Add(this.dataGrid1);
  98.             this.Name = "Form1";
  99.             this.Text = "Form1";
  100.             ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
  101.             this.ResumeLayout(false);
  102.  
  103.         }
  104.         #endregion
  105.  
  106.         /// <summary>
  107.         /// 应用程序的主入口点。
  108.         /// </summary>
  109.         [STAThread]
  110.         static void Main() 
  111.         {
  112.             Application.Run(new Form1());
  113.         }
  114.  
  115.         private void button1_Click(object sender, System.EventArgs e)
  116.         {
  117.             DataSet ds = new DataSet();//创建DataSet对象
  118.             DataTable dt = new DataTable();//创建数据表
  119.             DataColumn dc = dt.Columns.Add("编号",typeof(string));//为数据表添加字段
  120.  
  121.             dt.Columns.Add("姓名",typeof(string));//创建约束
  122.             dt.Columns.Add("年龄",typeof(int));
  123.             dt.Columns["编号"].AllowDBNull=false;
  124.             dt.Columns["姓名"].Unique = true;
  125.             dt.Columns["年龄"].DefaultValue=0;
  126.  
  127.             DataRow dr = dt.NewRow();//创建新的行
  128.             dr["编号"] = "a100";//填充值
  129.             dr["姓名"] = "neeke";
  130.             dr["年龄"] = 20;
  131.             dt.Rows.Add(dr);//加入到表的行集合中
  132.             dr = dt.NewRow();
  133.             dr["编号"] = "a101";
  134.             dr["姓名"] = "jerry";
  135.             dr["年龄"] = "19";
  136.             dt.Rows.Add(dr);
  137.             this.dataGrid1.DataSource = dt;//显示在dataGrid中
  138.  
  139.             
  140.  
  141.     }
  142.  
  143.         private void button2_Click(object sender, System.EventArgs e)
  144.         {
  145.             try
  146.             {
  147.                 SqlConnection conn = new SqlConnection("server = .;uid = sa;pwd = ;database = pubs");//创建连接
  148. &n
    bsp;               SqlDataAdapter sda = 
    new SqlDataAdapter();//创建适配器
  149.                 DataSet ds = new DataSet();//创建DataSet对象
  150.                 conn.Open();//打开连接
  151.                 sda.SelectCommand = new SqlCommand("select * from jobs",conn);//查询数据
  152.                 sda.Fill(ds,"jobs");//填充ds
  153.  
  154.                 this.dataGrid1.DataSource = ds.Tables[0];//绑定并显示
  155.             }
  156.             catch(SqlException ex)
  157.             {
  158.                 MessageBox.Show(ex.Message);
  159.             }
  160.         }
  161.     }
  162. }

 

C# p2p应用程序示例

2008.06.14 / 标签: / 分类: WinForm
Sofa

P2P,即英文peer-to-peer的缩写,译为对等互联或点对点技术。p2p是一种用于在不同PC用户之间,不经过中继设备直接交换数据或服务的技术,它允许Internet用户直接使用对方的文件。网络中的任意用户都可以直接连接到其他用户的计算机,并进行文件的交换,而不需要连接到服务器上在进行浏览与下载。因为消除了中间环节,p2p技术是的网络上的沟通变得更容易、更直接。此程序演示了如何从一点向另一点发送文件。 源码1:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Net;
  10. using System.Net.Sockets;
  11.  
  12. namespace FileReceive
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             IPAddress ip = IPAddress.Parse("127.0.0.1");
  24.             TcpListener tcplistener = new TcpListener(ip, 4525);
  25.             tcplistener.Start();
  26.  
  27.             TcpClient tcpclient = tcplistener.AcceptTcpClient();
  28.             NetworkStream ns = tcpclient.GetStream();
  29.             StreamReader sr = new StreamReader(ns,Encoding.Default);
  30.  
  31.             string result = sr.ReadToEnd();
  32.             this.textBox1.Text = result;
  33.             sr.Close();
  34.             ns.Close();
  35.             tcpclient.Close();
  36.             tcplistener.Stop();
  37.         }
  38.     }
  39. }

源码2:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Net;
  10. using System.Net.Sockets;
  11.  
  12. namespace Peer2Peer
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.        &nbs
    p;    InitializeComponent();
  19.         }
  20.  
  21.         private void btnSend_Click(object sender, EventArgs e)
  22.         {
  23.             
  24.             this.openFileDialog1.ShowDialog();
  25.             string filename = this.openFileDialog1.FileName;
  26.             FileStream fs = File.Open(filename, FileMode.Open);
  27.  
  28.             TcpClient tcpclient = new TcpClient(txtIp.Text, int.Parse(txtPort.Text));
  29.             NetworkStream ns = tcpclient.GetStream();
  30.             
  31.             int data = fs.ReadByte();
  32.             while (data != -1)
  33.             {
  34.                 ns.WriteByte((byte)data);
  35.                 data = fs.ReadByte();
  36.             }
  37.             fs.Close();
  38.             ns.Close();
  39.             tcpclient.Close();
  40.         }
  41.     }
  42. }

 

C#网页浏览器、网页源码浏览器

2008.06.14 / 标签: / 分类: WinForm

winform的高级应用,我咋就不觉得有多高级呢?这个C#网页浏览器、网页源码浏览器就是所谓高级应用。代码很短,很简单。所以就不写注释了。效果图: 源码:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.IO;
  11.  
  12. namespace WebBrowser
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void btnSeeCode_Click(object sender, EventArgs e)
  22.         {
  23.             axWebBrowser1.Visible = false;
  24.             WebClient wbc = new WebClient();
  25.             Stream stm = wbc.OpenRead(txtUrl.Text);
  26.             StreamReader sr = new StreamReader(stm,Encoding.Default);
  27.             string line;
  28.             string lines = "";
  29.             while ((line = sr.ReadLine()) != null)
  30.             {
  31.                 lines += line;
  32.             }
  33.             txtCode.Text = lines;
  34.         }
  35.  
  36.         private void btnView_Click(object sender, EventArgs e)
  37.         {
  38.             axWebBrowser1.Visible = true;
  39.             axWebBrowser1.Navigate(txtUrl.Text);
  40.         }
  41.     }
  42. }

 

无觅相关文章插件,快速提升流量