將前面的Form1.cs代碼改成:
修改了前面的第44行(nameSpace-->namespace)和第53行
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;
using System.IO;
using System.Text.RegularExpressions ;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string ClassName1 = textBox1.Text.Trim();
string classexp1 = txtexp.Text.Trim();
string NameSpace1 = txtNameSapace.Text.Trim();
if (ClassName1.Length == 0)
{
MessageBox.Show("類名不能爲空");
return;
}
sfdFile.FileName = ClassName1;
if (sfdFile.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(sfdFile.FileName, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
if (radyou.Checked && txtNameSapace.Text.Trim() != null)
{
sw.WriteLine("namespace " + NameSpace1);
sw.WriteLine("{");
}
sw.WriteLine(" public class " + ClassName1);
sw.WriteLine(" {");
foreach (DataGridViewRow Row in fdcontent.Rows)
{
if (Row.Cells[0].Value != null )
{
string propname = Row.Cells[0].Value.ToString();
string type = Row.Cells[1].Value.ToString();
//替換propname前一個或多個下劃線,中間下劃線不替換
sw.WriteLine(" private " + type + " " + propname + ";");
string propname1 = Regex.Replace(propname, "^_+", "");
//把propname首字母變爲大寫
string functionname = propname1.Substring(0, 1).ToUpper() + propname1.Substring(1);
sw.WriteLine(" public " + type + " " + functionname);
sw.WriteLine(" {");
sw.WriteLine(" get { return " + propname + "; }");
sw.WriteLine(" set { " + propname + " = value; }");
sw.WriteLine(" }");
}
}
sw.WriteLine("}");
if (radyou.Checked && txtNameSapace.Text.Trim() != null)
{
sw.WriteLine("}");
}
sw.Close();
fs.Close();
MessageBox.Show("實體類創建成功!");
}
}
private void radwu_CheckedChanged(object sender, EventArgs e)
{
txtNameSapace.Visible = false;
}
private void radyou_CheckedChanged(object sender, EventArgs e)
{
txtNameSapace.Visible = true;
}
}
}