c# 将点发送到文本框

c# Sending points to text box

我制作了两个列表,其中包含我用来在图表上绘制点的值。其中一个包含整数(值列表),另一个包含长整数(时间列表)。我想将它们放入这种格式 (x,y) 的文本框中,但我不确定该怎么做。我尝试使用 foreach 和 for 循环,但到目前为止都没有用。

private void pointsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        /*
        foreach (var pointsY in Times)
        {
            foreach(var pointsX in Values)
            {

            }
        } // end foreach 
        */
        for(int i = 0; i < Times.Count; i++)
        {
            for(int a = 0; a < Values.Count; i++)
            {
                // textBox1.Text += "(" + Values[a] + "," + (int) Times[i] + "), ";
            }
        }

    }

假设它们是匹配的集合,这意味着每个集合都具有相同数量的项目,您只需像这样对其中一个列表进行索引。

        for (int i = 0; i < Times.Count; i++)
        {
                 textBox1.Text += "(" + Values[i] + "," + Times[i] + "), ";
        }

假设您有两个列表:

List<int> ValuesList = new List<int>() { 1, 5, 7, 9, 12, 15 };
List<long> TimesList = new List<long>() { 0001, 0002, 0003, 0004, 0005, 0006 };

然后...(假设每个列表中的项目数相同,请参阅下面的字典示例以了解另一种存储数据的方法)您可以使用简单的 for 运算符并将文本附加到文本框:

for (int i = 0; i == ValuesList.Count; i++)
            {
                textBox.Text += string.Format("({0}, {1})", ValuesList[i], TimesList[i]);
            }

但是,(假设您的时间是唯一的)最好一起使用不同的数据结构来将值存储在:

Dictionary<long, int> VTDictionary = new Dictionary<long, int>();

然后您可以像这样向字典中添加项目:

VTDictionary.Add(0001, 1);
VTDictionary.Add(0002, 2);

这样可以将相似的类型放在一起...而不是在单独的列表中,因此您不必协调它们。

然后,要从字典中检索项目并将它们附加到文本框,您可以像您之前提到的那样使用 foreach:

foreach (KeyValuePair<long, int> kvp in VTDictionary)
            {
                textBox.Text += string.Format("({0}, {1})", kvp.Value, kvp.Key); 
            }