📄 datar.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace RegexTool
{
/// <summary>
/// DataR 的摘要说明。
/// </summary>
public class DataR : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public System.Windows.Forms.ListBox lbData;
private System.Windows.Forms.Button btnView;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnRefresh;
public Form1 form1; //对主窗体进行引用,以便将数据传到主窗体的文本框内
public DataR()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DataR));
this.lbData = new System.Windows.Forms.ListBox();
this.btnView = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.btnRefresh = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lbData
//
this.lbData.BackColor = System.Drawing.SystemColors.Control;
this.lbData.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbData.HorizontalExtent = 300;
this.lbData.HorizontalScrollbar = true;
this.lbData.ItemHeight = 12;
this.lbData.Location = new System.Drawing.Point(0, 0);
this.lbData.Name = "lbData";
this.lbData.ScrollAlwaysVisible = true;
this.lbData.Size = new System.Drawing.Size(304, 338);
this.lbData.TabIndex = 0;
//
// btnView
//
this.btnView.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnView.Location = new System.Drawing.Point(312, 24);
this.btnView.Name = "btnView";
this.btnView.TabIndex = 2;
this.btnView.Text = "查看";
this.btnView.Click += new System.EventHandler(this.button2_Click);
//
// btnAdd
//
this.btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnAdd.Location = new System.Drawing.Point(312, 72);
this.btnAdd.Name = "btnAdd";
this.btnAdd.TabIndex = 3;
this.btnAdd.Text = "添加";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnDelete
//
this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnDelete.Location = new System.Drawing.Point(312, 120);
this.btnDelete.Name = "btnDelete";
this.btnDelete.TabIndex = 4;
this.btnDelete.Text = "删除";
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnRefresh
//
this.btnRefresh.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnRefresh.Location = new System.Drawing.Point(312, 192);
this.btnRefresh.Name = "btnRefresh";
this.btnRefresh.TabIndex = 5;
this.btnRefresh.Text = "刷新";
this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
//
// DataR
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(392, 341);
this.Controls.Add(this.btnRefresh);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnView);
this.Controls.Add(this.lbData);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "DataR";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "典型正则表达式";
this.Load += new System.EventHandler(this.DataR_Load);
this.ResumeLayout(false);
}
#endregion
//查看选中的正则表达式并在主窗体中进行验证
private void button2_Click(object sender, System.EventArgs e)
{
int i = lbData.SelectedIndex;
try
{
if (i!=-1)
{
DataSet ds = new DataSet();
ds.ReadXml("Data.xml");
form1.txtRegex.Text = ds.Tables[0].Rows[i][1].ToString();
form1.txtText.Text = ds.Tables[0].Rows[i][2].ToString();
this.Close();
}
else
{
MessageBox.Show("你必须选中其中的一项才能查看!","没有选中项!");
}
}
catch (Exception exception)
{
MessageBox.Show( exception.Message.ToString() );
}
}
//添加数据事件
private void btnAdd_Click(object sender, System.EventArgs e)
{
InsertForm insert = new InsertForm();
insert.ShowDialog();
}
//从XML文件加载数据
private void DataR_Load(object sender, System.EventArgs e)
{
LoadData(); //加载数据
}
//删除某一项正则表达式
private void btnDelete_Click(object sender, System.EventArgs e)
{
try
{
DataSet ds = new DataSet();
ds.ReadXml("Data.xml");
int i = lbData.SelectedIndex;
if ( i != -1 )
{
ds.Tables[0].Rows[i].Delete();
ds.WriteXml("Data.xml");
lbData.Items.RemoveAt(i);
}
else
{
MessageBox.Show("您只有选中一项后才能进行删除操作!","没有选中项");
}
}
catch(Exception eee)
{
MessageBox.Show( eee.Message.ToString() );
}
}
//刷新lbData内的正则表达式内容
private void btnRefresh_Click(object sender, System.EventArgs e)
{
lbData.Items.Clear(); //清空
LoadData(); //重新加载
}
//从Data.xml文件加载数据到lbData控件上。
private void LoadData()
{
try
{
DataSet ds = new DataSet();
ds.ReadXml("Data.xml");
lbData.ColumnWidth = 450;
for ( int i=0; i<ds.Tables[0].Rows.Count; i++ )
{
lbData.Items.Add(ds.Tables[0].Rows[i][0].ToString()+" : "+ds.Tables[0].Rows[i][1].ToString());
}
}
catch
{
MessageBox.Show("找不到数据文件!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -