我正在使用brighttideassoftware的treelistview,并希望在我的树列表中选择一个特定的孩子。
我的对象类型是'MeasurementFile'。
public class MeasurementFile
{
private string fileName = string.Empty;
private List<SignalObjects> signals = new List<SignalObjects>();
...
}
子元素的类型是'SelectObjects':
public class SignalObjects
{
private string signalName = string.Empty;
public SignalObjects(string signalName)
{
this.signalName = signalName;
}
public string SignalName { get { return signalName; } }
}
所以每个MeasurementFile都有一个信号列表。
我的treelistview看起来像:
MyWinForm:
name/signal //Column
-myFile1
|_____signal1
|_____signal2
|_____signal3
-myFile2
-myFile3
-myFile4
现在我想搜索一个特定的孩子。如果扩展了MeasurementFileobject,并且我在searchtextit中输入了一些内容,那么所有匹配的子对象都应该被选中。如果我在搜索栏中输入"al1",则名称为"signal1"的子节点应该被选中。
我的问题是:我如何选择匹配的孩子?
I tried…
private void searchSignalTextEdit_TextChanged(object sender, EventArgs e)
{
string text = searchSignalTextEdit.Text;
if (!String.IsNullOrEmpty(text))
{
foreach (MeasurementFile measurementFile in treeListView1.Objects)
{
if (treeListView1.IsExpanded(measurementFile))
{
foreach (SignalObjects signal in measurementFile.getSignals)
{
if (signal.SignalName.Contains(text))
{
treeListView1.SelectObject(signal);
}
}
}
}
}
}
…但是没有人被选中。如果有人能帮助我,我将很高兴:)
使用linq和正则表达式例如:
Regex regex = new Regex(...);
var signals = MeasurementFile.Signals.Select(x=>x.signalName).Asenumebrable;
var matchingsignals = signals.Where(x => regex.IsMatch(x)).ToList();
var matching = MeasurementFile.Signals.Where(d => matchingsignals.Contains(d.signalName));