winForms, threading and dragdrop ?
From the attached code.With AllowDrop = True I get a .NET error as follows: System.InvalidOperationException: DragDrop registration failed. ---> System.Threading.ThreadStateException: The current thread must set to Single Thread Apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. From googling I need to set ApartmentState.STA. But this is readonly. How do I set STA correctly in Python for .NET? Any thoughts appeciated. Guy from CLR import System import CLR.System.Windows.Forms as WinForms from CLR.System.Drawing import Color, Size, Point from CLR.System import ComponentModel,Data, Collections, Threading from CLR.System.Runtime import InteropServices class dataNode(WinForms.TreeNode): def __init__(self,name,total,skill): self.Text = name self.Total = total self.Skill = skill class DnDTree(WinForms.Form): """A WinForms example. """ def __init__(self): self.components = System.ComponentModel.Container() self.treeView1 = WinForms.TreeView() self.listBox1 = WinForms.ListBox() self.SuspendLayout() # Set properties of TreeView 1. self.treeView1.AllowDrop = True self.treeView1.ImageIndex = -1 self.treeView1.Location = System.Drawing.Point(24, 32) self.treeView1.CheckBoxes = True self.treeView1.Name = "treeView1" self.treeView1.Nodes.Clear() self.treeView1.BeginUpdate() for pnode in range(50): self.treeView1.Nodes.Add(dataNode("node%d"%pnode,3,"high")) for cnode in range(15): self.treeView1.Nodes[pnode].Nodes.Add("childnode%d"%cnode) self.treeView1.EndUpdate() #for node in self.treeView1.Nodes: #print node.Total self.treeView1.SelectedImageIndex = -1 self.treeView1.Size = System.Drawing.Size(300, 600) self.treeView1.TabIndex = 0 #self.treeView1.MouseDown += System.Windows.Forms.MouseEventHandler(self.treeView1_MouseDown) #self.treeView1.AfterSelect += System.Windows.Forms.TreeViewEventHandler(self.treeView1_AfterSelect) #self.treeView1.QueryContinueDrag += System.Windows.Forms.QueryContinueDragEventHandler(self.treeView1_QueryContinueDrag) #self.treeView1.DragEnter += System.Windows.Forms.DragEventHandler(self.treeView1_DragEnter) #self.treeView1.ItemDrag += System.Windows.Forms.ItemDragEventHandler(self.treeView1_ItemDrag) # Set properties of ListBox 1. self.listBox1.AllowDrop = True self.listBox1.Location = System.Drawing.Point(24, 650) self.listBox1.Name = "listBox1" self.listBox1.Size = System.Drawing.Size(300, 300) self.listBox1.TabIndex = 1 #self.listBox1.DragDrop += System.Windows.Forms.DragEventHandler(self.listBox1_DragDrop) #self.listBox1.DragEnter += System.Windows.Forms.DragEventHandler(self.listBox1_DragEnter) #Now do Form self.AutoScaleBaseSize = System.Drawing.Size(5,13) self.ClientSize = System.Drawing.Size(400,1024) self.Controls.Add(self.listBox1) self.Controls.Add(self.treeView1) self.ShowInTaskbar = False self.StartPosition = WinForms.FormStartPosition.CenterScreen self.Name = "Form1" self.Text = "Form1" self.ResumeLayout(False) def Dispose(self): self.components.Dispose() WinForms.Form.Dispose(self) def main(): app = DnDTree() WinForms.Application.Run(app) app.Dispose() if __name__ == '__main__': main()
participants (2)
-
Brian Lloyd
-
Guy Robinson