C#类向窗体添加控件 [英] C# class to add controls to form

查看:79
本文介绍了C#类向窗体添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个类,该类将在每次单击表单上的按钮时添加一个TabControl和DataGridView.我还必须获取添加到表单中的最后一个TabControl的坐标.我当前的功能将控件添加到表单中 第一次单击时正确无误,但是当我第二次单击按钮时,控件集合被设置回零,因此它不会拾取其他已添加的控件.任何帮助将不胜感激.

I am trying to create a class that will add a TabControl and DataGridView every time a button is clicked on the form. I also have to get the cordinates of the last TabControl that was added to the form. My current functionality adds the controls to the form correctly on the first click, but when I click the button a second time the collection of controls is set back zero, so its not picking up the other controls that were added. Any help would be greatly appreciated.

 

这是我的课程

 

  public partial class CreateControls : Form1
  {

    protected Form1 Form1;
    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();


    //constructor logic
    public CreateControls(Form1 Form1, int controlcnt)
    {

      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.Name = "tabControl_" + controlcnt.ToString();
      //tc.Size = d1.Size;

      //add the TabControl to the form
      Form1.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;
      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);


      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;

    
    }


    public static int GetTabControlYPoint()
    {
      Control.ControlCollection coll = tcTabControl.Controls;
      int cnt = 0;
      int y = 0;
      foreach (Control c in coll)
      {

        if (c is TabControl)
        {
          cnt++;
        }

      }
      if (cnt == 0)
      {
        y = 293;   
      }

      return y;

    }

    void dgView_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
      (sender as DataGridView).BeginEdit(false);
    }


  }

 

这是我在链接按钮上单击创建类的实例的方式

Here's how i create my instance of the class on the linkbutton click

 

    private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      
      controlcnt++;

      CreateControls cc = new CreateControls(this,controlcnt);
}

 

推荐答案

我认为问题在于,当您调用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;
    }


这篇关于C#类向窗体添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆