且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#类向窗体添加控件

更新时间:2022-06-02 07:13:55

我认为问题在于,当您调用CreateControls构造函数时,您会得到一种新的表单和一个新的控件集合.

I think the problem is that when you invoke the CreateControls constructor, you're getting a new form and a new Controls collection.

我对您的代码进行了一些调整,因此它更符合您的需求.我拔出了CreateControl类,并将所有逻辑放入Form1实现中.

I tweaked your code a bit, so it's more in the direction of what you're looking for. I pulled out the CreateControl class and put all the logic into the Form1 implementation.   


  public partial class Form1 : Form
  {
    static int controlcnt = 0;

    //private DataGridView dgView = new DataGridView();
    //private DataGridViewTextBoxColumn ValueColumn = new DataGridViewTextBoxColumn();
    //private DataGridViewCheckBoxColumn CheckColumn = new DataGridViewCheckBoxColumn();
    //public static TabControl tcTabControl = new TabControl();
    //private TabPage tpTabPage = new TabPage();


    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      this.CreateControls(++controlcnt);
    }

    public void CreateControls(int controlcnt)
    {
      DataGridView dgView = new DataGridView();
      DataGridViewTextBoxColumn ValueColumn = new DataGridViewTextBoxColumn();
      DataGridViewCheckBoxColumn CheckColumn = new DataGridViewCheckBoxColumn();
      TabControl tcTabControl = new TabControl();
      TabPage tpTabPage = new TabPage();


      //this.Form1 = Form1;
      //SetCreateControlCnt();
      //String strSever = Form1.cbServer.Text.ToString();
      //String strDB = Form1.cbDB.SelectedItem.ToString();
      //String strSproc = Form1.cbSproc.SelectedItem.ToString();

      //dgView.DataSource = SqlHelper.GetSprocParameters(strSever, strDB, strSproc).Tables[0];
      dgView.Name = "dgView_" + controlcnt.ToString();

      ValueColumn.HeaderText = "Value";

      CheckColumn.HeaderText = "Pass Null Value";


      dgView.Location = new Point(0, 0);
      dgView.AllowUserToAddRows = false;
      dgView.AllowUserToDeleteRows = false;
      dgView.AllowUserToOrderColumns = false;
      dgView.AutoSize = true;
      //dgView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
      dgView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
      dgView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
      dgView.RowHeadersVisible = false;
      dgView.BorderStyle = BorderStyle.None;
      dgView.BackgroundColor = SystemColors.Window;
      dgView.Margin = new Padding(0, 0, 0, 0);
      dgView.CellEnter += new DataGridViewCellEventHandler(dgView_CellEnter);


      //TabControl tcTabControl = new TabControl();
      //tcTabControl.Location = new System.Drawing.Point(12, GetTabControlYPoint());
      tcTabControl.Location = new System.Drawing.Point( 10 * controlcnt, 10 * controlcnt );
      tcTabControl.Name = "tabControl_" + controlcnt.ToString();
      //tc.Size = d1.Size;

      //add the TabControl to the form
      this.Controls.Add(tcTabControl);


      //TabPage tpTabPage = new TabPage();
      tpTabPage.AutoScroll = true;
      tpTabPage.Location = new System.Drawing.Point(4, 22);
      tpTabPage.Name = "tabPage_" + controlcnt.ToString();
      tpTabPage.Padding = new System.Windows.Forms.Padding(3);
      tpTabPage.BackColor = SystemColors.Window;
      //tp.Size = d1.Size;
      tpTabPage.Text = "strSproc"; // strSproc;
      tpTabPage.UseVisualStyleBackColor = true;

      //add DataGridView to the Tabpage
      tpTabPage.Controls.Add(dgView);

      //show the DataGridView
      //dgView.Show();


      //add the tab page to the tabcontrol
      tcTabControl.TabPages.Add(tpTabPage);
      tcTabControl.Visible = true;


      dgView.Columns.Add(CheckColumn);
      dgView.Columns.Add(ValueColumn);

      int i = 0;
      foreach (DataGridViewColumn dgvc in dgView.Columns)
      {

        //the checkbox and value columns should not be read only
        if ((i != (dgView.Columns.Count - 1)) && (i != (dgView.Columns.Count - 2)))
        {
          dgView.Columns[i].ReadOnly = true;
        }

        if (i == dgView.Columns.Count - 3) //precision column
        {
          dgView.Columns[i].Visible = false;
        }



        if (i == 0) //parameter column
        {
          dgView.Columns[i].Width = 200;
        }
        else if (i == 4) //value column
        {
          dgView.Columns[i].Width = 300;
        }
        else
        {
          dgView.Columns[i].Width = 100;
        }

        dgvc.SortMode = DataGridViewColumnSortMode.NotSortable;

        i++;
      }

      int j = 0;
      String strDataType = null;
      String strDataTypeLength = null;

      foreach (DataGridViewRow dgvr in dgView.Rows)
      {
        // dataGridView1.Rows[n].Cells[0].Value
        strDataType = dgView.Rows[j].Cells[1].Value.ToString();
        strDataTypeLength = dgView.Rows[j].Cells[2].Value.ToString();

        if (strDataType.Contains("bigint") || strDataType.Contains("int"))
        {
          dgView.Rows[j].Cells[1].Value = strDataType;
        }
        else
        {
          dgView.Rows[j].Cells[1].Value = strDataType + "(" + strDataTypeLength + ")";
        }

        j++;
      }


      //resize the TabPage
      //tpTabPage.Size = dgView.Size;
      tpTabPage.Height = dgView.Height + 30;
      tpTabPage.Width = dgView.Width + 30;

      //resize the TabControl
      //tcTabControl.Size = dgView.Size;
      tcTabControl.Height = dgView.Height + 30;
      tcTabControl.Width = dgView.Width + 30;


      //tcTabControl.l
      //int x = tcTabControl.Location.X;
      //int y = tcTabControl.Location.Y;
      //int height = tcTabControl.Height;
      //int controlpadding = 25;
    }