您的位置:首页 > 产品设计 > UI/UE

GUID生成器(二)

2012-05-08 19:36 69 查看
郁闷,原来GUID的格式有这么多,而且C#可以自动提供诸如加{}的GUID格式生成,之前我还在生成的GUID(ToString()方法中参数为空)字符串上处理,画蛇添足地把{}加上。

唯一需要处理的内容,只是是否需要大写,或者也可以加上是否需要加"",因为GUID在sql中是以字符形式存储的,在写SQL语句时,需要""。

对于窗体总在最前显示和自动复制到剪切板还是很实用的,嘿嘿,虽然实现起来很简单。

运行截图:



不多说了,贴上代码。

From:

namespace Eyuan.GUIDGenerator
{
partial class fmGUIDGenerator
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtGUID = new System.Windows.Forms.TextBox();
this.btnGenerator = new System.Windows.Forms.Button();
this.btnCopy = new System.Windows.Forms.Button();
this.cbUppercase = new System.Windows.Forms.CheckBox();
this.cbAlwaysVisible = new System.Windows.Forms.CheckBox();
this.cbAutoCopy = new System.Windows.Forms.CheckBox();
this.gbGUIDFormat = new System.Windows.Forms.GroupBox();
this.rbX = new System.Windows.Forms.RadioButton();
this.rbP = new System.Windows.Forms.RadioButton();
this.rbB = new System.Windows.Forms.RadioButton();
this.rbD = new System.Windows.Forms.RadioButton();
this.rbN = new System.Windows.Forms.RadioButton();
this.gbGUIDFormat.SuspendLayout();
this.SuspendLayout();
//
// txtGUID
//
this.txtGUID.Location = new System.Drawing.Point(6, 5);
this.txtGUID.Name = "txtGUID";
this.txtGUID.Size = new System.Drawing.Size(444, 21);
this.txtGUID.TabIndex = 1;
this.txtGUID.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// btnGenerator
//
this.btnGenerator.Location = new System.Drawing.Point(4, 40);
this.btnGenerator.Name = "btnGenerator";
this.btnGenerator.Size = new System.Drawing.Size(75, 23);
this.btnGenerator.TabIndex = 2;
this.btnGenerator.Text = "生成(&G)";
this.btnGenerator.UseVisualStyleBackColor = true;
this.btnGenerator.Click += new System.EventHandler(this.btnGenerator_Click);
//
// btnCopy
//
this.btnCopy.Location = new System.Drawing.Point(92, 40);
this.btnCopy.Name = "btnCopy";
this.btnCopy.Size = new System.Drawing.Size(75, 23);
this.btnCopy.TabIndex = 3;
this.btnCopy.Text = "复制(&C)";
this.btnCopy.UseVisualStyleBackColor = true;
//
// cbUppercase
//
this.cbUppercase.AutoSize = true;
this.cbUppercase.Location = new System.Drawing.Point(180, 43);
this.cbUppercase.Name = "cbUppercase";
this.cbUppercase.Size = new System.Drawing.Size(48, 16);
this.cbUppercase.TabIndex = 4;
this.cbUppercase.Text = "大写";
this.cbUppercase.UseVisualStyleBackColor = true;
this.cbUppercase.CheckedChanged += new System.EventHandler(this.cbUppercase_CheckedChanged);
//
// cbAlwaysVisible
//
this.cbAlwaysVisible.AutoSize = true;
this.cbAlwaysVisible.Location = new System.Drawing.Point(241, 43);
this.cbAlwaysVisible.Name = "cbAlwaysVisible";
this.cbAlwaysVisible.Size = new System.Drawing.Size(72, 16);
this.cbAlwaysVisible.TabIndex = 6;
this.cbAlwaysVisible.Text = "显示最前";
this.cbAlwaysVisible.UseVisualStyleBackColor = true;
this.cbAlwaysVisible.CheckedChanged += new System.EventHandler(this.cbAlwaysVisible_CheckedChanged);
//
// cbAutoCopy
//
this.cbAutoCopy.AutoSize = true;
this.cbAutoCopy.Location = new System.Drawing.Point(326, 43);
this.cbAutoCopy.Name = "cbAutoCopy";
this.cbAutoCopy.Size = new System.Drawing.Size(120, 16);
this.cbAutoCopy.TabIndex = 7;
this.cbAutoCopy.Text = "自动复制到剪切板";
this.cbAutoCopy.UseVisualStyleBackColor = true;
this.cbAutoCopy.CheckedChanged += new System.EventHandler(this.cbAutoCopy_CheckedChanged);
//
// gbGUIDFormat
//
this.gbGUIDFormat.Controls.Add(this.rbX);
this.gbGUIDFormat.Controls.Add(this.rbP);
this.gbGUIDFormat.Controls.Add(this.rbB);
this.gbGUIDFormat.Controls.Add(this.rbD);
this.gbGUIDFormat.Controls.Add(this.rbN);
this.gbGUIDFormat.Location = new System.Drawing.Point(100, 78);
this.gbGUIDFormat.Name = "gbGUIDFormat";
this.gbGUIDFormat.Size = new System.Drawing.Size(257, 37);
this.gbGUIDFormat.TabIndex = 8;
this.gbGUIDFormat.TabStop = false;
this.gbGUIDFormat.Text = "GUID格式";
//
// rbX
//
this.rbX.AutoSize = true;
this.rbX.Location = new System.Drawing.Point(206, 13);
this.rbX.Name = "rbX";
this.rbX.Size = new System.Drawing.Size(29, 16);
this.rbX.TabIndex = 4;
this.rbX.TabStop = true;
this.rbX.Text = "X";
this.rbX.UseVisualStyleBackColor = true;
this.rbX.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
//
// rbP
//
this.rbP.AutoSize = true;
this.rbP.Location = new System.Drawing.Point(158, 13);
this.rbP.Name = "rbP";
this.rbP.Size = new System.Drawing.Size(29, 16);
this.rbP.TabIndex = 3;
this.rbP.TabStop = true;
this.rbP.Text = "P";
this.rbP.UseVisualStyleBackColor = true;
this.rbP.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
//
// rbB
//
this.rbB.AutoSize = true;
this.rbB.Location = new System.Drawing.Point(110, 13);
this.rbB.Name = "rbB";
this.rbB.Size = new System.Drawing.Size(29, 16);
this.rbB.TabIndex = 2;
this.rbB.TabStop = true;
this.rbB.Text = "B";
this.rbB.UseVisualStyleBackColor = true;
this.rbB.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
//
// rbD
//
this.rbD.AutoSize = true;
this.rbD.Location = new System.Drawing.Point(62, 13);
this.rbD.Name = "rbD";
this.rbD.Size = new System.Drawing.Size(29, 16);
this.rbD.TabIndex = 1;
this.rbD.TabStop = true;
this.rbD.Text = "D";
this.rbD.UseVisualStyleBackColor = true;
this.rbD.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
//
// rbN
//
this.rbN.AutoSize = true;
this.rbN.Location = new System.Drawing.Point(14, 13);
this.rbN.Name = "rbN";
this.rbN.Size = new System.Drawing.Size(29, 16);
this.rbN.TabIndex = 0;
this.rbN.TabStop = true;
this.rbN.Text = "N";
this.rbN.UseVisualStyleBackColor = true;
this.rbN.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
//
// fmGUIDGenerator
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(454, 128);
this.Controls.Add(this.gbGUIDFormat);
this.Controls.Add(this.cbAutoCopy);
this.Controls.Add(this.cbAlwaysVisible);
this.Controls.Add(this.cbUppercase);
this.Controls.Add(this.btnCopy);
this.Controls.Add(this.btnGenerator);
this.Controls.Add(this.txtGUID);
this.Name = "fmGUIDGenerator";
this.Text = "GUID生成器";
this.Load += new System.EventHandler(this.fmGUIDGenerator_Load);
this.gbGUIDFormat.ResumeLayout(false);
this.gbGUIDFormat.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox txtGUID;
private System.Windows.Forms.Button btnGenerator;
private System.Windows.Forms.Button btnCopy;
private System.Windows.Forms.CheckBox cbUppercase;
private System.Windows.Forms.CheckBox cbAlwaysVisible;
private System.Windows.Forms.CheckBox cbAutoCopy;
private System.Windows.Forms.GroupBox gbGUIDFormat;
private System.Windows.Forms.RadioButton rbN;
private System.Windows.Forms.RadioButton rbX;
private System.Windows.Forms.RadioButton rbP;
private System.Windows.Forms.RadioButton rbB;
private System.Windows.Forms.RadioButton rbD;
}
}


后台代码:

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;

namespace Eyuan.GUIDGenerator
{
public partial class fmGUIDGenerator : Form
{
#region 字段
string strGUID = string.Empty;//GUID
bool isUppercase = false;//是否大小写
bool isAutoCopy = false;//是否自动复制到剪切板
string strGUIDFormat = string.Empty;//GUID格式
#endregion

public fmGUIDGenerator()
{
InitializeComponent();
txtGUID.ReadOnly = true;
}

/// <summary>
/// 生成GUID
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGenerator_Click(object sender, EventArgs e)
{
strGUID = GenerateGUID();
if (isAutoCopy)
{
Clipboard.SetText(strGUID, TextDataFormat.Text);
}
txtGUID.Text = strGUID;
}

/// <summary>
/// 返回指定格式的的GUID字符串
/// </summary>
/// <returns></returns>
private string GenerateGUID()
{
string sGUID = string.Empty;
//生成指定格式的GUID
sGUID = System.Guid.NewGuid().ToString(strGUIDFormat);
//大小写
if (isUppercase)
{
sGUID = sGUID.ToUpper();
}
return sGUID;
}

#region bool变量设置
private void cbUppercase_CheckedChanged(object sender, EventArgs e)
{
isUppercase = cbUppercase.Checked;
}

private void cbAlwaysVisible_CheckedChanged(object sender, EventArgs e)
{
this.TopMost = cbAlwaysVisible.Checked;
}

private void cbAutoCopy_CheckedChanged(object sender, EventArgs e)
{
isAutoCopy = cbAutoCopy.Checked;
}
#endregion

/// <summary>
/// 手动复制到剪贴板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCopy_Click(object sender, EventArgs e)
{
Clipboard.SetText(strGUID, TextDataFormat.Text);
}

#region GUID格式
private void rb_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = (sender as RadioButton);
if (rb.Checked)
{
strGUIDFormat = rb.Text;
}
}
#endregion

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: