我在一个Winform应用程序中使用了一个网格(ComponentOne的FlexGrid),并尝试通过单元格的列索引和值查找该网格中的一个单元格。
我编写了下面的扩展方法来循环遍历网格并找到该单元格。
我正在测试这个方法,对于一个具有6列和64行的网格,我的代码需要10分钟才能找到正确的单元格(位于最后一行)。
有没有什么方法可以加速我的算法?
注意:我还尝试将PlayBack.PlayBackSetting.SmartMatchOption设置为TopLevelWindow,但似乎没有改变任何东西...
谢谢!
我编写了下面的扩展方法来循环遍历网格并找到该单元格。
我正在测试这个方法,对于一个具有6列和64行的网格,我的代码需要10分钟才能找到正确的单元格(位于最后一行)。
有没有什么方法可以加速我的算法?
注意:我还尝试将PlayBack.PlayBackSetting.SmartMatchOption设置为TopLevelWindow,但似乎没有改变任何东西...
谢谢!
public static WinCell FindCellByColumnAndValue(this WinTable table, int colIndex, string strCellValue)
{
int count = table.GetChildren().Count;
for (int rowIndex = 0; rowIndex < count; rowIndex++)
{
WinRow row = new WinRow(table);
WinCell cell = new WinCell(row);
row.SearchProperties.Add(WinRow.PropertyNames.RowIndex, rowIndex.ToString());
cell.SearchProperties.Add(WinCell.PropertyNames.ColumnIndex, colIndex.ToString());
cell.SearchProperties.Add(WinCell.PropertyNames.Value, strCellValue);
if (cell.Exists)
return cell;
}
return new WinCell();
}
编辑
我修改了我的方法,如下所示(例如,我不再使用winrow),这似乎快了约3倍。但是,在一个有3行6列的表格中查找单元格仍需要7秒钟,因此仍然相当慢...
我稍后会将此答案标记为已接受,以便给其他人留出时间提出更好的建议。
public static WinCell FindCellByColumnAndValue(this WinTable table, int colIndex, string strCellValue, bool searchHeader = false)
{
Playback.PlaybackSettings.SmartMatchOptions = Microsoft.VisualStudio.TestTools.UITest.Extension.SmartMatchOptions.None;
int count = table.GetChildren().Count;
for (int rowIndex = 0; rowIndex < count; rowIndex++)
{
WinCell cell = new WinCell(table);
cell.SearchProperties.Add(WinRow.PropertyNames.RowIndex, rowIndex.ToString());
cell.SearchProperties.Add(WinCell.PropertyNames.ColumnIndex, colIndex.ToString());
cell.SearchProperties.Add(WinCell.PropertyNames.Value, strCellValue);
if (cell.Exists)
return cell;
}
return new WinCell();
}
编辑 #2: 根据@Andrii的建议,我尝试使用FindMatchingControls,现在已经接近成功,只是在下面的代码中,单元格的列索引(c.ColumnIndex)有错误的值..
public static WinCell FindCellByColumnAndValue2(this WinTable table, int colIndex, string strCellValue, bool searchHeader = false)
{
WinRow row = new WinRow(table);
//Filter rows containing the wanted value
row.SearchProperties.Add(new PropertyExpression(WinRow.PropertyNames.Value, strCellValue, PropertyExpressionOperator.Contains));
var rows = row.FindMatchingControls();
foreach (var r in rows)
{
WinCell cell = new WinCell(r);
cell.SearchProperties.Add(WinCell.PropertyNames.Value, strCellValue);
//Filter cells with the wanted value in the current row
var controls = cell.FindMatchingControls();
foreach (var ctl in controls)
{
var c = ctl as WinCell;
if (c.ColumnIndex == colIndex)//ERROR: The only cell in my table with the correct value returns a column index of 2, instead of 0 (being in the first cell)
return c;
}
}
return new WinCell();
}