View previous topic :: View next topic |
Author |
Message |
mouse9
Joined: 26 May 2010 Posts: 2
|
Posted: Wed May 26, 2010 4:23 pm Post subject: How do you enable/disable individual cells? |
|
|
I need to enable and disable individual cells, Windows style (disabled = grayed and doesn't respond) The column is boolean (checkbox). The Editable property of CellWidget has no effect. Below is what I have tried in a class derived from VirtualTree, which sort of works but the checkbox controls disappear in some rows and appear sometimes on a row after the last row. Is there a better/correct way to do this? Thanks
private void EnableCheckBoxColumn(bool enabled)
{
Column column = GetColumnByName("ShowROI");
if (column == null)
return;
int i = 0;
Row row = GetRow(i);
while (row != null)
{
CellWidget cellWidget = GetCellWidget(row, column);
if (cellWidget != null)
{
Control control = cellWidget.EditorControl;
if (control != null)
{
control.Enabled = enabled;
}
}
row = GetRow(++i);
}
}
private void EnableCheckBox(RoiAnalysis roi, bool enabled)
{
Column column = GetColumnByName("ShowROI");
Row row = FindRow(roi);
if (column != null && row != null)
{
CellWidget cellWidget = GetCellWidget(row, column);
if (cellWidget != null)
{
Control control = cellWidget.EditorControl;
if (control != null)
{
control.Enabled = enabled;
}
}
}
} |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Wed May 26, 2010 10:55 pm Post subject: |
|
|
Because VirtualTree is data driven and creates editor controls dynamically the approach you have taken to try and enumerate over the currently visible editor controls is likely to have problems.
The correct way to do this is to handle the InitializeControl event of the CellEditor. Then set the Control enabled to true or false based on some properties of the data in the row (or cell). The code below assumes you have some method "CanEdit" that returns true or false depending on whether the control should be enabled for that data item.
Code: |
void myEditor_InitializeControl(object sender, CellEditorInitializeEventArgs e)
{
object rowItem = e.CellWidget.Row.Item;
e.Control.Enabled = CanEdit(rowItem);
} |
_________________ Infralution Support |
|
Back to top |
|
|
mouse9
Joined: 26 May 2010 Posts: 2
|
Posted: Thu May 27, 2010 2:57 pm Post subject: |
|
|
This works only if the rows affected by changes to the underlying data are not visible in the control (ie scrolled out of visible range). Visible rows do not update their enabled state. I am calling UpdateRows() immediately after changing the underlying data. |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Thu May 27, 2010 10:23 pm Post subject: |
|
|
Yes you are right - I didn't realize you wanted to change the enabled state dynamically. Instead of handling the InitializeControl event you can handle the SetValue event like below:
Code: | private bool _enableCheck = true;
public bool EnableCheck
{
get { return _enableCheck; }
set
{
_enableCheck = value;
this.UpdateRowData();
}
}
private void _checkEditor_SetControlValue(object sender, CellEditorSetValueEventArgs e)
{
CheckBox checkBox = (CheckBox)e.Control;
checkBox.Enabled = EnableCheck;
if (EnableCheck)
{
checkBox.Checked = (bool)e.Value;
}
} |
This examples sets all the controls enabled state to the same value - but you can enable/disable individual cells based on the row data by checking the value of e.CellWidget.Row.Item as in my previous code.
Note that you only have to call UpdateRowData not UpdateRows when the value is changed. _________________ Infralution Support |
|
Back to top |
|
|
|