创建表格
- 在 UI Editor 中添加表格控件,函数 uifcTableFind()获取表格句柄。
xstring tablename = "table";
uifcTable_ptr table = uifcTableFind(dialogname, tablename);
table->AddActionListener(new MyTableListener());
- SetColumnNameArray()、SetColumnTextArray()、SetRowNameArray()、SetRowTextArray()分别添加行列并设置行列名称
xstringsequence_ptr col_title = xstringsequence::create();
xstringsequence_ptr row_title = xstringsequence::create();
for (int i = 1; i <= col_num; i++)
{
col_title->append(("C" + to_string(i)).c_str());
}
for (int j = 1; j <= row_num; j++)
{
row_title->append(("R" + to_string(j)).c_str());
}
table->SetColumnNameArray(col_title);
table->SetColumnTextArray(col_title);
table->SetRowNameArray(row_title);
table->SetRowTextArray(row_title);
- uifcTableCellFind()和 uifcTableCellLookup()分别根据行列名称和行列索引获取单元格句柄,setText()设置单元格文字。
for (int i = 0; i < col_title->getarraysize(); i++)
{
for (int j = 0; j < row_title->getarraysize(); j++)
{
uifcTableCell_ptr cell = uifcTableCellFind(dialogname, tablename, row_title->get(j), col_title->get(i));
cell->SetText("test");
}
}
- table 中单元格不支持直接修改,可以通过监听器的 OnActivate()函数执行单元格修改函数,实现单元格修改功能。
void ChangeCellText(uifcTable_ptr handle)
{
pfcSession_ptr session = pfcGetProESession();
try
{
xstring text = session->UIReadStringMessage(false);
xstring CellSelected = handle->GetSelectedCellNameArray()->get(0);
uifcTableCellCoordinates_ptr coor = handle->GetAnchorCellName();
uifcTableCell_ptr cell = uifcTableCellFind(handle->GetDialog(), handle->GetComponent(),
coor->GetRow(), coor->GetColumn());
if (!(text.IsNull() || text == xstring("use_default_string")))
cell->SetText(text);
session->UIClearMessage();
}
OTK_EXCEPTION_PRINT_LOG
}
- 监听器函数 OnKeyDown()可以监听按键,通过监听 Delete 键,可以实现删除单元格内容功能
void OnKeyDown(uifcTable_ptr handle)
{
uifcKey_ptr keypress = handle->GetKeyPressed();
try
{
switch (keypress->GetId())
{
case uifcKEY_DELETE:
DeleteCellText(handle);
break;
default:
break;
}
}
OTK_EXCEPTION_PRINT_LOG
}
- GetSelectedCellNameArray()按字面意思是获取单元格名称数组,但是单元格本身没有名字,单元格由行列定位,该函数获取的是单元格对应行列的名字。
void DeleteCellText(uifcTable_ptr handle)
{
xstringsequence_ptr cells = handle->GetSelectedCellNameArray();
try
{
xint num = cells->getarraysize();
if (num > 0)
{
for (int i = 0; i < num / 2; i++)
{
uifcTableCell_ptr cell = uifcTableCellFind(handle->GetDialog(), handle->GetComponent(),
cells->get(2 * i), cells->get(2 * i + 1));
cell->SetText(xstring(""));
}
}
}
OTK_EXCEPTION_PRINT_LOG
}
实例 1.3TableInteraction
源代码:1.3TableInteraction