繁体   English   中英

从动态添加的组合框打印数据?

[英]Printing data from dynamically added combo boxes?

我已经获得了可以使用的代码,因此,当我按下按钮“添加更多”时,它将向名称为dayBox1,dayBox2等的表单添加更多组合框。

这是该代码:

        private void addMoreBtn_Click(object sender, EventArgs e)
    {
        //Keep track of how many dayBoxes we have
        howMany++;

        //Make a new instance of ComboBox
        ComboBox dayBox = new System.Windows.Forms.ComboBox();

        //Make a new instance of Point to set the location
        dayBox.Location = new System.Drawing.Point(Left, Top);
        dayBox.Left = 13;
        dayBox.Top = 75 + dayLastTop;

        //Set the name of the box to dayBoxWhateverNumberBoxItIs
        dayBox.Name = "dayBox" + howMany.ToString();

        //The TabIndex = the number of the box
        dayBox.TabIndex = howMany;

        //Make it visible
        dayBox.Visible = true;

        //Set the default text
        dayBox.Text = "Pick One";

        //Copy the items of the original dayBox to the new dayBox
        for (int i = 0; i < dayBoxO.Items.Count; i++)
        {
            dayBox.Items.Add(dayBoxO.Items[i]);
        }

        //Add the control to the form
        this.Controls.Add(dayBox);

        //The location of the last box's top with padding
        dayLastTop = dayBox.Top - 49;
    }

打印带有按钮事件添加的框的选定成员的最佳方法是什么?

我之前将想要的信息打印到文件中的方式是这样的(仅从一个框开始):

public void saveToFile()
        {     
            FileInfo t = new FileInfo("Workout Log.txt");
            StreamWriter Tex = t.CreateText();
            Tex.WriteLine("---------------Workout Buddy Log---------------");
            Tex.WriteLine("------------------- " + DateTime.Now.ToShortDateString() + " ------------------");
            Tex.Write(Tex.NewLine);
            if (dayBoxO.Text != "Pick One")
            {
                Tex.WriteLine("Day: " + dayBoxO.Text);
            }
            else
            {
                Tex.WriteLine("Day: N/A");
            }
        }

我希望能够对每个框,每个新行都执行此操作。 因此,它就像:Day:(box1的文本)Day:(box2的文本)Day:(box3的文本)等等……谢谢!

假设控件存在于窗体上,请遍历窗体上的所有控件:

var boxes =  from Control c in this.Controls
             where c.GetType() == typeof(System.Windows.Forms.ComboBox)
             select c;

StringBuilder sb = new StringBuilder();
foreach (Control c in boxes)
{
    sb.AppendLine(c.Text); // ...
}

非Linq方法:

StringBuilder sb = new StringBuilder();
foreach (Control c in this.Controls)
{
    if (c.GetType() == typeof(System.Windows.Forms.ComboBox))
    {
        sb.AppendLine(c.Text); // ...
    }
}

但是,依赖于Controls集合有点麻烦。 特别是当您开始有面板和其他分组时。 您可以考虑按照Josh的建议将控件添加到自己的集合中。

我立即看到有两种方法可以执行此操作:

首先是在添加控件时维护对这些控件的引用列表。

private List<ComboBox> _comboBoxList = new List<ComboBox>();

...

//Make a new instance of ComboBox
ComboBox dayBox = new System.Windows.Forms.ComboBox();

//Add your combobox to the master list
_comboBoxList.Add(dayBox);

...

然后,saveToFile()方法将包含如下所示的一行:

foreach(var box in _comboBoxList)
{
      Tex.Write(Tex.NewLine);
      if (box.Text != "Pick One")
      {
          Tex.WriteLine("Day: " + box.Text);
      }
      else
      {
          Tex.WriteLine("Day: N/A");
      }
}

第二种方法是按照Mike的指示进行操作,然后在控件列表中查找您的ComboBoxes。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM