1. 程式人生 > >對集合控制元件(ListBox,ComboBox,TreeView,RichTextBox,DataGridView)的查詢、替換操作控制元件

對集合控制元件(ListBox,ComboBox,TreeView,RichTextBox,DataGridView)的查詢、替換操作控制元件

一、程式入口:

using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;
namespace HR.Control
{
/// <summary>
/// 對集合類控制元件中的文字進行查詢、替換的元件
/// </summary>
public class SearchSelect
{
/// <summary>
/// 查詢檔案的操作型別
/// </summary>
public enum FindOption
{
/// <summary>
/// 查詢文字
/// </summary>
FindText,
/// <summary>
/// 替換文字
/// </summary>
ReplaceText
}


/// <summary>
/// 查詢ListBox控制元件
/// </summary>
/// <param name="lb">ListBox</param>
/// <param name="option">查詢型別</param>
/// <remarks></remarks>
public void FindAndSelect(ListBox lb, FindOption option)
{
LoadForm(lb, option, "ListBox");
}

/// <summary>
/// 查詢ComboBox控制元件
/// </summary>
/// <param name="cbb">ComboBox</param>
/// <param name="option">查詢型別</param>
/// <remarks></remarks>
public void FindAndSelect(ComboBox cbb, FindOption option)
{
LoadForm(cbb, option, "ComboBox");
}


/// <summary>
/// 查詢TreeView控制元件
/// </summary>
/// <param name="tv">TreeView</param>
/// <param name="option">查詢型別</param>
/// <remarks></remarks>
public void FindAndSelect(TreeView tv, FindOption option)
{
LoadForm(tv, option, "TreeView");
}

/// <summary>
/// 查詢RichTextBox控制元件
/// </summary>
/// <param name="rtb">RichTextBox</param>
/// <param name="option">查詢型別</param>
/// <remarks></remarks>
public void FindAndSelect(RichTextBox rtb, FindOption option)
{
LoadForm(rtb, option, "RichTextBox");
}


/// <summary>
/// 查詢DataGridView控制元件
/// </summary>
/// <param name="dgv">DataGridView</param>
/// <param name="option">查詢型別</param>
/// <remarks></remarks>
public void FindAndSelect(DataGridView dgv, FindOption option)
{
LoadForm(dgv, option, "DataGridView");
}

/// <summary>
/// 載入窗體,根據不同的控制元件
/// </summary>
/// <param name="control">要查詢的控制元件</param>
/// <param name="option">查詢型別</param>
/// <param name="controlType">控制元件型別</param>
private static void LoadForm(object control, FindOption option,string controlType)
{
HR.Control.Form1_Search fs = new Form1_Search();
fs.FindControl = control;
fs.ControlType = controlType;
if (option == FindOption.FindText)//查詢文字
{
fs.tabControl1.SelectedTab = fs.tabFind;
}
else
{
fs.tabControl1.SelectedTab = fs.tabReplace;
}
fs.ShowDialog();
}
}
}


二、入口程式呼叫的窗體

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HR.Control
{
public partial class Form1_Search : Form
{
/// <summary>
/// 要查詢的控制元件
/// </summary>
public object FindControl;
/// <summary>
/// 查詢的控制元件型別
/// </summary>
public string ControlType;

/// <summary>
///選擇的索引
/// </summary>
int selIndex;

/// <summary>
/// 選擇的行
/// </summary>
int selR;

/// <summary>
/// 選擇的列
/// </summary>
int selC;

/// <summary>
/// 開始索引
/// </summary>
int sI;
/// <summary>
/// 結束索引
/// </summary>
int eI;
public Form1_Search()
{
InitializeComponent();
}

private void Form1_Search_Load(object sender, EventArgs e)
{
switch (ControlType)
{
case"ListBox":
this.btnColor.Enabled = false;
this.checkBox4.Enabled = false;
ListBox lb = (ListBox)FindControl;
if (lb.Items.Count==0)
{
MessageBox.Show("沒有可查詢的項,查詢將終止!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
if (lb.SelectionMode==SelectionMode.One)
{
this.btnFindAll.Enabled = false;
};
break;


case"ComboBox":
this.btnColor.Enabled = false;
this.checkBox4.Enabled = false;
ComboBox cbb = (ComboBox)FindControl;
if (cbb.Items.Count==0)
{
MessageBox.Show("沒有可查詢的項,查詢將終止!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
};
this.btnFindAll.Enabled = false;
break;

case"TreeView":
this.btnColor.Enabled = false;
this.checkBox4.Enabled = false;
TreeView tv = (TreeView)FindControl;
if (tv.Nodes.Count==0)
{
MessageBox.Show("沒有可查詢的項,查詢將終止!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
this.btnfindNext.Enabled = false;
break;

case"RichTextBox":
RichTextBox rtb = (RichTextBox)FindControl;
if (string.IsNullOrEmpty(rtb.Text))
{
MessageBox.Show("沒有可查詢的項,查詢將終止!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
this.ckbsbcordbc.Enabled = false;
this.chkTpf.Enabled = false;
break;

case"DataGridView":
DataGridView dgv = (DataGridView)FindControl;
if (dgv.Rows.Count == 0)
{
MessageBox.Show("沒有可查詢的項,查詢將終止!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
};
break;

}
}


private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}

/// <summary>
/// 對ListBox的查詢、替換操作
/// </summary>
/// <param name="lb"></param>
/// <param name="lx"></param>
private void FindListBox(ListBox lb, string lx)
{
if (lx == "Next")
{
if (lb.SelectedIndex == -1)
{
selIndex = 0;
}
else
{

if (selIndex == 0)
{
selIndex = lb.SelectedIndex;
}
}

string tempFindText =null;
string tempDesc = null;
int i;
if (this.tabControl1.SelectedTab.Text == "查詢")
{
tempFindText = this.txtFindText.Text;
}
else
{
tempFindText = this.txtFindTextB.Text;
}
//從選中的索引位置開始找
for (i = selIndex; i < lb.Items.Count; i++)
{
tempDesc = lb.Items[i].ToString();
if (this.ckbsbcordbc.Checked == false) //不區分全形和半形
{
HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
tempDesc = SO.SBCToDBC(lb.Items[i].ToString());
tempFindText = SO.SBCToDBC(tempFindText);
}
if (this.ckbUpporLow.Checked == false) //不區分大小寫
{
tempDesc = tempDesc.ToLower();
tempFindText = tempFindText.ToLower();
}
if (this.chkTpf.Checked == true) //使用萬用字元
{
if (tempDesc.IndexOf(tempFindText) > -1) //有匹配項
{
if (this.tabControl1.SelectedTab.Text == "替換")
{

lb.Items[i] = tempDesc.Replace(tempFindText,this.txtFindReplaceText.Text);
}
lb.SelectedIndex = i;
selIndex = i + 1;
break;
}
}
else //不使用萬用字元
{
if (tempDesc.IndexOf(tempFindText) == 0)
{
if (this.tabControl1.SelectedTab.Text == "替換")
{
lb.Items[i] = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
}
lb.SelectedIndex = i;
selIndex = i + 1;
break;
}
}
}

//已經查詢到最後一項
if (i == lb.Items.Count)
{
lb.SelectedIndex = -1;
}
}
else //全部查詢
{
string tempFindText =null;
string tempDesc = null;
int i = 0;
if (this.tabControl1.SelectedTab.Text == "查詢")
{
tempFindText = this.txtFindText.Text;
}
else
{
tempFindText = this.txtFindTextB.Text;
}
for (i = 0; i <lb.Items.Count; i++)
{
if (this.ckbsbcordbc.Checked == false)
{
HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
tempDesc = SO.SBCToDBC(lb.Items[i].ToString());
tempFindText = SO.SBCToDBC(tempFindText);
}
if (this.ckbUpporLow.Checked == false)
{
tempFindText = tempFindText.ToLower();
tempDesc = tempDesc.ToLower();
}
if (this.chkTpf.Checked == true)
{
if (this.tabControl1.SelectedTab.Text == "替換")
{
if (tempDesc.IndexOf(tempFindText) > -1)
{
lb.Items[i] = tempDesc.Replace(this.txtFindTextB.Text, this.txtFindReplaceText.Text);
}
lb.SelectedIndex = i;
}
}
else
{
if (tempDesc.IndexOf(tempFindText) == 0)
{
if (this.tabControl1.SelectedTab.Text == "替換")
{
lb.Items[i] = tempDesc.Replace(this.txtFindTextB.Text, this.txtFindReplaceText.Text);
}
lb.SelectedIndex = i;
}
}
}
}
}


/// <summary>
/// 對ComboBox控制元件的查詢、替換操作
/// </summary>
/// <param name="cmm"></param>
/// <param name="lx"></param>
private void FindComboBox(ComboBox cmm,string lx)
{
string tempDesc = null;
string tempFindText = null;

if (cmm.SelectedIndex==-1)
{
selIndex = 0;
}
if (this.tabControl1.SelectedTab.Text == "替換")
{
tempFindText = this.txtFindTextB.Text;
}
else
{
tempFindText = this.txtFindText.Text;
}
int i;
for (i = selIndex; i < cmm.Items.Count; i++)
{
tempDesc = cmm.Items[i].ToString();
if (this.ckbsbcordbc.Checked==false)
{
HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
tempDesc = SO.SBCToDBC(tempDesc);
tempFindText = SO.SBCToDBC(tempFindText);
}
if (this.ckbUpporLow.Checked==false)
{
tempDesc = tempDesc.ToLower();
tempFindText = tempFindText.ToLower();
}
if (this.chkTpf.Checked == true) //使用萬用字元
{
if (tempDesc.IndexOf(tempFindText) > -1)
{
if (this.tabControl1.SelectedTab.Text == "替換")
{
cmm.Items[i] = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
}
cmm.SelectedIndex = i;
selIndex = selIndex + 1;
break;
}
}
else //使用萬用字元
{
if (tempDesc.IndexOf(tempFindText)==0)
{
if (this.tabControl1.SelectedTab.Text == "替換")
{

cmm.Items[i] = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
}
cmm.SelectedIndex = i;
selIndex = selIndex + 1;
break;

}
}
}

if (i==cmm.Items.Count)
{
cmm.SelectedIndex = -1;
}
}


/// <summary>
/// 對RichTextBox控制元件的查詢、替換操作
/// </summary>
/// <param name="rct"></param>
/// <param name="lx"></param>
private void FindRichTextBox(RichTextBox rct, string lx)
{

eI = rct.Text.Length;
int i = 0;
string tempFindText = null;
if (sI==0)
{
sI = 0;
}

try
{
if (lx == "Next")
{
if (this.tabControl1.SelectedTab.Text == "替換")
{
tempFindText = this.txtFindTextB.Text;
if (this.ckbUpporLow.Checked == true) //區分大小寫
{
i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.MatchCase);
}
else
{
i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.None);
}
if (i > 0)//找到了匹配項
{
rct.SelectedText = this.txtFindReplaceText.Text;
rct.SelectionStart = i;
rct.SelectionLength = txtFindReplaceText.Text.Length;//從新定位一個新的位置開始找
sI = i + txtFindReplaceText.Text.Length;
}

}
else //查詢
{
tempFindText = this.txtFindText.Text;
if (this.ckbUpporLow.Checked == true) //區分大小寫
{
i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.MatchCase);
}
else
{
i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.None);
}
sI = i + tempFindText.Length;

}
}
else //查詢全部
{
sI = 0;
eI = rct.Text.Length;
if (this.tabControl1.SelectedTab.Text == "替換")
{
tempFindText = this.txtFindTextB.Text;
int k = 0;
for (k = 0; k < rct.Text.Length; k++)
{
if (this.ckbUpporLow.Checked == true)
{
i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.MatchCase);
}
else
{
i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.None);
}
if (i > 0)
{
rct.SelectedText = this.txtFindReplaceText.Text;
rct.SelectionStart = i;
rct.SelectionLength = txtFindReplaceText.Text.Length;//從替換後的新位置開始找
sI = i + txtFindReplaceText.Text.Length;
}
else
{
return;
}
}
}
else //如果是查詢
{
rct.SelectionColor = Color.Black;
tempFindText = this.txtFindText.Text;
int k = 0;
for (k = 0; k < rct.Text.Length; k++)
{
if (this.ckbUpporLow.Checked == true)
{
i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.MatchCase);
}
else
{
i = rct.Find(tempFindText, sI, eI, RichTextBoxFinds.None);
}
if (i > 0)
{
sI = i + tempFindText.Length;
rct.SelectionColor = this.btnColor.BackColor;
}
else
{
return;
}
}
}
}
}
catch
{
sI = 0;
}

}

/// <summary>
/// 對TreeView的查詢、替換操作
/// </summary>
/// <param name="tv">TreeView控制元件</param>
/// <param name="lx"></param>
/// <remarks></remarks>
private void FindTreeView(TreeView tv, string lx)
{
foreach (TreeNode FirstNode in tv.Nodes)
{
RecurseTreeNode(FirstNode);
}
}

/// <summary>
/// 遞迴查詢樹節點
/// </summary>
/// <param name="tempNode">樹節點</param>
private void RecurseTreeNode(TreeNode tempNode)
{
tempNode.ForeColor = Color.Black;
tempNode.Expand();
string tempFindText = "";
string tempDesc = "";
if (this.tabControl1.SelectedTab.Text == "替換")
{
tempFindText = this.txtFindTextB.Text;
}
else
{
tempFindText = this.txtFindText.Text;
}

tempDesc = tempNode.Text;
if (this.ckbsbcordbc.Checked==false)
{
HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
tempDesc = SO.SBCToDBC(tempDesc);
tempFindText = SO.SBCToDBC(tempFindText);
}
if (this.ckbUpporLow.Checked==false)
{
tempDesc = tempDesc.ToLower();
tempFindText = tempFindText.ToLower();
}
if (this.chkTpf.Checked == true)
{
if (tempDesc.IndexOf(tempFindText) > -1)
{
tempNode.ForeColor = this.btnColor.BackColor;
if (this.tabControl1.SelectedTab.Text == "替換")
{
tempNode.Text = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
}
if (tempNode.Parent != null) //當前節點有父節點時,展開父節點
{
tempNode.Parent.Expand();
}
}
}
else
{
if (tempDesc.IndexOf(tempFindText)==0)
{
tempNode.ForeColor = this.btnColor.BackColor;
if (this.tabControl1.SelectedTab.Text == "替換 ")
{
tempNode.Text = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
}
if (tempNode.Parent != null) //當前節點有父節點時,展開父節點
{
tempNode.Parent.Expand();
}
}
}

foreach (TreeNode aNode in tempNode.Nodes)
{
RecurseTreeNode(aNode);
}
}


/// <summary>
/// 對DataGridView的查詢、替換操作
/// </summary>
/// <param name="dgv"></param>
/// <param name="lx"></param>
private void FindDataGridView(DataGridView dgv, string lx)
{
int i = 0;
int j = 0;
string tempDesc = null;
string tempFindText = null;
if (this.tabControl1.SelectedTab.Text == "替換")
{
tempFindText= this.txtFindText.Text;
}
else
{
tempFindText = this.txtFindTextB.Text;
}

if (lx == "Next")
{
for (i = selR; i < dgv.Rows.Count; i++)
{
for (j = selC; j < dgv.ColumnCount; j++)
{
selC = j + 1;

dgv.Rows[i].Cells[j].Style.ForeColor = Color.Black;

if (!Object.ReferenceEquals(dgv.Rows[i].Cells[j].Value, null)) //單元格不為空
{
tempDesc = dgv.Rows[i].Cells[j].Value.ToString();

if (this.ckbsbcordbc.Checked == false)
{
HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
tempDesc = SO.SBCToDBC(tempDesc);
tempFindText = SO.SBCToDBC(tempFindText);
}
if (this.ckbUpporLow.Checked == false)
{
tempDesc = tempDesc.ToLower();
tempFindText = tempFindText.ToLower();
}
if (this.chkTpf.Checked == true)
{
if (tempDesc.IndexOf(tempFindText) > -1)
{
if (this.tabControl1.SelectedTab.Text == "替換")
{
dgv.Rows[i].Cells[j].Value = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
}
dgv.Rows[i].Cells[j].Selected = true;
dgv.Rows[i].Cells[j].Style.ForeColor = btnColor.BackColor;
dgv.FirstDisplayedCell = dgv.Rows[i].Cells[j];//設定查詢到的單元格,一定出現在可視區域
return;
}
}
else
{
if (tempDesc.IndexOf(tempFindText) == 0)
{
if (this.tabControl1.SelectedTab.Text == "替換")
{
dgv.Rows[i].Cells[j].Value = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
}
dgv.Rows[i].Cells[j].Selected = true;
dgv.Rows[i].Cells[j].Style.ForeColor = btnColor.BackColor;
dgv.FirstDisplayedCell = dgv.Rows[i].Cells[j];//設定查詢到的單元格,一定出現在可視區域
return;
}
}

}
}
//當一行查詢完後
if (selC == dgv.ColumnCount)
{
selR = selR + 1;
selC = 0;
}
}
//所有行查詢完後
if (selR == dgv.Rows.Count)
{
selR = 0;
selC = 0;
}
}
else //查詢全部
{
for (i = selR; i < dgv.Rows.Count; i++)
{
for (j = selC; j < dgv.ColumnCount; j++)
{
dgv.Rows[i].Cells[j].Style.ForeColor = Color.Black;

if (!Object.ReferenceEquals(dgv.Rows[i].Cells[j].Value, DBNull.Value)) //單元格不為空
{
tempDesc = dgv.Rows[i].Cells[j].Value.ToString();

if (this.ckbsbcordbc.Checked == false) //不區分全形半形
{
HR.StringOperate.StringOperate.ConvertDBCAndSBC SO = new HR.StringOperate.StringOperate.ConvertDBCAndSBC();
tempDesc = SO.SBCToDBC(tempDesc);
tempFindText = SO.SBCToDBC(tempFindText);
}
if (this.ckbUpporLow.Checked == false)//不區分大小寫
{
tempDesc = tempDesc.ToLower();
tempFindText = tempFindText.ToLower();
}
if (this.chkTpf.Checked == true)
{
if (tempDesc.IndexOf(tempFindText) > -1)
{
if (this.tabControl1.SelectedTab.Text == "替換")
{
dgv.Rows[i].Cells[j].Value = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
}
dgv.Rows[i].Cells[j].Selected = true;
dgv.Rows[i].Cells[j].Style.ForeColor = btnColor.BackColor;
dgv.FirstDisplayedCell = dgv.Rows[i].Cells[j];//設定查詢到的單元格,一定出現在可視區域
}
}
else
{
if (tempDesc.IndexOf(tempFindText) == 0)
{
if (this.tabControl1.SelectedTab.Text == "替換")
{
dgv.Rows[i].Cells[j].Value = tempDesc.Replace(tempFindText, this.txtFindReplaceText.Text);
}
dgv.Rows[i].Cells[j].Selected = true;
dgv.Rows[i].Cells[j].Style.ForeColor = btnColor.BackColor;
dgv.FirstDisplayedCell = dgv.Rows[i].Cells[j];//設定查詢到的單元格,一定出現在可視區域
}
}

}
}
}
}

}
private void btnfindNext_Click(object sender, EventArgs e)
{
if (this.tabControl1.SelectedTab.Text == "查詢" && string.IsNullOrEmpty(this.txtFindText.Text))
{
MessageBox.Show("請先輸入要查詢的內容,再進行查詢!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if(this.tabControl1.SelectedTab.Text == "替換" && string.IsNullOrEmpty(this.txtFindTextB.Text))
{
MessageBox.Show("請先輸入要查詢的內容,再進行替換!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

switch (ControlType)
{
case"ListBox":
FindListBox((ListBox)FindControl, "Next");
break;

case"ComboBox":
FindComboBox((ComboBox)FindControl, "Next");
break;

case "RichTextBox":
FindRichTextBox((RichTextBox)FindControl, "Next");
break;

case "TreeView":
FindTreeView((TreeView)FindControl, "Next");
break;

case "DataGridView":
FindDataGridView((DataGridView)FindControl, "Next");
break;
}
}

private void btnFindAll_Click(object sender, EventArgs e)
{
if (this.tabControl1.SelectedTab.Text == "查詢" && string.IsNullOrEmpty(this.txtFindText.Text))
{
MessageBox.Show("請先輸入要查詢的內容,再進行查詢!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if (this.tabControl1.SelectedTab.Text == "替換" && string.IsNullOrEmpty(this.txtFindTextB.Text))
{
MessageBox.Show("請先輸入要查詢的內容,再進行替換!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

switch (ControlType)
{
case "ListBox":
FindListBox((ListBox)FindControl, "All");
break;

case "ComboBox":
FindComboBox((ComboBox)FindControl, "All");
break;

case "RichTextBox":
FindRichTextBox((RichTextBox)FindControl, "All");
break;

case "TreeView":
FindTreeView((TreeView)FindControl, "All");
break;

case "DataGridView":
FindDataGridView((DataGridView)FindControl, "All");
break;
}
}

private void btnColor_Click(object sender, EventArgs e)
{
ColorDialog cd= new ColorDialog();
if (cd.ShowDialog()==DialogResult.OK)
{
this.btnColor.BackColor = cd.Color;
}
if (this.tabControl1.SelectedTab.Text == "查詢")
{
this.txtFindText.Focus();

}
else
{
this.txtFindTextB.Focus();
}
}

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (object.ReferenceEquals(this.tabControl1.SelectedTab,this.tabFind))
{
this.btnfindNext.Text = "查詢下一處(&N)";
this.btnFindAll.Text = "查詢全部(&A)";
this.txtFindText.Focus();
}
else
{
this.btnfindNext.Text = "替換下一處(&N)";
this.btnFindAll.Text = "替換全部(&A)";
this.txtFindTextB.Focus();
}
}
}
}

查詢、替換窗體
[img]http://dl2.iteye.com/upload/attachment/0085/7856/55b0ccf2-8e82-35c1-86e8-dca2151450c4.jpg[/img]