View previous topic :: View next topic |
Author |
Message |
mwe
Joined: 08 Dec 2005 Posts: 15
|
Posted: Tue Oct 03, 2006 10:32 pm Post subject: Trying to visualize selected cell |
|
|
I'm trying to visualize selected cell. I'm using VirtualTree 3.1.2. The way VirtualTree selection visualization works is by marking the FullRow or the MainCellText. I'd like to visualize to the user the cell that has been selected by for example making it sunken.
I went about implementing a CellClick handler and had my first surprise. In the CellClick handler I followed the advice given here http://www.infralution.com/phpBB2/viewtopic.php?t=487&highlight=cell+click. Now I had a nasty surprise finding that SelectedItem on the tree is not equal to Row.Item from the CellWidget. SelectedItem is right whereas Row.Item is of by 1 or 2 depending on what row I'm clicking (I'm using the tree to display hierarchical data). This is very confusing for the user because another cell than the one clicked is visualized as selected.
What do I do to get it right?
Also, is it safe to hold on to the row and column to be able to identify the selected cell in GetCellData handler (to apply the sunken style if the cell has been scrolled out of sight) with respect to columns being reordered by the user or filters applied to the rows or new rows being added for example.
Some example code
Code: |
private void virtualTree1_CellClick(object sender, EventArgs e)
{
Point pos = this.PointToClient(Control.MousePosition);
Widget widget = virtualTree1.GetWidget(pos.X, pos.Y);
if (widget is CellWidget)
{
if (previousCellWidget != null)
{
// Reset the previously selected cell to its normal looks
previousCellWidget.CellData.EvenStyle = StyleFactory.NormalStyle(previousCellWidget.CellData.EvenStyle);
previousCellWidget.CellData.OddStyle = StyleFactory.NormalStyle(previousCellWidget.CellData.OddStyle);
}
CellWidget cellWidget = widget as CellWidget;
Row row = (widget as CellWidget).Row;
Column column = (widget as CellWidget).Column;
cellWidget.CellData.EvenStyle = StyleFactory.SunkenStyle(cellWidget.CellData.EvenStyle);
cellWidget.CellData.OddStyle = StyleFactory.SunkenStyle(cellWidget.CellData.OddStyle);
previousCellWidget = cellWidget;
virtualTree1.Refresh();
}
}
|
_________________ Kind regards
Magnus Werner, System Architect
------------------------------------------
Saab Training Systems AB |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Tue Oct 03, 2006 11:37 pm Post subject: |
|
|
Quote: | Now I had a nasty surprise finding that SelectedItem on the tree is not equal to Row.Item from the CellWidget. SelectedItem is right whereas Row.Item is of by 1 or 2 depending on what row I'm clicking (I'm using the tree to display hierarchical data). This is very confusing for the user because another cell than the one clicked is visualized as selected.
What do I do to get it right? |
The problem is the line of code that converts the mouse coordinates to control coordinates - you are converting to the forms coordinates rather than the tree controls coordinates. This line should be:
Code: | Point pos = virtualTree1.PointToClient(Control.MousePosition); |
This isn't the best way to implement this type of functionality however. The CellData is for a widget can potentially be overwritten when the tree is rebuilt - which would mean the changes you made for your selected cell would dissappear. Another problems it that when a row is selected the VirtualTree.SelectedStyle is used to display the row (and cells in the row) rather than cell style. This would mean your style is not used anyway.
Have a look at the Custom Widget sample project. This illustrates the correct way to do this type of customization. In your case you would create a custom CellWidget class and override the GetSelectedStyle method to return the correct style for the selected cell.
Quote: | Also, is it safe to hold on to the row and column to be able to identify the selected cell in GetCellData handler (to apply the sunken style if the cell has been scrolled out of sight) with respect to columns being reordered by the user or filters applied to the rows or new rows being added for example. |
You can hold onto the column without any problems. You should not however hold onto row references. This is because the Row objects may be disposed of Virtual Tree when they are no longer required to support the current display. Instead you should hold onto the Row.Item object and check against it. _________________ Infralution Support |
|
Back to top |
|
|
mwe
Joined: 08 Dec 2005 Posts: 15
|
Posted: Wed Oct 04, 2006 7:00 am Post subject: |
|
|
Thanks !
Yes it works much better when using virtualTree1.PointToClient. And thanks for pointing me to the Custom Widget sample. I'll have a look at it and try slove it that way instead. _________________ Kind regards
Magnus Werner, System Architect
------------------------------------------
Saab Training Systems AB |
|
Back to top |
|
|
|