WinForm常用代码
//ToolStripSplitButton是标准按钮和下拉按钮的组合,各自工作,但有联系,感觉上后者是没有向下箭头ToolStripDropDownButton;
ToolStripDropDownButton只含有一个按钮,可以选择有没有向下箭头的标志,单击时显示关联的 ToolStripDropDown 的控件。两者均可改变箭头标志在做还是在右。 //VS自带双缓冲
this.SetStyle(ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, true); //控件双缓冲
Control.DoubleBuffered=true; //attribute modfied by Protected //手工双缓冲
Bitmap bmp = new Bitmap(600, 600);
Graphics g = Graphics.FromImage(bmp);
g.DrawLine();
this.CreateGraphics().DrawImage(bmp, 0, 0);//这句是关键,不能在OnPaint里画BitBmp在这里调Invalidate Invalidate(Rectangle)//规定区域重绘,解决闪烁的另一种方法 ComboBox ComboBox1 = (ComboBox) sender;
(Sender as SomeObject).Method() this.label1.Font = new System.Drawing.Font("微软雅黑", 72F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Font = new Font("微软雅黑", fontSize); //自定义控件背景透明
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent; //获得程序集
System.Reflection.Assembly assem = System.Reflection.Assembly.GetExecutingAssembly(); //点移位
Point.Offset(Point);
Point.Offset(int,int); Rectangle.Contains(Point); //截获标题栏消息,自画标题栏
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D; [DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); private const int WM_NCPAINT = 0x0085;
private const int WM_NCACTIVATE = 0x0086;
private const int WM_NCLBUTTONDOWN = 0x00A1;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
Rectangle vRectangle = new Rectangle((Width - 75) / 2, 3, 75, 25);
switch (m.Msg)
{
case WM_NCPAINT:
case WM_NCACTIVATE:
IntPtr vHandle = GetWindowDC(m.HWnd);
Graphics vGraphics = Graphics.FromHdc(vHandle);
vGraphics.FillRectangle(new LinearGradientBrush(vRectangle,
Color.Pink, Color.Purple, LinearGradientMode.BackwardDiagonal),
vRectangle); StringFormat vStringFormat = new StringFormat();
vStringFormat.Alignment = StringAlignment.Center;
vStringFormat.LineAlignment = StringAlignment.Center;
vGraphics.DrawString("About", Font, Brushes.BlanchedAlmond,
vRectangle, vStringFormat); vGraphics.Dispose();
ReleaseDC(m.HWnd, vHandle);
break;
case WM_NCLBUTTONDOWN:
Point vPoint = new Point((int)m.LParam);
vPoint.Offset(-Left, -Top);
if (vRectangle.Contains(vPoint))
MessageBox.Show(vPoint.ToString());
break;
}
} Control.SuspendLayout;//在它和ResumeLayout之间的代码不会引起Parent Control的重绘
Control.AddRange(new Control[]{});//添加多个控件
Control.ResumeLayout;// 在它和SuspendLayout之间的代码不会引起Parent Control的重绘 Button[] buttons = new Button[] {};//大胆地设类数组吧~
Button.SetBounds(int,int,int,int);//设置Button的左、右、宽、高; //应该尽可能地用Anchor、Dock,特殊情况下用Layout事件 Form.MdiParent=(Form);//设置MDI父窗口 //Active事件里this.Hide()是正道 Form.Show();
Form.Text=”Mytext”;//这两句的顺序不能 //static不能修饰临时变量,一般用来修饰类变量(不是类的对象实例变量!!!) Form.MdiParent = this;
Form.TopLevel = true;
Form.IsMdiContainer= true; Form. ActivateMdiChild //sqlconnection连接字符串
@"Data Source= .\SQLEXPRESS;AttachDBFilename=C:\..\*.MDF;Integrated Security=True;User Instance=True")) //sqlconnection连接的基本步骤
using System.Data.SqlClient; Dataset dataset = new DataSet(); using (SqlConnection conn = new SqlConnection(@"Data Source= .\SQLEXPRESS;AttachDBFilename=C:\SQL Server 2000 Sample Databases\NORTHWND.MDF;Integrated Security=True;User Instance=True"))
{
conn.Open(); SqlDataAdapter adapter = new SqlDataAdapter(conn.CreateCommand());
adapter.SelectCommand.CommandText = "select * from customers"; adapter.Fill(dataset); foreach (DataRow row in dataset.Tables[0].Rows)
{
string item=row["ContactTitle"]+","+row["ContactName"];
listBox1.Items.Add(item);
}
} ListBox.Items.Add(new string)//ListBox添加项 //创建DataSet中的记录
DataRow row = DataSet.Tables[0].NewRow();
row["**"] =***;
dataset.Tables[0].Rows.Add(row); //更新DataSet
DataRow row=DataSet.Table[0].Rows[index];
row[“***”]=***; //删除DataSet中的记录
DataSet.Tables[0].Rows.Remove(DataSet.Table[0].Rows[index]); //DataRow.Delete()和DataSet.Tables[0].Rows.Remove()不一样,后者是从DataSet中彻底删除
DataRow row=DataSet.Table[0].Rows[index];
row[“***”]=***; row.delete();
TYPE varable=row[“***”,DataRowVersion.Original] // DataRow的完整访问方式和DataRow.RowState
Switch (row.RowState)
{
case DataRowState.Deleted:
row["***", DataRowVersion.Original]; case DataRowState.Added:
row["["***"] case DataRowState.Modified:
row["***", DataRowVersion.Original]
row["***", DataRowVersion.Current]
case DataRowVersion.Unchanged:
row["***"]
} //获取部分特定状态的数据集
DataTable modifiedTable = DataSet.Tables[0].GetChanges(DataRowState.Added| DataRowState.Modified| DataRowState.Deleted); //创建数据库查询连接适配器的几种方式
SqlDataAdapter adapter = new SqlDataAdapter("select * from TABLENAME", SqlConnection); //最简单 SqlDataAdapter adapter = new SqlDataAdapter(SqlConnection.CreateCommand());
adapter.SelectCommand.CommandText = "select * from TABLENAME "; SqlDataAdapter adapter = new sqldat SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("select * from TABLENAME ", SqlConnection); //万能的数据集更新器SqlCommandBuilder
SqlDataAdapter adapter = new SqlDataAdapter("select * from TABLENAME ", SqlConnection); new SqlCommandBuilder(adapter); try
{
adapter.Update(modifiedDataSet);
PoulateListBox();
}
catch (System.Exception ex)
{
} //多表数据集建议分别创建适配器 SqlDataAdapter adapter1 = new SqlDataAdapter("select * from TABLENAME", SqlConnection);
adapter1.Fill(DataSet,”TABLENAME1”); SqlDataAdapter adapter2 = new SqlDataAdapter("select * from TABLENAME", SqlConnection);
adapter2.Fill(DataSet,”TABLENAME2”);
//
//Make some changes to the DataSet .TABLENAME1 or DataSet .TABLENAME2
//
new SqlCommandBuilder(adapter1);
adapter1.Update(DataSet,” TABLENAME1”); new SqlCommandBuilder(adapter2);
adapter2.Update(DataSet,” TABLENAME2”); //创建DataSet自带约束
UniqueConstraint constrint = new UniqueConstraint(DataTable.Columns["***"]);//唯一性约束
DataTable.Constraints.Add(constrint);
//外键约束:ForeignKeyConstraint //关系基于两张表的两个列上,添加于两张表共属的数据集,并且自动生成分别在两个表上生成UniqueConstraint 和ForeignKeyConstraint
DataRelation relation = new DataRelation("CustomersOrders", DataTable.Columns["***"], DataTable.Columns["***"]);
dataset.Relations.Add(relation); Form.Modal//判断显示方式是模式还是非模式,模式为true,非模式为false,只有在Load事件中和之后该属性才有实际意义,在构造期间默认为false myForm.Control1.Text=”Data put in by a user”;//这样不好,封装性不强不易维护更新,用下面的 pulbic String Control1Text
{
get{
return Control1.Text;
}
Set{
Control1.Text;=value;
}
}
//…
myForm. Control1Text=” Data put in by a user”; //DialogResult res=ShowDialog()只是获取对话框结果的快捷方式,完整方式如下
void someButton_Click(object sender,EventArgs e){
this.DialogResult=DialogResult.Retry;
this.close();
} someForm=new someForm(); someForm.showDialog();
DialogResult ref= someForm .DialogResult;
if(ref= DialogResult.Retry)
//… string path =Directory.GetCurrentDirectory();
System.IO.FileStream aFile = new System.IO.FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(aFile, System.Text.Encoding.Default); /*
对于每个关联的 SqlConnection,一次只能打开一个 SqlDataReader
SqlConnection 与 SqlDataAdapter 和 SqlCommand 一起使用,可以在连接 Microsoft SQL Server 数据库时提高性能。
对于所有第三方 SQL 服务器产品以及其他支持 OLE DB 的数据源,请使用 OleDbConnection。
SqlConnection 超出范围,则不会将其关闭。因此,必须通过调用 Close 或 Dispose 显式关闭该连接。最好在using 块内部打开连接。
连接自字符串关键字不区分大小写,并将忽略键/值对之间的空格。 不过,根据数据源的不同,值可能是区分大小写的。 任何包含分号、单引号或双引号的值必须用双引号引起来。
*/ System.Data.SqlClient.SqlConnectionStringBuilder builder =new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Data Source"] = "(local)";
builder["integrated Security"] = true;
builder["Initial Catalog"] = "AdventureWorks;NewValue=Bad";
// 使用System.Data.SqlClient.SqlConnectionStringBuilder不需要担心分号、单引号或双引号的转义问题
Console.WriteLine(builder.ConnectionString); //打开数据库的某个古老方法
SqlConnection mc=new SqlConnection();
mc.ConnectionString=”/*…*/”;
mc.Open(); //有关SqlCommand, SqlDataReader的基本使用
SqlCommand scm=SqlConnection.CreateCommand();
scm.CommandText=”select */*…*/”;
SqlDataReader sdr=scm.ExecuteReader();
sdr.Read();
//…
sdr.close(); //以ToolStrip为例绘制简便背景
e.Graphics.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(
new System.Drawing.Point(0, toolStrip1.Height),
new System.Drawing.Point(0, 0),
Color.FromKnownColor(KnownColor.ControlDark),
Color.FromKnownColor(KnownColor.ControlLight)),
toolStrip1.ClientRectangle); //获取Color的几种方式
Color.FromKnownColor(KnownColor.ControlLight);
Color.FromArgb(int r,int g,int b);
Color.FromArgb(int a,int r,int g,int b);//a表示透明度,0-255,0为全透明,255为不透明,
/*
如果安装时,改了实例名,也就是命名实例,那么客户端在连接时,要使用机器名加实例名来进行标识:计算机名\实例名。
*/
//This table shows all connection string properties for the ADO.NET SqlConnection object. Most of the properties are also used in ADO. All properties and descriptions is from msdn.
|
Name |
Default |
Description |
|
Application Name |
The name of the application, or '.Net SqlClient Data Provider' if no application name is provided. |
|
|
AttachDBFilename |
The name of the |
|
|
Connect Timeout |
15 |
The length of |
|
Connection |
0 |
When a connection |
|
Connection Reset |
'true' |
Determines |
|
Current Language |
The SQL Server |
|
|
Data Source |
The name or |
|
|
Enlist |
'true' |
When true, the |
|
Initial Catalog |
The name of the |
|
|
Integrated |
'false' |
Whether the |
|
Max Pool Size |
100 |
The maximum |
|
Min Pool Size |
0 |
The minimum |
|
Network Library |
'dbmssocn' |
The network |
|
Packet Size |
8192 |
Size in bytes of |
|
Password |
The password for |
|
|
Persist Security |
'false' |
When set to |
|
Pooling |
'true' |
When true, the |
|
User ID |
The SQL Server |
|
|
Workstation ID |
the local |
The name of the |
WinForm常用代码的更多相关文章
- Winform常用开发模式第一篇
Winform常用开发模式第一篇 上一篇博客最后我提到“异步编程模型”(APM),之后本来打算整理一下这方面的材料然后总结一下写篇文章与诸位分享,后来在整理的过程中不断的延伸不断地扩展,发现完全偏离了 ...
- DevExpress Winform 常用控件
Ø 前言 DevExpress 控件的功能比较强大,是全球知名控件开发公司,对于开发 B/S 或 C/S 都非常出色,可以实现很炫且功能强大的效果. DevExpress Winform 常用控件是 ...
- GCD 常用代码
GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...
- 转--Android实用的代码片段 常用代码总结
这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下 1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...
- 刀哥多线程之03GCD 常用代码
GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...
- jquery常用代码集锦
1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 ) 1 2 3 4 5 $.ajaxSetup({ ajaxSettings : { contentT ...
- Mysql:常用代码
C/S: Client Server B/S: Brower Server Php主要实现B/S .net IIS Jave TomCat LAMP:L Mysql:常用代码 Create table ...
- javascript常用代码大全
http://caibaojian.com/288.html 原文链接 jquery选中radio //如果之前有选中的,则把选中radio取消掉 $("#tj_cat .pro_ca ...
- Android 常用代码大集合 [转]
[Android]调用字符串资源的几种方法 字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...
随机推荐
- 【算法】插入排序 insertion_sort
准备写个<STL 源代码剖析>的读书笔记,开个专栏.名为<STL 的实现>,将源代码整理一遍.非常喜欢侯捷先生写在封底的八个字:天下大事.必作于细.他在书中写到:"我 ...
- BOOST 线程完全攻略 - 结束语
modulethread扩展多线程破解通讯 全文介绍了3个boost::thread的扩展类,希望能给大家书写多线程代码带来便捷. thread -> controlled_module_ex ...
- 1.jdk、Tomcat、solr的安装和配置
1.jdk安装和配置 1)根据电脑类型,到官网下载相应的jdk版本 2)双击jdk-8u5-windows-x64.exe安装包,一直点下一步就可以了,注意记住jdk和jre的安装目录. 3)环境变量 ...
- 1215.1——动态分配内存的补充realloc
当再次在原来申请的内存基础上再加内存的时候用realloc,如果第一次分配的内存后面存储地方够用,则连着原来的申请,如果不够用,就重新找到一块够用的地方,然后把原来的复制过去 int main(int ...
- c#程序为PDF文件填写表单内容
众所周知,PDF文件一般情况下是无法修改的,如果你有一张现成的PDF表格,这时想通过编程实现从数据库或者动态生成内容去填写这张表格,就会有些问题了,首先我们要解决以下2个重要的问题: 1.如何将内容写 ...
- python调webservice和COM接口
调webservice # -*- coding: cp936 -*- from suds.client import Client url = 'http://192.168.50.165/port ...
- struts2 0day漏洞
描述 Apache Struts2 近日出现一个0day漏洞,该漏洞在修补CVE-2014-0050和2014-0094两个安全漏洞处理不当,分别可以导致服务器受到拒绝服务攻击和被执行恶意代码. 漏洞 ...
- [Zookeeper研究]一 Zookeeper技术简介
最近的项目中使用到了Zookeeper.Kafka以及Storm.仔细研究了一下,觉得这几个开源项目对于搞分布式的人来说是非常有用的,所以想把自己的一点心得体会总结一下,希望能对大家有所帮助. 首先从 ...
- [转]MySQL 5.6 全局事务 ID(GTID)实现原理(一)
原文作者:淘长源 原文连接:http://qing.blog.sina.com.cn/1757661907/68c3cad333002qhe.html 转载注明以上信息 MySQL 5.6 的新特 ...
- BC 65 ZYB's Premutation (线段树+二分搜索)
题目简述:有一个全排列,一直每个前缀区间的逆序对数,还原这个排列. fi记录逆序对数,pi记录该位置数值,则k=fi-f(i-1)表示前i-1个数比pi大的数的个数,那么只要在剩余元素求出按大小顺序第 ...