2017-05-09 3 views
-1

두 개의 목록 상자 (listboxlong 및 listboxlat)가 있으며 목록 상자에 타이머 (timer1)를 사용하여 값을 그릴 그림 상자가 있습니다. 각 줄의 그림 상자 x 및 y 값 (x 값의 경우 listboxlong, y 값의 경우 listboxlat)에 listboxes 값을 참여시키고 싶습니다. foreach 루프를 시도해도 달성 할 수 없었습니다. foreach로 작동하는 코드가 있고 알려 주시면 감사하겠습니다. 당신의 도움을 주셔서 감사합니다. 여기 내 코드가있다. 당신은 당신의 qustion을 깨달을 필요가AND를 루프에 추가하는 방법은 무엇입니까?

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); 
      } 

     }   

    } 
+0

그래서 어떤 오류가 발생하고 "AND"질문은 나에게 의미가 없지만 상관없이 어떤 문제를보고 있습니까? 목록 상자 항목을 int로 캐스팅하려고하는 것을 보았습니다 ... 시도하지는 않았지만 item.value 뒤에 있어야한다고 생각했습니다. – Trey

+0

문제에 대한 명확한 설명을 제공해주십시오. – MetaColon

+0

foreach 루프는 한 가지만하고 한 가지만 수행하는 것을 의미합니다. 컬렉션의 각 요소를 반복합니다. 'foreach (..) '부분에서 또 다른 조건을 검사하지도 않습니다. 2 개의 콜렉션을 반복하고 싶다면, 콜렉션을 네 스팅해야합니다. 또는 for와 같은 다른 루프를 사용하십시오. –

답변

1

하지 분명하지만, 귀하의 의견을 읽고이를 위해 특별히 찾고 :

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

난 당신이 한 목록의 첫 번째 항목을 실행하려고 생각 첫 번째 항목을 다른 목록으로 이동하고 계속합니다. 그 menas 당신이 그들을 동기화하고 있습니다.

더 나은 방법은 튜플을 사용하여 튜플 목록에 저장하는 것입니다. 그리고 "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 
     } 
    } 
} 
+0

정말 고마워요 앤드류 내가 뭘 원하는지 정확히. 그냥 거기에 코드를 suc에 작은 실수가있다; 'foreach (myTuple의 var tuple)'이 'foreach (myTool의 var tuple)'대신에 있어야합니다. – Quanthema

+0

편집되고 수정되었습니다. ;) –

관련 문제