创建新表单时,文本并非全部显示

时间:2013-08-21 03:39:34

标签: c# winforms

我使用c#构建了一个程序,我想创建一个新的表单(如microsoft word,excel等),我已经可以实现它了,但是,当我命令创建时,文本并不是全部都显示出来的一种新形式。

以下是创建新表单之前的图片: enter image description here

这是创建新表单后的图像: enter image description here

我想知道为什么在创建新表单后文本和菜单“文件”没有显示?我已经调用了这个包含所有文本和文本框的函数,但只包含了出来的文本框:

private void AddObjects(object sender, EventArgs e, Form theForm)
{
  textBoxQuantityContainer = new List<NumericUpDown>();
  textBoxCodeContainer = new List<TextBox>();
  textBoxDescContainer = new List<TextBox>();
  textBoxSubTotalContainer = new List<TextBox>();
  textBoxTotalContainer = new List<TextBox>();
  textBoxAllTotalContainer = new TextBox();

  OleDbDataReader dReader;
  OleDbConnection conn = new OleDbConnection(connectionString);
  conn.Open();
  OleDbCommand cmd = new OleDbCommand("SELECT [Code] FROM [Data]", conn);

  dReader = cmd.ExecuteReader();

  AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection();

  while (dReader.Read())
  {
    string numString = dReader[0].ToString().PadLeft(4, '0');
    codesCollection.Add(numString);
  }

  dReader.Close();
  conn.Close();

  if (firstForm.comboBox1.SelectedIndex == 0)
  {
    label1.Text = "Code:";
    label1.Location = new Point(60, 125);
    label2.Text = "Welcome to the Selling System.";
    label2.Location = new Point(600, 30);
    label3.Text = "Quantity:";
    label3.Location = new Point(155, 125);
    label4.Text = "Description:";
    label4.Location = new Point(580, 125);
    label5.Text = "Sub Total on Rp:";
    label5.Location = new Point(1020, 125);
    label6.Text = "Total on Rp:";
    label6.Location = new Point(1210, 125);
    label7.Text = "Total on Rp:";
    label7.Location = new Point(1080, 580);
  }

  else if (firstForm.comboBox1.SelectedIndex == 1)
  {
    label1.Text = "Kode:";
    label1.Location = new Point(60, 125);
    label2.Text = "Selamat datang di Selling System.";
    label2.Location = new Point(600, 30);
    label3.Text = "Banyaknya:";
    label3.Location = new Point(145, 125);
    label4.Text = "Keterangan:";
    label4.Location = new Point(580, 125);
    label5.Text = "Sub Total di Rp:";
    label5.Location = new Point(1020, 125);
    label6.Text = "Total di Rp:";
    label6.Location = new Point(1210, 125);
    label7.Text = "Total di Rp:";
    label7.Location = new Point(1080, 580);
  }

  //****TextBox for Code****
  for (int y = 0; y <= 16; y++)
  {
    textBoxCodeContainer.Add(new TextBox());
    textBoxCodeContainer[y].Size = new Size(100, 50);
    textBoxCodeContainer[y].Location = new Point(25, 150 + (y * 25));
    textBoxCodeContainer[y].TextChanged += new System.EventHandler(this.textBox_TextChanged);

    textBoxCodeContainer[y].AutoCompleteMode = AutoCompleteMode.Suggest;
    textBoxCodeContainer[y].AutoCompleteSource = AutoCompleteSource.CustomSource;
    textBoxCodeContainer[y].AutoCompleteCustomSource = codesCollection;

    theForm.Controls.Add(textBoxCodeContainer[y]);
  }

  //****TextBox for Quantity****
  for (int y = 0; y <= 16; y++)
  {
    textBoxQuantityContainer.Add(new NumericUpDown());
    textBoxQuantityContainer[y].Size = new Size(100, 50);
    textBoxQuantityContainer[y].Location = new Point(125, 150 + (y * 25));
    textBoxQuantityContainer[y].TextChanged += new System.EventHandler(this.textBox_TextChanged);
    textBoxQuantityContainer[y].Maximum = 1000;

    theForm.Controls.Add(textBoxQuantityContainer[y]);
  }

  //****TextBox for Description****
  for (int y = 0; y <= 16; y++)
  {
    textBoxDescContainer.Add(new TextBox());
    textBoxDescContainer[y].Size = new Size(750, 50);
    textBoxDescContainer[y].Location = new Point(225, 150 + (y * 25));

    theForm.Controls.Add(textBoxDescContainer[y]);
  }

  //****TextBox for Sub Total****
  for (int y = 0; y <= 16; y++)
  {
    textBoxSubTotalContainer.Add(new TextBox());
    textBoxSubTotalContainer[y].Size = new Size(175, 50);
    textBoxSubTotalContainer[y].Location = new Point(975, 150 + (y * 25));

    theForm.Controls.Add(textBoxSubTotalContainer[y]);
  }

  //****TextBox for Total****
  for (int y = 0; y <= 16; y++)
  {
    textBoxTotalContainer.Add(new TextBox());
    textBoxTotalContainer[y].Size = new Size(175, 50);
    textBoxTotalContainer[y].Location = new Point(1150, 150 + (y * 25));
    textBoxTotalContainer[y].TextChanged += new System.EventHandler(this.textBox_TextChanged);

    theForm.Controls.Add(textBoxTotalContainer[y]);
  }

  //****TextBox for Total All****
  textBoxAllTotalContainer.Size = new Size(175, 50);
  textBoxAllTotalContainer.Location = new Point(1150, 575);
  textBoxAllTotalContainer.TextChanged += new System.EventHandler(this.textBox_TextChanged);

  theForm.Controls.Add(textBoxAllTotalContainer);
}

private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
  AddNewForm(sender, e);
}

private void AddNewForm(object sender, EventArgs e)
{
  this.Hide();

  Form newForm = new Form();

  AddObjects(sender, e, newForm);

  newForm.ShowDialog();
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

您需要在Form_Load事件期间添加控件,否则将无法在合适的时间绘制控件。