新建一个Windform 项目。新建一个MainForm窗体:
Mainform核心代码如下:
class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
public string replace(string temp)
{
return temp.Trim().Replace("\r\n", "").Trim();
}
private void MainForm_Load(object sender, EventArgs e)
{
txtp.Text = " int id This is identity;\r\n private string name This is username;string pwd";
txtp.Select(0, 0);
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
if (txtp.Text.Trim().Length<=0)
{
return;
}
txtp.Focus();
txtp.SelectAll();
Clipboard.SetText(txtp.Text);
setStatus("已复制到剪切板!");
}
}
public void setStatus(string status)
{
toolStripStatusLabel1.Text = status;
}
private void txtp_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
return;
}
setStatus("正在输入...");
}
private void toolbar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
if (e.Button==toolBarButton1)
{
string all = txtp.Text.Trim();
if (all.Length <= 0)
{
setStatus("请输入!");
return;
}
//替换n个空格为一个空格
all = System.Text.RegularExpressions.Regex.Replace(all, "\\s+", " ");
string alltext = all.Trim().Replace("private", "");
try
{
string[] ayy = alltext.Split(';');
StringBuilder priv = new StringBuilder("#region 字段\r\n");
StringBuilder pub = new StringBuilder("#region 属性\r\n");
foreach (string s in ayy)
{
if (s.Trim().Length <= 0)
{
continue;
}
string sub0 = s.Trim().Split(' ')[0];
string sub1 = s.Trim().Split(' ')[1];
string sub2 = "";
int index = sub0.Length + sub1.Length + 1;
string r = "private " + replace(sub0) + " _" + replace(sub1) + ";\r\n";
if (s.Trim().Split(' ').Length >= 3)
{
sub2 = s.Trim().Substring(index);
sub2 = "/// <summary>\r\n/// " + sub2 + "\r\n/// </summary>\r\n";
}
string u = sub2 + "public " + replace(sub0) + " " + replace(sub1) + "\r\n" + "{\r\n" + "get{return this._" + replace(sub1) + ";}" + "\r\n" + "set{this._" + replace(sub1) + "=value;}" + "\r\n}" + "\r\n";
pub.Append(u);
priv.Append(r);
}
priv.Append("#endregion");
pub.Append("#endregion");
txtp.Text = priv.ToString() + "\r\n\r\n" + pub.ToString();
setStatus("生成成功,按下ctl+a可直接复制");
}
catch (Exception ex)
{
setStatus("请输入正确的格式!");
}
}
if (e.Button == toolBarButton7)
{
string all = txtp.Text.Trim();
if (all.Length <= 0)
{
setStatus("请输入!");
return;
}
//替换n个空格为一个空格
all = System.Text.RegularExpressions.Regex.Replace(all, "\\s+", " ");
string alltext = all.Trim().Replace("private", "");
try
{
string[] ayy = alltext.Split(';');
StringBuilder pub = new StringBuilder("#region 成员\r\n");
foreach (string s in ayy)
{
if (s.Trim().Length <= 0)
{
continue;
}
string sub0 = s.Trim().Split(' ')[0];
string sub1 = s.Trim().Split(' ')[1];
string sub2 = "";
int index = sub0.Length + sub1.Length + 1;
if (s.Trim().Split(' ').Length >= 3)
{
sub2 = s.Trim().Substring(index);
sub2 = "/// <summary>\r\n/// " + sub2 + "\r\n/// </summary>\r\n";
}
string u = sub2 + "public " + replace(sub0) + " " + replace(sub1) + " { get;set;}" + "\r\n";
pub.Append(u);
}
pub.Append("#endregion");
txtp.Text = pub.ToString();
setStatus("生成成功,按下ctl+a可直接复制");
}
catch (Exception ex)
{
setStatus("请输入正确的格式!");
}
}
else if (e.Button == toolBarButton2)
{
if (txtp.Text.Trim().Length<=0)
{
setStatus("请输入!");
return;
}
txtp.Focus();
txtp.SelectAll();
Clipboard.SetText(txtp.Text);
setStatus("已复制到剪切板!");
}
else if (e.Button==toolBarButton3)
{
if (txtp.Text.Trim().Length <= 0)
{
setStatus("请输入!");
return;
}
txtp.Text = "";
txtp.Focus();
setStatus("清除成功!");
}
} |