简体   繁体   中英

c# Sending points to text box

I made two lists containing values I used to plot points on a graph. One of them containing ints (Values list) and the other containing longs (Times list). I want to put them into a textbox in this format (x,y), and I'm not sure how to do so. I tried using a foreach and a for loop but neither have worked so far.

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] + "), ";
            }
        }

    }

Assuming that they are matched sets meaning that each has the same number of items, you only should index through one of the lists like this.

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

Assuming you have two lists:

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

Then... (assuming the number of items in each list is the same, see below dictionary example for another way to store your data) you can use a simple for operator and append the text to the textbox:

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

However, (assuming your times are unique) it would probaly be better to use a different data structure all together to store the values in:

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

Then you can add items to the dictionary like so:

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

This keeps the like types together... not in separate lists, so, you don't have to reconcile them.

Then, to retrieve the items out of the dictionary and append them to the textbox you can use a foreach like you mentioned that you tried to do before:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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