如何将 AND 添加到 foreach 循环?

How to add AND to foreach loop?

我有两个列表框(listboxlong 和 listboxlat),还有用于在列表框中绘制值的图片框(通过使用计时器 (timer1))。我想将每行的列表框值添加到图片框 x 和 y 值(x 值的 listboxlong,y 值的 listboxlat)。即使我尝试了 foreach 循环,我也无法实现它。如果有与 foreach 一样工作的代码,请告诉我。谢谢你的帮助。这是我的代码;

 private void timer1_Tick(object sender, EventArgs e)
    {
        pictureBoxPath.Refresh();
       listBoxLong.Items.Add(gPathBoylam);
       listBoxLat.Items.Add(gPathEnlem);
     }
  private void pictureBoxPath_Paint(object sender, PaintEventArgs e)
    {
        SolidBrush myBrush = new SolidBrush(Color.Red);

       foreach (var item in listBoxLat.Items)
        {
              foreach (var item2 in listBoxLong.Items)
            {
          e.Graphics.DrawEllipse(myPen, Convert.ToInt16(item), Convert.ToInt16(item2), 2, 2);
            }

        }           

    }

你需要意识到你的问题不是很清楚,但是阅读你的评论并专门寻找这个:

foreach(var item in listBoxLat.Items && var item2 in listBoxLong.Items)
{
    e.Graphics.DrawEllipse(myPen, Convert.ToInt16(item), Convert.ToInt16(item2), 2, 2);
}

我认为您正在尝试 运行 从一个列表的第一项到另一个列表的第一项,然后继续。你正在同步它们。

因此,更好的方法是使用元组存储在元组列表中。您需要了解“Graphics.DrawEllipse”的工作原理。所以我把文档摘要放在下面。

所以下面的代码可能有效,我无法测试这个,因为我现在在工作。

List<Tuple<int, int>> myTuple = new List<Tuple<int, int>>();

private void timer1_Tick(object sender, EventArgs e)
{
    pictureBoxPath.Refresh();

    myTuple.Add(new Tuple<int, int>(gPathBoylam, gPathEnlem));  
}

//
// Summary:
//     Draws an ellipse defined by a bounding rectangle specified by coordinates
//     for the upper-left corner of the rectangle, a height, and a width.
//
// Parameters:
//   pen:
//     System.Drawing.Pen that determines the color, width,
//      and style of the ellipse.
//
//   x:
//     The x-coordinate of the upper-left corner of the bounding rectangle that
//     defines the ellipse.
//
//   y:
//     The y-coordinate of the upper-left corner of the bounding rectangle that
//     defines the ellipse.
//
//   width:
//     Width of the bounding rectangle that defines the ellipse.
//
//   height:
//     Height of the bounding rectangle that defines the ellipse.
//
// Exceptions:
//   System.ArgumentNullException:
//     pen is null.
private void pictureBoxPath_Paint(object sender, PaintEventArgs e)
{   
    Pen myPen = new Pen(Color.Red, 3); // Create pen

    if(myTuple != null && myTuple.Any())
    {
        foreach (var tuple in myTuple)
        {   
            Rectangle rect = new Rectangle(Convert.ToInt16(tuple.Item1), Convert.ToInt16(tuple.Item2), 2, 2); // Create rectangle for ellipse

            e.Graphics.DrawEllipse(myPen, rect); // Draw ellipse to screen
        }
    }
}