Home Forums WinForms controls Other WinForms controls SmartTreeView item removed

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #18427 |

    Hello guys,

    Please tell me how can I know which item was deleted from the Items collection of a SmartTreeView control? The SmartTreeView.OnItemRemoved has as its own parameter an EventArgs.Empty 🙁 ( as opposed to the SmartTreeView OnItemAdded which receives a SmartItemEventArgs param).

    Also I need to catch the moment just after a SmartTreeView.Items.Clear() method is call. I didn’t found any event on protected ‘On…’ method to use.

    The reason I need these two issues is that I have a custom TreeView derived from SmartTreeView, that maintains a private list which has to be cleared out when the Items collection is cleared. Or, if an item is removed, that the item should be removed from this inner list also.

    I would appreciate any help on this,
    Thanks,
    Bogdan

    Imported from legacy forums. Posted by bogdip (had 3804 views)

    Xceed Support
    Member
    Post count: 5658

    For the OnItemRemoved event, if you use pre-determined data, you know which items have been removed. If you remove item using the SelectedItem property to choose which Item will be deleted, the SelectedItem will still be set when the OnItemRemoved event is fire.

    For on event that is fired when the Clear() method is called, since you already use your own custom TreeView, you could add an event and a Clear Method that would fire the event.

    Here is some code that should get you started:

    <code>
    //The custom SmartTreeView
    class MySmartTreeView : SmartTreeView
    {
    public MySmartTreeView()
    : base()
    {

    }

    public void myClearItems()
    {
    this.Items.Clear();
    this.OnItemsClear( new EventArgs() );
    }

    public event EventHandler ItemsCleared;

    public void OnItemsClear( EventArgs e )
    {
    if( ItemsCleared != null )
    {
    ItemsCleared( this, e );
    }
    }
    }

    //The form that uses the custom control (MySmartTreeView)
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private MySmartTreeView smartTreeView1;

    private void Form1_Load( object sender, EventArgs e )
    {
    smartTreeView1 = new MySmartTreeView();
    smartTreeView1.Size = new Size( 523, 429 );
    this.Controls.Add( smartTreeView1 );
    smartTreeView1.Location = new Point( 12, 12 );

    for( int i = 0; i < 10; i++ )
    {
    smartTreeView1.Items.Add( “Item ” + i.ToString() );
    for( int j = 0; j < 5; j++ )
    {
    smartTreeView1.Items[ i ].Items.Add( “Sub Item ” + j.ToString() );
    for( int k = 0; k < 3; k++ )
    {
    smartTreeView1.Items[ i ].Items[ j ].Items.Add( “Sub Sub Item ” + k.ToString() );
    }
    }
    }

    smartTreeView1.ItemRemoved += new EventHandler( smartTreeView1_ItemRemoved );
    smartTreeView1.ItemAdded += new Xceed.SmartUI.SmartItemEventHandler( smartTreeView1_ItemAdded );
    smartTreeView1.ItemsCleared += new EventHandler( smartTreeView1_ItemsCleared );
    }

    void smartTreeView1_ItemsCleared( object sender, EventArgs e )
    {
    //
    }

    void smartTreeView1_ItemAdded( object sender, Xceed.SmartUI.SmartItemEventArgs e )
    {
    //
    }

    void smartTreeView1_ItemRemoved( object sender, EventArgs e )
    {
    //
    }

    private void button1_Click( object sender, EventArgs e )
    {
    if( smartTreeView1.SelectedItem != null )
    {
    if( smartTreeView1.SelectedItem.ParentItem == null )
    smartTreeView1.Items.Remove( smartTreeView1.SelectedItem );
    else
    smartTreeView1.SelectedItem.ParentItem.Items.Remove( smartTreeView1.SelectedItem );
    }
    }

    private void button2_Click( object sender, EventArgs e )
    {
    if( smartTreeView1.SelectedItem != null )
    {
    smartTreeView1.SelectedItem.Items.Add( “My New Item ” + counter.ToString() );
    counter++;
    }
    }

    private static int counter = 0;

    private void button3_Click( object sender, EventArgs e )
    {
    smartTreeView1.myClearItems();
    }
    }
    </code>

    Imported from legacy forums. Posted by CharlesB (had 394 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank,

    About the “myClearItems()” method, yes, this is the way I did it but I hoped this were just a temporarily solution. If some other developers in my project will use this custom tree view control, they will probably use the Items.Clear() method first time (because it’s straightforward) and the issue will persist. So a source of errors is already there.

    About the single item removing, there will be no just the selected item removed. Any item can be a potentially removed item.

    I wanted to encapsulate this functionality into the custom smart tree view, without letting me or who else will be furthermore using this control in the project taking care about synchronizing the items collection with the inner items list.
    By the way, why the OnItemRemoved method doesn’t take a Xceed.SmartUI.SmartItemEventArgs parameter like the OnItemAdded does?

    Thanks,
    Bogdan

    Imported from legacy forums. Posted by bogdip (had 623 views)

    Xceed Support
    Member
    Post count: 5658

    I will add those 2 comments as 2 feature requests. For the moment, we do not have a time frame to offer as of when (or if) these behaviors will be modified. If they are, we will notify you as soon as it is available.

    Imported from legacy forums. Posted by CharlesB (had 4974 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.