2017-05-09 7 views
-1

私は2つのリストボックス(listboxlongとlistboxlat)を持っており、(タイマー(timer1)を使用して)リストボックスで値を描画するピクチャボックスを持っています。私は各行のピクチャボックスのxとyの値(x値の場合はリストボックス、y値の場合はリストボックス)に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ループは、1つのことと1つのことだけを行うことを意図しています:いくつかのコレクションの各要素をループします。それは '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); 
} 

私はあなたが1つのリストの最初の項目を実行しようとしていると思います最初の項目を他のリストに追加して続行します。あなたはそれらを同期させています。

タプルを使用してタプルのリストに格納する方がよいでしょう。そして、あなたは "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

ありがとう、あなたはそんなにアンドリューそれは私が欲しいものです。ちょうどあなたのコードに小さな間違いがあります。 'foreach(myTupleのvar tuple)'は 'foreach(myTableのvar tuple)'の代わりにする必要があります – Quanthema

+0

編集して修正しました。 ;) –

関連する問題