using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new 简单通讯录());
}
}
}
------------------简单通讯录.Designer.cs--------------------------------
namespace WindowsFormsApplication3
{
partial class 简单通讯录
{
/// <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.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.flowLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Controls.Add(this.button2);
this.flowLayoutPanel1.Controls.Add(this.button3);
this.flowLayoutPanel1.Controls.Add(this.button1);
this.flowLayoutPanel1.Controls.Add(this.button4);
this.flowLayoutPanel1.Location = new System.Drawing.Point(74, 23);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(133, 130);
this.flowLayoutPanel1.TabIndex = 0;
//
// button2
//
this.button2.Location = new System.Drawing.Point(3, 3);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(129, 25);
this.button2.TabIndex = 1;
this.button2.Text = "管理通信录";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(3, 34);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(129, 25);
this.button3.TabIndex = 2;
this.button3.Text = "快速查找";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(3, 65);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(129, 25);
this.button1.TabIndex = 0;
this.button1.Text = "添加联系人";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(3, 96);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(129, 25);
this.button4.TabIndex = 3;
this.button4.Text = "退出";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// 简单通讯录
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.flowLayoutPanel1);
this.Name = "简单通讯录";
this.Text = "简单通讯录";
this.flowLayoutPanel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
}
}
------------------------------简单通讯录.cs---------------------------
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;
namespace WindowsFormsApplication3
{
public partial class 简单通讯录 : Form
{
public 简单通讯录()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{ //点击退出按钮系统提示是否退出通讯录系统
DialogResult diares;
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
MessageBoxDefaultButton defbut = MessageBoxDefaultButton.Button1;
MessageBoxIcon icon = MessageBoxIcon.Question;
string ask = "你是否要退出通讯录?";
string caption = "@温馨提示@";
diares = MessageBox.Show(this, ask, caption, buttons, icon, defbut);
if (diares == DialogResult.Yes)
{
this.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
new 添加联系人().Show();
}
private void button2_Click(object sender, EventArgs e)
{
new ManageForm().Show();
}
private void button3_Click(object sender, EventArgs e)
{
new 快速查找().Show();
}
}
}
------------------------管理通讯录.Designer.cs---------------------------------
namespace WindowsFormsApplication3
{
partial class ManageForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.column1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.column2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.column3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.column4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.deletebutton = new System.Windows.Forms.Button();
this.alterbutton = new System.Windows.Forms.Button();
this.savebutton = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.textbox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textbox4 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textbox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.textbox1 = new System.Windows.Forms.TextBox();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// listView1
//
this.listView1.BackColor = System.Drawing.SystemColors.ScrollBar;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.column1,
this.column2,
this.column3,
this.column4});
this.listView1.ForeColor = System.Drawing.SystemColors.InfoText;
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(12, 12);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(327, 238);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// column1
//
this.column1.Text = "姓名";
//
// column2
//
this.column2.Text = "性别";
//
// column3
//
this.column3.Text = "电话";
this.column3.Width = 90;
//
// column4
//
this.column4.Text = "住址";
this.column4.Width = 120;
//
// deletebutton
//
this.deletebutton.Location = new System.Drawing.Point(345, 43);
this.deletebutton.Name = "deletebutton";
this.deletebutton.Size = new System.Drawing.Size(75, 23);
this.deletebutton.TabIndex = 1;
this.deletebutton.Text = "删除";
this.deletebutton.UseVisualStyleBackColor = true;
this.deletebutton.Click += new System.EventHandler(this.deletebutton_Click);
//
// alterbutton
//
this.alterbutton.Location = new System.Drawing.Point(345, 131);
this.alterbutton.Name = "alterbutton";
this.alterbutton.Size = new System.Drawing.Size(75, 23);
this.alterbutton.TabIndex = 2;
this.alterbutton.Text = "修改";
this.alterbutton.UseVisualStyleBackColor = true;
this.alterbutton.Click += new System.EventHandler(this.alterbutton_Click);
//
// savebutton
//
this.savebutton.Location = new System.Drawing.Point(345, 227);
this.savebutton.Name = "savebutton";
this.savebutton.Size = new System.Drawing.Size(75, 23);
this.savebutton.TabIndex = 3;
this.savebutton.Text = "保存";
this.savebutton.UseVisualStyleBackColor = true;
this.savebutton.Click += new System.EventHandler(this.savebutton_Click);
//
// panel1
//
this.panel1.AutoSize = true;
this.panel1.Controls.Add(this.textbox3);
this.panel1.Controls.Add(this.label4);
this.panel1.Controls.Add(this.textbox4);
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(this.textbox2);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.button4);
this.panel1.Controls.Add(this.textbox1);
this.panel1.Location = new System.Drawing.Point(12, 256);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(379, 88);
this.panel1.TabIndex = 7;
this.panel1.Visible = false;
//
// textbox3
//
this.textbox3.Location = new System.Drawing.Point(263, 0);
this.textbox3.Name = "textbox3";
this.textbox3.Size = new System.Drawing.Size(100, 21);
this.textbox3.TabIndex = 18;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label4.Location = new System.Drawing.Point(191, 33);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(66, 19);
this.label4.TabIndex = 17;
this.label4.Text = "住址:";
//
// textbox4
//
this.textbox4.Location = new System.Drawing.Point(263, 31);
this.textbox4.Name = "textbox4";
this.textbox4.Size = new System.Drawing.Size(100, 21);
this.textbox4.TabIndex = 16;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label3.Location = new System.Drawing.Point(191, 2);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(66, 19);
this.label3.TabIndex = 15;
this.label3.Text = "电话:";
//
// textbox2
//
this.textbox2.Location = new System.Drawing.Point(75, 35);
this.textbox2.Name = "textbox2";
this.textbox2.Size = new System.Drawing.Size(100, 21);
this.textbox2.TabIndex = 12;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.Location = new System.Drawing.Point(3, 35);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(66, 19);
this.label2.TabIndex = 11;
this.label2.Text = "性别:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(3, 2);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(66, 19);
this.label1.TabIndex = 10;
this.label1.Text = "姓名:";
//
// button4
//
this.button4.Location = new System.Drawing.Point(155, 62);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 5;
this.button4.Text = "确认修改";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// textbox1
//
this.textbox1.Location = new System.Drawing.Point(75, 0);
this.textbox1.Name = "textbox1";
this.textbox1.Size = new System.Drawing.Size(100, 21);
this.textbox1.TabIndex = 3;
//
// ManageForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(450, 353);
this.Controls.Add(this.panel1);
this.Controls.Add(this.savebutton);
this.Controls.Add(this.alterbutton);
this.Controls.Add(this.deletebutton);
this.Controls.Add(this.listView1);
this.Name = "ManageForm";
this.Text = "管理通讯录";
this.Load += new System.EventHandler(this.管理通讯录_Load);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader column1;
private System.Windows.Forms.ColumnHeader column2;
private System.Windows.Forms.ColumnHeader column3;
private System.Windows.Forms.ColumnHeader column4;
private System.Windows.Forms.Button deletebutton;
private System.Windows.Forms.Button alterbutton;
private System.Windows.Forms.Button savebutton;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox textbox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textbox4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textbox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.TextBox textbox1;
}
}--------------------------管理通讯录.cs--------------------------------------
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 WindowsFormsApplication3
{
public partial class ManageForm : Form
{
private Boolean ischanged = false;
//定义ischanged变量来表示通讯录是否被修改,用户每次点击删除或者确定修改按钮后,ischanged=true,
//用户点击每次保存后,ischanged=false,这样在关闭窗口时,触发关闭窗口事件后
//系统会判断ischanged的值,如果ischanged=true说明用户有未保存的修改,系统会提示用户是否保存修改
public ManageForm()
{
InitializeComponent();
listView1.ColumnClick += new ColumnClickEventHandler(listView1_ColumnClick);//注册排序事件
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.ManageForm_FormClosing);
//注册关闭窗口事件
}
//点击列名更改排序排序方式,默认是不排序
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (listView1.Sorting == SortOrder.Ascending)
{
listView1.Sorting = SortOrder.Descending;
}
else
{
listView1.Sorting = SortOrder.Ascending;
}
}
private void AddlistViewItem(String[] a)//向列表中添加项的方法
{
ListViewItem item = new ListViewItem(a);
listView1.Items.Add(item);
}
private void 管理通讯录_Load(object sender, EventArgs e)
{
//读取通讯录.txt中的每一行保存到a[]中
String[] a = File.ReadAllLines(@"通讯录.txt", Encoding.Default);
for (int i = 0; i < a.Length; i++)
{
string[] ax = a[i].Split(new String[] { "*&$" }, StringSplitOptions.None);
//分割通讯录的一行
AddlistViewItem(ax);//调用方法,添加每一行到表格中
}
}
private void deletebutton_Click(object sender, EventArgs e)//删除方法,删除选中的1个或多个列表项
{
ischanged = true;
for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem item = listView1.SelectedItems[i];
listView1.Items.Remove(item);
}
}
public void save()//这个方法用户保存,它会新建一个与原文件同名的文件将原文件覆盖,实现对原文件的复写
{
FileStream fs = new FileStream(@"通讯录.txt", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
for (int i = 0; i < this.listView1.Items.Count; i++)
{
sw.WriteLine(string.Format("{0}*&${1}*&${2}*&${3}", listView1.Items[i].SubItems[0].Text,
listView1.Items[i].SubItems[1].Text, listView1.Items[i].SubItems[2].Text,
listView1.Items[i].SubItems[3].Text));
}
sw.Close();
fs.Close();
}
//下面是保存方法,能将编辑后的列表信息保存到通讯录中并给出提示
private void savebutton_Click(object sender, EventArgs e)
{
ischanged = false;
save();
MessageBox.Show("保存成功");
}
//下面方法实现这样的功能:点击修改按钮,系统会将修改组件在显示与隐藏直接切换
private void alterbutton_Click(object sender, EventArgs e)
{
if (panel1.Visible == true)
{
panel1.Visible = false;
} else
{
panel1.Visible = true;
}
}
private void button4_Click(object sender, EventArgs e)
{
ischanged = true;
if (listView1.SelectedItems.Count >= 1)
{
ListViewItem item = listView1.SelectedItems[listView1.SelectedItems.Count - 1];
if (!textbox1.Text.Contains("*&$") && !textbox2.Text.Contains("*&$") &&
!textbox3.Text.Contains("*&$") && !textbox4.Text.Contains("*&$"))
{
if (textbox1.Text != "")//用户输入的姓名不能为空
{
if (Regex.IsMatch(textbox3.Text, "^[0-9]*$"))
{
item.SubItems[0].Text = textbox1.Text;
item.SubItems[1].Text = textbox2.Text;
item.SubItems[2].Text = textbox3.Text;
item.SubItems[3].Text = textbox4.Text;
MessageBox.Show("修改成功");
textbox1.Clear();
textbox2.Clear();
textbox3.Clear();
textbox4.Clear();
}
else MessageBox.Show("输入的电话号码只能为数字");
}
else MessageBox.Show("用户输入姓名不能为空");
}
else MessageBox.Show("修改失败,字符“*&$”是系统定义的特殊字符,请用户不要使用");
}
}
private void ManageForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (ischanged)
{
DialogResult result = MessageBox.Show("发现您有未保存的修改,是否要将修改应用到通讯录",
"提示信息", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
if (result == DialogResult.Cancel)
{
e.Cancel = true;
}
else if (result == DialogResult.No)
{
e.Cancel = false;
}
else
{
save();
e.Cancel = false;
}
}
else { e.Cancel = false; }
}
}
}
----------------------------------快速查找.Designer.cs------------------------
namespace WindowsFormsApplication3
{
partial class 快速查找
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.searchbutton = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.listView2 = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.SuspendLayout();
//
// searchbutton
//
this.searchbutton.Location = new System.Drawing.Point(175, 12);
this.searchbutton.Name = "searchbutton";
this.searchbutton.Size = new System.Drawing.Size(75, 23);
this.searchbutton.TabIndex = 0;
this.searchbutton.Text = "快速搜索";
this.searchbutton.UseVisualStyleBackColor = true;
this.searchbutton.Click += new System.EventHandler(this.searchbutton_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(2, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(133, 21);
this.textBox1.TabIndex = 1;
//
// listView2
//
this.listView2.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.listView2.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4});
this.listView2.FullRowSelect = true;
this.listView2.GridLines = true;
this.listView2.Location = new System.Drawing.Point(2, 56);
this.listView2.Name = "listView2";
this.listView2.Size = new System.Drawing.Size(309, 221);
this.listView2.TabIndex = 3;
this.listView2.UseCompatibleStateImageBehavior = false;
this.listView2.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "姓名";
//
// columnHeader2
//
this.columnHeader2.Text = "性别";
//
// columnHeader3
//
this.columnHeader3.Text = "电话";
this.columnHeader3.Width = 88;
//
// columnHeader4
//
this.columnHeader4.Text = "住址";
this.columnHeader4.Width = 120;
//
// 快速查找
//
this.AcceptButton = this.searchbutton;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(311, 272);
this.Controls.Add(this.listView2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.searchbutton);
this.Name = "快速查找";
this.Text = "快速查找";
this.Load += new System.EventHandler(this.快速查找_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button searchbutton;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListView listView2;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
}
}
-----------------------------------快速查找.cs---------------------------------
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;
namespace WindowsFormsApplication3
{
public partial class 快速查找 : Form
{
public 快速查找()
{
InitializeComponent();
}
private void searchbutton_Click(object sender, EventArgs e)
{
ListViewItem foundItem=listView2.FindItemWithText(textBox1.Text,false,0,true);
if(foundItem!=null){
listView2.TopItem=foundItem;
}
else{
MessageBox.Show("没有找到信息");
textBox1.Text = "";
}
}
private void AddlistViewItem(String[] a)//向列表中添加项的方法
{
ListViewItem item = new ListViewItem(a);
listView2.Items.Add(item);
}
private void 快速查找_Load(object sender, EventArgs e)
{
//读取通讯录.txt中的每一行保存到a[]中
String[] a = File.ReadAllLines(@"通讯录.txt", Encoding.Default);
for (int i = 0; i < a.Length; i++)
{
string[] ax = a[i].Split(new String[] { "*&$" }, StringSplitOptions.None);
//分割通讯录的一行
AddlistViewItem(ax);//调用方法,添加每一行到表格中
}
}
}
}
------------------------添加联系人.Designer.cs------------------------------
namespace WindowsFormsApplication3
{
partial class 添加联系人
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.textbox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textbox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.textbox4 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textbox1 = new System.Windows.Forms.TextBox();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.textbox3);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.label4);
this.panel1.Controls.Add(this.textbox2);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.textbox4);
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(this.textbox1);
this.panel1.Location = new System.Drawing.Point(37, 21);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(197, 169);
this.panel1.TabIndex = 4;
//
// textbox3
//
this.textbox3.Location = new System.Drawing.Point(75, 67);
this.textbox3.Name = "textbox3";
this.textbox3.Size = new System.Drawing.Size(100, 21);
this.textbox3.TabIndex = 14;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label4.Location = new System.Drawing.Point(3, 100);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(66, 19);
this.label4.TabIndex = 13;
this.label4.Text = "住址:";
//
// textbox2
//
this.textbox2.Location = new System.Drawing.Point(75, 35);
this.textbox2.Name = "textbox2";
this.textbox2.Size = new System.Drawing.Size(100, 21);
this.textbox2.TabIndex = 12;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.Location = new System.Drawing.Point(3, 35);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(66, 19);
this.label2.TabIndex = 11;
this.label2.Text = "性别:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(3, 2);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(66, 19);
this.label1.TabIndex = 10;
this.label1.Text = "姓名:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(59, 143);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 5;
this.button1.Text = "确定";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textbox4
//
this.textbox4.Location = new System.Drawing.Point(75, 100);
this.textbox4.Name = "textbox4";
this.textbox4.Size = new System.Drawing.Size(100, 21);
this.textbox4.TabIndex = 9;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label3.Location = new System.Drawing.Point(3, 69);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(66, 19);
this.label3.TabIndex = 5;
this.label3.Text = "电话:";
//
// textbox1
//
this.textbox1.Location = new System.Drawing.Point(75, 0);
this.textbox1.Name = "textbox1";
this.textbox1.Size = new System.Drawing.Size(100, 21);
this.textbox1.TabIndex = 3;
//
// 添加联系人
//
this.AcceptButton = this.button1;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(272, 217);
this.Controls.Add(this.panel1);
this.Name = "添加联系人";
this.Text = "添加联系人";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox textbox4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textbox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textbox2;
private System.Windows.Forms.TextBox textbox3;
private System.Windows.Forms.Label label4;
}
}
------------------------添加联系人.cs-----------------------------------------
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 WindowsFormsApplication3
{
public partial class 添加联系人 : Form
{
public 添加联系人()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String s1 = textbox1.Text;
String s2 = textbox2.Text;
String s3 = textbox3.Text;
String s4 = textbox4.Text;
//这里将字符“*&$”作为分隔符号,,如果用户的输入包括了这个字符,系统会提示输入
//不合法并跳过添加,让用户修改自己的输入
if (!s1.Contains("*&$") && !s2.Contains("*&$") && !s3.Contains("*&$") && !s4.Contains("*&$"))
{
if (s1 != "")//用户输入的姓名不能为空
{
if (Regex.IsMatch(s3, "^[0-9]*$")) //使用正则表达式来判断用户输入的电话号码是否为纯数字
{
String s1a = s1.Trim();
String s2a = s2.Trim();
String s3a = s3.Trim();
String s4a = s4.Trim();
s1 = s1a + "*&$";
s2 = s2a + "*&$";
s3 = s3a + "*&$";
s4 = s4a + Environment.NewLine;
String path = @"通讯录.txt"; //这里使用了绝对路径
File.AppendAllText(path, s1 + s2 + s3 + s4, Encoding.Default);
MessageBox.Show("添加成功");
textbox1.Clear();
textbox2.Clear();
textbox3.Clear();
textbox4.Clear();
}
else MessageBox.Show("输入的电话号码只能为数字");
}
else MessageBox.Show("用户输入姓名不能为空");
}
else MessageBox.Show("添加失败,字符“*&$”是系统定义的特殊字符,请用户不要使用");
}
}
}