Exception with Drag and Drop

4 posts, 0 answers
  1. Meir Bezalel
    Meir Bezalel avatar
    9 posts
    Member since:
    Dec 2009

    Posted 14 Jan 2013 Link to this post

    I have a problem when dragging a tree node into a panel. In the  Drag Drop event of the panel , I try to add a control to the panel and then I get the following exception :

    System.ComponentModel.Win32Exception was unhandled by user code
      Message=Error creating window handle.
      Source=System.Windows.Forms
      ErrorCode=-2147467259
      NativeErrorCode=1411
      StackTrace:
           at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
           at System.Windows.Forms.Control.CreateHandle()
           at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
           at System.Windows.Forms.Control.CreateControl()
           at System.Windows.Forms.Control.ControlCollection.Add(Control value)
           at TestTreeView.Form1.panel1_DragDrop(Object sender, DragEventArgs e) in E:\Users\Meir\Documents\Visual Studio 2010\Projects\MilestoneTesting\TestTreeView\Form1.cs:line 37
           at System.Windows.Forms.Control.OnDragDrop(DragEventArgs drgevent)
           at System.Windows.Forms.Control.System.Windows.Forms.IDropTarget.OnDragDrop(DragEventArgs drgEvent)
           at System.Windows.Forms.DropTarget.System.Windows.Forms.UnsafeNativeMethods.IOleDropTarget.OleDrop(Object pDataObj, Int32 grfKeyState, Int64 pt, Int32& pdwEffect)
      InnerException: 


    This does not happen to me if I am using Microsoft Tree control. 
    I am using Telerik for Windows Forms , updated to Dec 11, 2012

    Here is the code snippet:
    using System;
    using System.Linq;
     
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    namespace TestTreeView
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                 
                radTreeView1.AddNodeByPath("\\test");
                radTreeView1.ExpandAll();
                radTreeView1.ShowFeedbackForm = false;
            }
     
            private void radTreeView1_DragStarted(object sender, Telerik.WinControls.UI.RadTreeViewDragEventArgs e)
            {
                DoDragDrop(e.Node, DragDropEffects.Move);
            }
     
            private void panel1_DragDrop(object sender, DragEventArgs e)
            {
                Label test = new Label();
                panel1.Controls.Add(test);
            }
     
            private void panel1_DragEnter(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.Move;
            }
     
       
     
        }
    }

    The command "panel1.Controls.Add(test);" generates the exception.
    What am I doing wrong?

    Below is also the  form designer code:
    namespace TestTreeView
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
     
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
     
            #region Windows Form Designer generated code
     
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.radTreeView1 = new Telerik.WinControls.UI.RadTreeView();
                this.panel1 = new System.Windows.Forms.Panel();
                ((System.ComponentModel.ISupportInitialize)(this.radTreeView1)).BeginInit();
                this.SuspendLayout();
                //
                // radTreeView1
                //
                this.radTreeView1.AllowDragDrop = true;
                this.radTreeView1.AllowDrop = true;
                this.radTreeView1.Location = new System.Drawing.Point(31, 35);
                this.radTreeView1.Name = "radTreeView1";
                this.radTreeView1.Size = new System.Drawing.Size(243, 348);
                this.radTreeView1.SpacingBetweenNodes = -1;
                this.radTreeView1.TabIndex = 0;
                this.radTreeView1.Text = "radTreeView1";
                this.radTreeView1.DragStarted += new Telerik.WinControls.UI.RadTreeView.DragStartedHandler(this.radTreeView1_DragStarted);
                //
                // panel1
                //
                this.panel1.AllowDrop = true;
                this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                this.panel1.Location = new System.Drawing.Point(289, 127);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(146, 100);
                this.panel1.TabIndex = 1;
                this.panel1.DragDrop += new System.Windows.Forms.DragEventHandler(this.panel1_DragDrop);
                this.panel1.DragEnter += new System.Windows.Forms.DragEventHandler(this.panel1_DragEnter);
                 
                //
                // Form1
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(475, 395);
                this.Controls.Add(this.panel1);
                this.Controls.Add(this.radTreeView1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.radTreeView1)).EndInit();
                this.ResumeLayout(false);
     
            }
     
            #endregion
     
            private Telerik.WinControls.UI.RadTreeView radTreeView1;
            private System.Windows.Forms.Panel panel1;
        }
    }


    Thanks
    Meir
  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 16 Jan 2013 Link to this post

    Hi Meir,

    I did not manage to reproduce the issue. I suppose that it happens due to mixing the Microsoft OLE Drag and Drop with Telerik Drag and Drop approach. I recommend using the Telerik Drag and Drop approach in cooperation with
    RadPanel instead of Microsoft Panel.

    You should use the following code snippets as sample:
    public partial class TestTreeView : Form
        {
            public TestTreeView()
            {
                InitializeComponent();
     
                this.panel1.PanelElement.AllowDrop = true;
                this.radTreeView1.TreeViewElement.DragDropService.PreviewDragDrop += this.OnPreviewDragDrop;
                this.radTreeView1.TreeViewElement.DragDropService.PreviewDragOver += this.OnPreviewDragOver;
            }
     
            void OnPreviewDragOver(object sender, Telerik.WinControls.RadDragOverEventArgs e)
            {
                RadPanelElement panelElement = e.HitTarget as RadPanelElement;
     
                if (panelElement != null)
                {
                    e.CanDrop = true;
                }
            }
     
            void OnPreviewDragDrop(object sender, Telerik.WinControls.RadDropEventArgs e)
            {
                RadPanelElement panelElement = e.HitTarget as RadPanelElement;
     
                if (panelElement != null)
                {
                    Label test = new Label();
                    test.Text = "Hello";
                    panel1.Controls.Add(test);
                    e.Handled = true;
                }
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
     
                radTreeView1.AddNodeByPath("\\test");
                radTreeView1.ExpandAll();
                //   radTreeView1.ShowFeedbackForm = false;
            }
        }

    partial class TestTreeView
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
     
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
     
            #region Windows Form Designer generated code
     
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.radTreeView1 = new Telerik.WinControls.UI.RadTreeView();
                this.panel1 = new Telerik.WinControls.UI.RadPanel();
                ((System.ComponentModel.ISupportInitialize)(this.radTreeView1)).BeginInit();
                this.SuspendLayout();
                //
                // radTreeView1
                //
                this.radTreeView1.AllowDragDrop = true;
                this.radTreeView1.AllowDrop = true;
                this.radTreeView1.Location = new System.Drawing.Point(31, 35);
                this.radTreeView1.Name = "radTreeView1";
                this.radTreeView1.Size = new System.Drawing.Size(243, 348);
                this.radTreeView1.SpacingBetweenNodes = -1;
                this.radTreeView1.TabIndex = 0;
                this.radTreeView1.Text = "radTreeView1";
                //
                // panel1
                //
                this.panel1.AllowDrop = true;
                this.panel1.Location = new System.Drawing.Point(289, 127);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(146, 100);
                this.panel1.TabIndex = 1;
     
                //
                // Form1
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(475, 395);
                this.Controls.Add(this.panel1);
                this.Controls.Add(this.radTreeView1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.radTreeView1)).EndInit();
                this.ResumeLayout(false);
     
            }
     
            #endregion
     
            private Telerik.WinControls.UI.RadTreeView radTreeView1;
            private Telerik.WinControls.UI.RadPanel panel1;
        }
    All the best,
    Svett
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Meir Bezalel
    Meir Bezalel avatar
    9 posts
    Member since:
    Dec 2009

    Posted 17 Jan 2013 Link to this post

    Hi Svett,

    Thank you for your reply. The solution you provide answers the issue when the panel control is just an empty Telerik panel, but does not solve the problem I am facing. I am trying to Drag & Drop nodes from a tree to a panel that is sometimes is  empty and in other times has a ActiveX. Since the Drag and Drop services of Telerik, only work with Telerik controls, when the panel contains a non-Telerik control such as an ActiveX , this method does not work.
    I was hoping that the RadTreeView will work with the simple Windows drag and drop services instead of the Telerik Drag & drop services , since I am using the Windows D&D services  with the RadGridview control to drag rows to the panel ,  where it does work.  I was expecting the RadTreeView to work just like the RadGridView.


    Thanks 
    Meir
  5. Svett
    Admin
    Svett avatar
    728 posts

    Posted 21 Jan 2013 Link to this post

    Hello Meir,

    You should not mix the Telerik drag and drop and Microsoft OLE Drag and Drop. In your scenario you should use the Microsoft OLE Drag and Drop. You can read more about how you can do that in
    MSDN.

    Kind regards,
    Svett
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Back to Top