你不需要使用這個正則表達式。 簡單的解決方法是使用LINQ和字符串數組像這樣(在我的事件處理程序例如爲框TextChanged):
string[] blocks = ((TextBox)sender).Text.Split(' ');
for (int idx = 0; idx < clb_CheckedListBox.Items.Count; idx++)
clb_CheckedListBox.SetItemChecked(idx, clb_CheckedListBox.Items[idx].ToString().Split(' ').Intersect(blocks).Any());
你可以用這個下一個代碼運行完全的例子(我不能上傳圖片,所以我完全貼在這裏運行main .cs文件的內容):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Example());
}
}
public partial class Example : Form
{
private System.Windows.Forms.CheckedListBox clb_CheckedListBox;
private System.Windows.Forms.TextBox txb_TextBox;
public Example()
{
this.clb_CheckedListBox = new System.Windows.Forms.CheckedListBox();
this.txb_TextBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.clb_CheckedListBox.FormattingEnabled = true;
this.clb_CheckedListBox.Items.AddRange(new object[] {
"John 1455", "Jenny 786", "Bob 410", "Adam 97", "Jennifer 4100"});
this.clb_CheckedListBox.Location = new System.Drawing.Point(13, 13);
this.clb_CheckedListBox.Name = "clb_CheckedListBox";
this.clb_CheckedListBox.Size = new System.Drawing.Size(168, 139);
this.txb_TextBox.Location = new System.Drawing.Point(187, 13);
this.txb_TextBox.Multiline = true;
this.txb_TextBox.Name = "txb_TextBox";
this.txb_TextBox.Size = new System.Drawing.Size(205, 139);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(404, 166);
this.Controls.Add(this.txb_TextBox);
this.Controls.Add(this.clb_CheckedListBox);
this.ResumeLayout(false);
this.PerformLayout();
this.txb_TextBox.TextChanged += new System.EventHandler(this.txb_TextBox_TextChanged);
this.Load += new System.EventHandler(this.TestForm_Load);
}
private void txb_TextBox_TextChanged(object sender, EventArgs e)
{
string[] blocks = ((TextBox)sender).Text.Split(' ');
for (int idx = 0; idx < clb_CheckedListBox.Items.Count; idx++)
clb_CheckedListBox.SetItemChecked(idx, clb_CheckedListBox.Items[idx].ToString().Split(' ').Intersect(blocks).Any());
}
private void TestForm_Load(object sender, EventArgs e)
{
this.txb_TextBox.Text = "John 410 Jennys 970 4100";
}
}
}
不錯的代碼,現在它出錯了嗎?我做「有點」瞭解你的問題,但我沒有看到你的代碼中的問題。我的意思是代碼可以在這裏和那裏使用一些改進。但請更詳細地描述問題,也許指向代碼中的行。 – Bazzz 2011-04-19 06:47:11
感謝您的建議Buddy – 2011-04-19 12:52:30