日期:2014-05-17  浏览次数:20946 次

c#窗体中初始化tabcontrol出现dragdrop注册失败,求大神
我设置窗体内一个tabcontrol的AllowDrop=true
然后就报这个错

System.InvalidOperationException: DragDrop 注册失败。 ---> System.Threading.ThreadStateException: 要进行 OLE 调用,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数在其上已标有 STAThreadAttribute。
   at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)
   --- 内部异常堆栈跟踪的结尾 ---
   at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)
   at System.Windows.Forms.Control.OnHandleCreated(EventArgs e)
   at System.Windows.Forms.TreeView.OnHandleCreated(EventArgs e)
   at System.Windows.Forms.Control.WmCreate(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.TreeView.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)





我这个窗口是一个类里面show的
没有Main函数
c#

------解决方案--------------------
检查Program.cs的Main方法前是否有sta属性:
[STAThread]
static void Main() { 。。 }

如果是自已建的线程,启动前设个标志:
thread.SetApartmentState(ApartmentState.STA);
thread.Start();