本帖最后由 sys339 于 2009-11-19 20:59:03 编辑

解决方案 »

  1.   

    我修改后还是原有的问题,重新贴一下代码,大家把代码拷贝下来可看到效果,帮忙解决了,谢谢大家捞,会的帮忙解决下,不会的就顶下了。
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;namespace jxzymkbz
    {
        public partial class MkBz : UserControl
        {
           public  int xh=0;
           public Hashtable hashtable = new Hashtable();
            
            public MkBz()
            {
                InitializeComponent();          
            }   
            ///<summary>   
            ///   在指定的容器中的指定位置创建指定类型的控件。   
            ///   </summary>   
            ///   <param   name="abs_control">要创建的控件的相关信息(结构体)</param>   
            ///   <param   name="ac_parentContainer">要在上面创建控件容器控件</param>   
            ///   <param   name="int   ai_positionX">要创建的控件的X坐标</param>   
            ///   <param   name="int   ai_positionY">要创建的控件的Y坐标</param>   
            ///   <param   name="as_msg">错误返回信息</param>   
            public int CreateControl(ButtonStruct abs_control, Control ac_parentContainer, int ai_positionX, int ai_positionY, int xh, out   string as_msg,ref Control newcontrol)
            {
                as_msg = null;
                try
                {
                    string ls_assemblyQualifiedName = null;
                    if (ac_parentContainer is Form)
                    {
                        ls_assemblyQualifiedName = typeof(System.Windows.Forms.Form).AssemblyQualifiedName;
                    }
                    else if (ac_parentContainer is Panel)
                    {
                        ls_assemblyQualifiedName = typeof(System.Windows.Forms.Panel).AssemblyQualifiedName;
                    }
                    else
                    {
                        //as_msg   =   "暂不支持在"   +typeof(ac_parentContainer).FullName.ToString()   +   "上面放置控件";   
                        as_msg = "暂不支持在上面放置控件";
                       return -1;
                    }
                    string assemblyInformation = ls_assemblyQualifiedName.Substring(ls_assemblyQualifiedName.IndexOf(","));
                    Type lt_type = Type.GetType(abs_control.ls_type + assemblyInformation);
                    Control lc_newControl = (Control)System.Activator.CreateInstance(lt_type);
                    ac_parentContainer.SuspendLayout();
                  //lc_newControl.Location = new System.Drawing.Point(ai_positionX, ai_positionY);
                    lc_newControl.Size = new System.Drawing.Size(abs_control.li_width, abs_control.li_height);
                    lc_newControl.Name = lt_type.Name + xh.ToString();
                    lc_newControl.Text = abs_control.ls_text;
                    lc_newControl.Tag = abs_control.ls_tag;
                    ContextMenuStrip lc_control = (ContextMenuStrip)lc_newControl;
                    lc_control.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.删除备注ToolStripMenuItem});                
                    lc_control.TopLevel = false;               ac_parentContainer.Controls.Add(lc_control);             
                    ac_parentContainer.ResumeLayout();
                    newcontrol = lc_newControl;
                }
                catch (Exception ex)
                {
                    as_msg = "创建控件失败,原因为:\n" + ex.Message.ToString();
                    return -1;
                }
                return 0;
            }        ///<summary>   
            ///   在指定的容器中的指定位置创建指定类型的控件。   
            ///   </summary>   
            ///   <param   name="abs_control">要创建的控件的相关信息(结构体)</param>   
            ///   <param   name="ac_parentContainer">要在上面创建控件容器控件</param>   
            ///   <param   name="int   ai_positionX">要创建的控件的X坐标</param>   
            ///   <param   name="int   ai_positionY">要创建的控件的Y坐标</param>   
            ///   <param   name="as_msg">错误返回信息</param>   
            public int CreateControl(ButtonStruct abs_control, Control ac_parentContainer, int ai_positionX, int ai_positionY, int xh, out   string as_msg, ref Control newcontrol,Control oldcontrol)
            {
                as_msg = null;
                try
                {
                    string ls_assemblyQualifiedName = null;
                    if (ac_parentContainer is Form)
                    {
                        ls_assemblyQualifiedName = typeof(System.Windows.Forms.Form).AssemblyQualifiedName;
                    }
                    else if (ac_parentContainer is Panel)
                    {
                        ls_assemblyQualifiedName = typeof(System.Windows.Forms.Panel).AssemblyQualifiedName;
                    }
                    else
                    {
                        //as_msg   =   "暂不支持在"   +typeof(ac_parentContainer).FullName.ToString()   +   "上面放置控件";   
                        as_msg = "暂不支持在上面放置控件";
                        return -1;
                    }
                    string assemblyInformation = ls_assemblyQualifiedName.Substring(ls_assemblyQualifiedName.IndexOf(","));
                    Type lt_type = Type.GetType(abs_control.ls_type + assemblyInformation);
                    Control lc_newControl = (Control)System.Activator.CreateInstance(lt_type);
                    ac_parentContainer.SuspendLayout();
                    lc_newControl.Location = new System.Drawing.Point(ai_positionX, ai_positionY);
                    lc_newControl.Size = new System.Drawing.Size(abs_control.li_width, abs_control.li_height);
                    lc_newControl.Name = lt_type.Name + xh.ToString();
                    lc_newControl.Text = abs_control.ls_text;
                    lc_newControl.Tag = abs_control.ls_tag;
                    lc_newControl.ContextMenuStrip = (ContextMenuStrip)oldcontrol;
                    oldcontrol.SuspendLayout();
                    ac_parentContainer.Controls.Add(lc_newControl);  
                    ac_parentContainer.ResumeLayout();
                    newcontrol = lc_newControl;
                    hashtable.Add(oldcontrol, lc_newControl);
                }
                catch (Exception ex)
                {
                    as_msg = "创建控件失败,原因为:\n" + ex.Message.ToString();
                    return -1;
                }
                return 0;
            }
            /// <summary>
            /// 
            /// </summary>
           public struct ButtonStruct
            {
               public Type ls_type;
               public int li_width;
               public int li_height;
               public string ls_text;
               public object ls_tag;
               public ButtonStruct(Type type,int width,int height,string text,object tag)
               {
                   ls_type = type;
                   li_width = width;
                   li_height = height;
                   ls_text = text;
                   ls_tag = tag;
                }
                       }
           private void contextMenuStrip1_MouseClick(object sender, MouseEventArgs e)
           {
              
               string as_msg = "";
               xh++;
               int positionX=this.contextMenuStrip_tjbz.Left - this.Parent.Left + e.X;
               int positionY=this.contextMenuStrip_tjbz.Top - this.Parent.Top + e.Y - 20;
               //TextBox txt = new TextBox();
               //txt.ContextMenuStrip = this.contextMenuStrip2;           
               //txt.Location = new System.Drawing.Point(positionX,positionY );          
               //this.panel1.Controls.Add(txt);
               object tag = xh;
               //ContextMenuStrip contextmenustrip = null;
               //TextBox textbox = null;
               Control contextmenustrip = null;
               Control textbox = null;
               ButtonStruct abs_control2 = new ButtonStruct(typeof(ContextMenuStrip), 50, 10, "", tag);
               CreateControl(abs_control2, panel1, 0, 0, xh, out as_msg, ref contextmenustrip);
               
             ButtonStruct abs_control = new ButtonStruct(typeof(RichTextBox), 200, 50, "",tag);
            CreateControl(abs_control, panel1, positionX, positionY, xh, out as_msg, ref textbox,contextmenustrip);            
               
                
           }       private void 删除备注ToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
           {          
               ToolStripMenuItem tool = (ToolStripMenuItem)sender;
               ContextMenuStrip contxt=(ContextMenuStrip)tool.GetCurrentParent();
               RichTextBox txt = (RichTextBox)hashtable[contxt];
               MessageBox.Show(txt.Name.ToString());
               contxt.Hide();          
           }         }
    }