激活窗口和取消激活窗口
在MDI中打开多个文档时,知道哪个子窗口响应“键按下”等操作很重要。假定有两个打开的子窗口,各容纳一个文档,再按下A键时,只在一个MDI字窗口中显示字符A,而不是在两个窗口都显示。这里的问题是:“哪个文档接受按键操作?”答案是:“活动窗口”中的文档。有多个窗口打开时,在同一时间只有一个窗口是“活动”的。应用程序的活动窗口是响应所有操作的窗口,通常最上面的窗口是活动窗口。另外,活动窗口的标题栏颜色一般不同于非活动的窗口。
阅读全文>>
Winform主窗体和子窗体
MDI应用程序由一个MDI父窗体和一个或多个子窗体构成。MDI父窗体是MDI应用程序的基础,它包含多个MDI子窗体,子窗口是用户与MDI应用程序进行交互的副窗口。
MDI父窗体有几个显著特点:
阅读全文>>
“记事本”和“Internet Explorer”是两个常用的Windows应用程序。对于这两个应用程序,任何时候应用程序实例内部都只有一个文档打开。也就是说,当“记事本”或“Internet Explorer”中已有一个文件打开时,若要再打开另外一个新文件,就必须先关闭第一个文件才能打开新的文件。如果要同时查看两个文件,就必须要应用另外一个应用程序实例。这种应用程序的一个实例不允许用户同时打开多个文件或窗口,因此被成为SDI(Single Document Interface,单文档界面)应用程序。
阅读全文>>
程序设计要求:要能够查询mssql2000数据库中pubs库的jobs表中的数据显示出来,并可对数据库进行修改、删除等操作。
程序源码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace DataTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- SqlConnection conn;//数据库连接对象
- SqlDataAdapter adpter;//适配器对象
- DataSet ds;//数据集对象
- private void button1_Click(object sender, EventArgs e)
- {
- conn = new SqlConnection("server=.;uid=sa;pwd=;database=pubs");
- adpter = new SqlDataAdapter("select * from jobs",conn);
- ds = new DataSet();
- adpter.Fill(ds,"jobs");
- this.dataGridView1.DataSource = ds.Tables[0];
- }
- private void button3_Click(object sender, EventArgs e)
- {
- ds.Tables[0].Rows[this.dataGridView1.CurrentRow.Index].Delete();//删除鼠标选定的数据
- SqlCommandBuilder scb = new SqlCommandBuilder(adpter);
- adpter.Update(ds,"jobs");//更新数据库
- }
- private void button2_Click(object sender, EventArgs e)
- {
- //保存数据
- SqlCommandBuilder scb = new SqlCommandBuilder(adpter);
- adpter.Update(ds, "jobs");
- }
- }
- }
数据集中的每个DataTable对象都标示一个数据库检索到的表,每个表的列和约束用于定义DataTable得结构。第一次创建DataTable时,其中不含该结构,通过创建DataColumn对象并将其添加至表的Columns集合中来定义该表的结构。
DataTable的属性、方法和事件
阅读全文>>
数据集
数据集是一个用于存储从数据库检索到的数据的对象,数据集可以简单的理解成为一个临时数据库,他是断开式、存储在内存中的。可以从任何有效数据源(如SQL Server、Oracle、文本文件或XML文件)将数据加载到数据集中。数据集是由数据行和列、约束和有关表对象中数据关系的信息组成的零个或多个表对象的集合。这些数据缓存在本地机上,不需要与数据库持续连接。
阅读全文>>
ado.net实现对数据库的添加、删除和查看操作。提示:添加删除都不是查询所以我们可以使用SqlCommand 的ExecuteNonQuery完成。
程序源码:
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Data.SqlClient;
- namespace Infomation
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.TextBox txtFlightNum;
- private System.Windows.Forms.TextBox txtName;
- private System.Windows.Forms.TextBox txtCertificate;
- private System.Windows.Forms.TextBox txtSeatId;
- private System.Windows.Forms.TextBox txtAge;
- private System.Windows.Forms.Button btnAdd;
- private System.Windows.Forms.Button btnDelete;
- private System.Windows.Forms.Button btnCancel;
- private System.Windows.Forms.ComboBox cboSex;
- private System.Windows.Forms.Label lblFlightNum;
- private System.Windows.Forms.Label lblName;
- private System.Windows.Forms.Label lblSex;
- private System.Windows.Forms.Label lblCertificate;
- private System.Windows.Forms.Label lblSeatId;
- private System.Windows.Forms.Label lblAge;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- &n
bsp; components.Dispose(); - }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.lblFlightNum = new System.Windows.Forms.Label();
- this.lblName = new System.Windows.Forms.Label();
- this.lblSex = new System.Windows.Forms.Label();
- this.lblCertificate = new System.Windows.Forms.Label();
- this.lblSeatId = new System.Windows.Forms.Label();
- this.lblAge = new System.Windows.Forms.Label();
- this.txtFlightNum = new System.Windows.Forms.TextBox();
- this.txtName = new System.Windows.Forms.TextBox();
- this.txtCertificate = new System.Windows.Forms.TextBox();
- this.txtSeatId = new System.Windows.Forms.TextBox();
- this.txtAge = new System.Windows.Forms.TextBox();
- this.btnAdd = new System.Windows.Forms.Button();
- this.btnDelete = new System.Windows.Forms.Button();
- this.btnCancel = new System.Windows.Forms.Button();
- this.cboSex = new System.Windows.Forms.ComboBox();
- this.SuspendLayout();
- //
- // lblFlightNum
- //
- this.lblFlightNum.Location = new System.Drawing.Point(24, 24);
- this.lblFlightNum.Name = "lblFlightNum";
- this.lblFlightNum.Size = new System.Drawing.Size(48, 23);
- this.lblFlightNum.TabIndex = 0;
- this.lblFlightNum.Text = "航班号:";
- //
- // lblName
- //
- this.lblName.Location = new System.Drawing.Point(24, 80);
- this.lblName.Name&
nbsp;= "lblName"; - this.lblName.Size = new System.Drawing.Size(40, 23);
- this.lblName.TabIndex = 1;
- this.lblName.Text = "姓名:";
- //
- // lblSex
- //
- this.lblSex.Location = new System.Drawing.Point(24, 136);
- this.lblSex.Name = "lblSex";
- this.lblSex.Size = new System.Drawing.Size(40, 23);
- this.lblSex.TabIndex = 2;
- this.lblSex.Text = "性别:";
- //
- // lblCertificate
- //
- this.lblCertificate.Location = new System.Drawing.Point(256, 24);
- this.lblCertificate.Name = "lblCertificate";
- this.lblCertificate.Size = new System.Drawing.Size(48, 23);
- this.lblCertificate.TabIndex = 3;
- this.lblCertificate.Text = "证件号:";
- //
- // lblSeatId
- //
- this.lblSeatId.Location = new System.Drawing.Point(256, 80);
- this.lblSeatId.Name = "lblSeatId";
- this.lblSeatId.Size = new System.Drawing.Size(48, 23);
- this.lblSeatId.TabIndex = 4;
- this.lblSeatId.Text = "座位号:";
- //
- // lblAge
- //
- this.lblAge.Location = new System.Drawing.Point(256, 136);
- this.lblAge.Name = "lblAge";
- this.lblAge.Size = new System.Drawing.Size(40, 23);
- this.lblAge.TabIndex = 5;
- this.lblAge.Text = "年龄:";
- //
- // txtFlightNum
- //
- this.txtFlightNum.Location = new System.Drawing.Point(88, 24);
- this.txtFlightNum.Name =
pan class="string">"txtFlightNum"; - this.txtFlightNum.TabIndex = 6;
- this.txtFlightNum.Text = "";
- //
- // txtName
- //
- this.txtName.Location = new System.Drawing.Point(88, 80);
- this.txtName.Name = "txtName";
- this.txtName.Size = new System.Drawing.Size(136, 21);
- this.txtName.TabIndex = 7;
- this.txtName.Text = "";
- //
- // txtCertificate
- //
- this.txtCertificate.Location = new System.Drawing.Point(320, 24);
- this.txtCertificate.Name = "txtCertificate";
- this.txtCertificate.Size = new System.Drawing.Size(168, 21);
- this.txtCertificate.TabIndex = 8;
- this.txtCertificate.Text = "";
- //
- // txtSeatId
- //
- this.txtSeatId.Location = new System.Drawing.Point(320, 80);
- this.txtSeatId.Name = "txtSeatId";
- this.txtSeatId.Size = new System.Drawing.Size(168, 21);
- this.txtSeatId.TabIndex = 9;
- this.txtSeatId.Text = "";
- //
- // txtAge
- //
- this.txtAge.Location = new System.Drawing.Point(320, 136);
- this.txtAge.Name = "txtAge";
- this.txtAge.TabIndex = 10;
- this.txtAge.Text = "";
- //
- // btnAdd
- //
- this.btnAdd.Location = new System.Drawing.Point(192, 216);
- this.btnAdd.Name = "btnAdd";
- this.btnAdd.TabIndex = 11;
- this.btnAdd.Text = "添加";
-  
; this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click); - //
- // btnDelete
- //
- this.btnDelete.Location = new System.Drawing.Point(296, 216);
- this.btnDelete.Name = "btnDelete";
- this.btnDelete.TabIndex = 12;
- this.btnDelete.Text = "删除";
- this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
- //
- // btnCancel
- //
- this.btnCancel.Location = new System.Drawing.Point(400, 216);
- this.btnCancel.Name = "btnCancel";
- this.btnCancel.TabIndex = 13;
- this.btnCancel.Text = "取消";
- this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
- //
- // cboSex
- //
- this.cboSex.Items.AddRange(new object[] {
- "男",
- "女"});
- this.cboSex.Location = new System.Drawing.Point(88, 136);
- this.cboSex.Name = "cboSex";
- this.cboSex.Size = new System.Drawing.Size(121, 20);
- this.cboSex.TabIndex = 14;
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(512, 273);
- this.Controls.Add(this.cboSex);
- this.Controls.Add(this.btnCancel);
- this.Controls.Add(this.btnDelete);
- this.Controls.Add(this.btnAdd);
- this.Controls.Add(this.txtAge);
- > this.Controls.Add(this.txtSeatId);
- this.Controls.Add(this.txtCertificate);
- this.Controls.Add(this.txtName);
- this.Controls.Add(this.txtFlightNum);
- this.Controls.Add(this.lblAge);
- this.Controls.Add(this.lblSeatId);
- this.Controls.Add(this.lblCertificate);
- this.Controls.Add(this.lblSex);
- this.Controls.Add(this.lblName);
- this.Controls.Add(this.lblFlightNum);
- this.Name = "Form1";
- this.Text = "乘客详细信息";
- this.Load += new System.EventHandler(this.Form1_Load);
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- //创建数据库连接对象及命令对象
- private SqlConnection conn;
- private SqlCommand cmd;
- private void btnAdd_Click(object sender, System.EventArgs e)
- {
- 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)+")";
- try
- {
- conn.Open();//打开数据库连接
- cmd = new SqlCommand(sql,conn);
- cmd.ExecuteNonQuery();//执行插入命令
- MessageBox.Show("数据添加成功");//提示执行结果
- }
- catch(SqlException ex)//捕获异常
- {
-  
; MessageBox.Show(ex.Message); - }
- finally
- {
- conn.Close();//关闭连接
- }
- }
- private void Form1_Load(object sender, System.EventArgs e)
- {
- //程序载入时创建数据库连接
- conn = new SqlConnection("server = .;uid = sa;pwd = ;database = pubs");
- }
- private void btnDelete_Click(object sender, System.EventArgs e)
- {
- string sql = "delete from Flight where FlightNum =’"+this.txtFlightNum.Text+"’";
- try
- {
- conn.Open();//打开连接
- cmd = new SqlCommand(sql,conn);
- cmd.ExecuteNonQuery();//执行删除数据操作
- MessageBox.Show("数据删除成功");//提示删除结果
- }
- catch(SqlException ex)//捕获异常
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- conn.Close();//关闭连接
- }
- }
- private void btnCancel_Click(object sender, System.EventArgs e)
- {
- Application.Exit();
- }
- }
- }
编程实现自制的DataSet 并在DataGird中显示提示:根据我们熟悉的 建库 建表 建字段 建约束 添加数据行 显示 这个步骤来。
程序源码:
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Data.SqlClient;
- namespace DataSetTest
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.DataGrid dataGrid1;
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.Button button2;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.dataGrid1 = new System.Windows.Forms.DataGrid();
- this.button1 = new
span> System.Windows.Forms.Button(); - this.button2 = new System.Windows.Forms.Button();
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
- this.SuspendLayout();
- //
- // dataGrid1
- //
- this.dataGrid1.DataMember = "";
- this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
- this.dataGrid1.Location = new System.Drawing.Point(8, 8);
- this.dataGrid1.Name = "dataGrid1";
- this.dataGrid1.Size = new System.Drawing.Size(384, 168);
- this.dataGrid1.TabIndex = 0;
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(82, 184);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(112, 23);
- this.button1.TabIndex = 1;
- this.button1.Text = "查看自建DataSet";
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(207, 184);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(116, 23);
- this.button2.TabIndex = 2;
- this.button2.Text = "查看数据库Jobs表";
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(400, 221);
- this.Controls.Add(this.button2);
- this.Controls.Add(this.button1);
- this.Controls.Add(this.dataGrid1);
- this.Name = "Form1";
- this.Text = "Form1";
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- private void button1_Click(object sender, System.EventArgs e)
- {
- DataSet ds = new DataSet();//创建DataSet对象
- DataTable dt = new DataTable();//创建数据表
- DataColumn dc = dt.Columns.Add("编号",typeof(string));//为数据表添加字段
- dt.Columns.Add("姓名",typeof(string));//创建约束
- dt.Columns.Add("年龄",typeof(int));
- dt.Columns["编号"].AllowDBNull=false;
- dt.Columns["姓名"].Unique = true;
- dt.Columns["年龄"].DefaultValue=0;
- DataRow dr = dt.NewRow();//创建新的行
- dr["编号"] = "a100";//填充值
- dr["姓名"] = "neeke";
- dr["年龄"] = 20;
- dt.Rows.Add(dr);//加入到表的行集合中
- dr = dt.NewRow();
- dr["编号"] = "a101";
- dr["姓名"] = "jerry";
- dr["年龄"] = "19";
- dt.Rows.Add(dr);
- this.dataGrid1.DataSource = dt;//显示在dataGrid中
- }
- private void button2_Click(object sender, System.EventArgs e)
- {
- try
- {
- SqlConnection conn = new SqlConnection("server = .;uid = sa;pwd = ;database = pubs");//创建连接
- &n
bsp; SqlDataAdapter sda = new SqlDataAdapter();//创建适配器 - DataSet ds = new DataSet();//创建DataSet对象
- conn.Open();//打开连接
- sda.SelectCommand = new SqlCommand("select * from jobs",conn);//查询数据
- sda.Fill(ds,"jobs");//填充ds
- this.dataGrid1.DataSource = ds.Tables[0];//绑定并显示
- }
- catch(SqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }
P2P,即英文peer-to-peer的缩写,译为对等互联或点对点技术。p2p是一种用于在不同PC用户之间,不经过中继设备直接交换数据或服务的技术,它允许Internet用户直接使用对方的文件。网络中的任意用户都可以直接连接到其他用户的计算机,并进行文件的交换,而不需要连接到服务器上在进行浏览与下载。因为消除了中间环节,p2p技术是的网络上的沟通变得更容易、更直接。此程序演示了如何从一点向另一点发送文件。
源码1:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- namespace FileReceive
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- IPAddress ip = IPAddress.Parse("127.0.0.1");
- TcpListener tcplistener = new TcpListener(ip, 4525);
- tcplistener.Start();
- TcpClient tcpclient = tcplistener.AcceptTcpClient();
- NetworkStream ns = tcpclient.GetStream();
- StreamReader sr = new StreamReader(ns,Encoding.Default);
- string result = sr.ReadToEnd();
- this.textBox1.Text = result;
- sr.Close();
- ns.Close();
- tcpclient.Close();
- tcplistener.Stop();
- }
- }
- }
源码2:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- namespace Peer2Peer
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- &nbs
p; InitializeComponent(); - }
- private void btnSend_Click(object sender, EventArgs e)
- {
- this.openFileDialog1.ShowDialog();
- string filename = this.openFileDialog1.FileName;
- FileStream fs = File.Open(filename, FileMode.Open);
- TcpClient tcpclient = new TcpClient(txtIp.Text, int.Parse(txtPort.Text));
- NetworkStream ns = tcpclient.GetStream();
- int data = fs.ReadByte();
- while (data != -1)
- {
- ns.WriteByte((byte)data);
- data = fs.ReadByte();
- }
- fs.Close();
- ns.Close();
- tcpclient.Close();
- }
- }
- }
winform的高级应用,我咋就不觉得有多高级呢?这个C#网页浏览器、网页源码浏览器就是所谓高级应用。代码很短,很简单。所以就不写注释了。效果图: 
源码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- namespace WebBrowser
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void btnSeeCode_Click(object sender, EventArgs e)
- {
- axWebBrowser1.Visible = false;
- WebClient wbc = new WebClient();
- Stream stm = wbc.OpenRead(txtUrl.Text);
- StreamReader sr = new StreamReader(stm,Encoding.Default);
- string line;
- string lines = "";
- while ((line = sr.ReadLine()) != null)
- {
- lines += line;
- }
- txtCode.Text = lines;
- }
- private void btnView_Click(object sender, EventArgs e)
- {
- axWebBrowser1.Visible = true;
- axWebBrowser1.Navigate(txtUrl.Text);
- }
- }
- }