2012-08-26 75 views
0

我需要這樣的幫助,要求在C#幫助需要實現移動圖片框

有3個圖片框,紅色應在其寬度增長到左,綠色應該增加其高度,頂部藍色的寬度如果一個到達頂部邊框右側

也/任何文本框它應該給一個錯誤/停止執行

我需要在未來增加更多的圖片框,如果他們兩個人的碰撞也應該給一個錯誤/停止執行。我已經設法編寫它們,但無法獲得其他功能。請有人幫我解決這個問題。

OR

https://www.box.com/s/d0d302c6c266f52e0abf

謝謝。 RR

我下面的代碼,

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 

namespace ThreadwithmovingPicbxmoving 
{ 
    public partial class Form1 : Form 
    { 
     Thread t1; 
     Thread t2; 
     Thread t3; 

     delegate void CTMethod(int val); 
     delegate void CTFinish(string t); 
     Queue<string> order = new Queue<string>(); 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      int r = int.Parse(textBox1.Text); 
      int y = int.Parse(textBox2.Text); 
      int g = int.Parse(textBox3.Text); 

      t1 = new Thread(new ParameterizedThreadStart(loopred)); 
      t2 = new Thread(new ParameterizedThreadStart(loopyel)); 
      t3 = new Thread(new ParameterizedThreadStart(loopGree)); 

      t1.Start(r); 
      t2.Start(y); 
      t3.Start(g); 

     } 
     private void updateRed(int val) 
     { 
      pictureBox1.Height = val; 
      pictureBox1.Refresh(); 
     } 

     private void updateyell(int val) 
     { 
      pictureBox2.Height = val; 
      pictureBox2.Refresh(); 
     } 
     private void updategree(int val) 
     { 
      pictureBox3.Height = val; 
      pictureBox3.Refresh(); 

     } 

     private void loopred(object o) 
     { 
      int c = (int)o; 
      CTMethod ctred = new CTMethod(updateRed); 
      if (c < 500) 
      { 
       for (int i = 0; i < c; i++) 
       { 
        this.Invoke(ctred, i); 
        Thread.Sleep(20); 
       } 
      } 

      else 
      { 
       MessageBox.Show("Enter a value less than 500 for Red Box!!!"); 
      } 
      CTFinish CTFin = new CTFinish(Threadfinish); 
      this.Invoke(CTFin, "Red"); 
     } 

     private void loopyel(object o) 
     { 
      int c = (int)o; 
      CTMethod ctyell = new CTMethod(updateyell); 
      if (c < 500) 
      { 
       for (int i = 0; i < c; i++) 
       { 
        this.Invoke(ctyell, i); 
        Thread.Sleep(20); 
       } 
      } 
      else 
      { 
       MessageBox.Show("Enter a valure less than 500 for Yellow Box!!!"); 
      } 
      CTFinish CTFin = new CTFinish(Threadfinish); 
      this.Invoke(CTFin, "Yell"); 
     } 


     private void loopGree(object o) 
     { 
      int c = (int)o; 
      CTMethod ctgree = new CTMethod(updategree); 
      if (c < 500) 
      { 
       for (int i = 0; i < c; i++) 
       { 
        this.Invoke(ctgree, i); 
        Thread.Sleep(20); 
       } 
      } 
      else 
      { 
       MessageBox.Show("Enter a valure less than 500 for Green Box!!!"); 
      } 
      CTFinish CTfin = new CTFinish(Threadfinish); 
      this.Invoke(CTfin, "Green"); 

     } 

     private void Threadfinish(string t) 
     { 
      order.Enqueue(t); 
      if (order.Count == 3) 
      { 

       MessageBox.Show("Threads finished in this order: \n" + "1." + order.Dequeue() + "\n" + "2." + order.Dequeue() 
        + "\n" + "3." + order.Dequeue() + "\n", "finished"); 

      } 
     } 

    } 
} 
+0

嗨,感謝您的關注。我希望圖片盒只能長到一邊,是它的一種移動或成長到一邊,紅色的應該長到左側,綠色到頂部,藍色到右側。如果我需要停止執行/某些錯誤,我會添加更多框。 –

+0

您可以在這些文本框中輸入圖片框應該生長的時間長度..有3個圖片框用於3個圖片框..其餘2個可以忽略它們,因爲它們是用於不同的用途。它使用線程來同時生成它們。 –

+0

三個圖片盒很簡單。但是對於動態數量的圖片框,每次添加或刪除圖片框時都必須更改代碼。 – Sami

回答

1

試試看。它的一切優秀的三個PictureBoxes

創建名爲幫助新寡婦應用程序,然後用下面

運行它替換Form1.cs的代碼。它還包括TextBoxes的值

using System; 
using System.Drawing; 
using System.Threading; 
using System.Windows.Forms; 
using System.Collections.Generic; 


namespace help 
{ 
public partial class Form1 : Form 
{ 

    Thread t1; 
    Thread t2; 
    Thread t3; 

    delegate void CTMethod(int val); 
    delegate void CTFinish(string t); 
    Queue<string> order = new Queue<string>(); 


    #region Variables of Designer File 
    private System.Windows.Forms.PictureBox pictureBox1; 
    private System.Windows.Forms.PictureBox pictureBox2; 
    private System.Windows.Forms.PictureBox pictureBox3; 
    private System.Windows.Forms.TextBox textBox1; 
    private System.Windows.Forms.TextBox textBox2; 
    private System.Windows.Forms.TextBox textBox3; 
    private System.Windows.Forms.Button button1; 
    #endregion 


    public Form1() 
    { 
     #region Designer Code I have Cut and Pasted Here 
     this.pictureBox1 = new System.Windows.Forms.PictureBox(); 
     this.pictureBox2 = new System.Windows.Forms.PictureBox(); 
     this.pictureBox3 = new System.Windows.Forms.PictureBox(); 
     this.textBox1 = new System.Windows.Forms.TextBox(); 
     this.textBox2 = new System.Windows.Forms.TextBox(); 
     this.textBox3 = new System.Windows.Forms.TextBox(); 
     this.button1 = new System.Windows.Forms.Button(); 
     ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 
     ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit(); 
     ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit(); 
     this.SuspendLayout(); 
     this.Controls.Add(this.button1); 
     this.Controls.Add(this.textBox3); 
     this.Controls.Add(this.textBox2); 
     this.Controls.Add(this.textBox1); 
     this.Controls.Add(this.pictureBox3); 
     this.Controls.Add(this.pictureBox2); 
     this.Controls.Add(this.pictureBox1); 

     // 
     // pictureBox1 
     // 
     this.pictureBox1.BackColor = System.Drawing.Color.Red; 
     this.pictureBox1.Location = new System.Drawing.Point(161, 268); 
     this.pictureBox1.Name = "pictureBox1"; 
     this.pictureBox1.Size = new System.Drawing.Size(100, 50); 
     this.pictureBox1.TabIndex = 0; 
     this.pictureBox1.TabStop = false; 
     // 
     // pictureBox2 
     // 
     this.pictureBox2.BackColor = System.Drawing.Color.Green; 
     this.pictureBox2.Location = new System.Drawing.Point(383, 268); 
     this.pictureBox2.Name = "pictureBox2"; 
     this.pictureBox2.Size = new System.Drawing.Size(100, 50); 
     this.pictureBox2.TabIndex = 1; 
     this.pictureBox2.TabStop = false; 
     // 
     // pictureBox3 
     // 
     this.pictureBox3.BackColor = System.Drawing.Color.Blue; 
     this.pictureBox3.Location = new System.Drawing.Point(605, 268); 
     this.pictureBox3.Name = "pictureBox3"; 
     this.pictureBox3.Size = new System.Drawing.Size(100, 50); 
     this.pictureBox3.TabIndex = 2; 
     this.pictureBox3.TabStop = false; 
     // 
     // textBox1 
     // 
     this.textBox1.Location = new System.Drawing.Point(161, 26); 
     this.textBox1.Name = "textBox1"; 
     this.textBox1.Size = new System.Drawing.Size(100, 20); 
     this.textBox1.TabIndex = 3; 
     // 
     // textBox2 
     // 
     this.textBox2.Location = new System.Drawing.Point(383, 25); 
     this.textBox2.Name = "textBox2"; 
     this.textBox2.Size = new System.Drawing.Size(100, 20); 
     this.textBox2.TabIndex = 4; 
     // 
     // textBox3 
     // 
     this.textBox3.Location = new System.Drawing.Point(605, 26); 
     this.textBox3.Name = "textBox3"; 
     this.textBox3.Size = new System.Drawing.Size(100, 20); 
     this.textBox3.TabIndex = 5; 
     // 
     // button1 
     // 
     this.button1.Location = new System.Drawing.Point(37, 23); 
     this.button1.Name = "button1"; 
     this.button1.Size = new System.Drawing.Size(75, 23); 
     this.button1.TabIndex = 6; 
     this.button1.Text = "Go"; 
     this.button1.UseVisualStyleBackColor = true; 
     this.button1.Click += new System.EventHandler(this.button1_Click); 
     this.PerformLayout(); 
     #endregion 

     InitializeComponent(); 

     textBox1.Text = "490"; 
     textBox2.Text = "490"; 
     textBox3.Text = "490"; 

     textBox1.Leave += new EventHandler(textBox1_Leave); 
     textBox2.Leave += new EventHandler(textBox2_Leave); 
     textBox3.Leave += new EventHandler(textBox3_Leave); 
    } 

    // To input Only Valid int values in TextBoxes 
    #region TextBoxes Input Validation 
    int t1Val = 490; 
    int t2Val = 490; 
    int t3Val = 490; 
    void textBox1_Leave(object sender, EventArgs e) 
    { 
     try 
     { 
      int t1Val = Convert.ToInt32(textBox1.Text); 
     } 
     catch 
     { 
      textBox1.Focus(); 
     } 
    } 
    void textBox2_Leave(object sender, EventArgs e) 
    { 
     try 
     { 
      int t2Val = Convert.ToInt32(textBox2.Text); 
     } 
     catch 
     { 
      textBox2.Focus(); 
     } 
    } 
    void textBox3_Leave(object sender, EventArgs e) 
    { 
     try 
     { 
      int t3Val = Convert.ToInt32(textBox3.Text); 
     } 
     catch 
     { 
      textBox3.Focus(); 
     } 
    } 
    #endregion 

    private void button1_Click(object sender, EventArgs e) 
    { 
     int r = t1Val; 
     int y = t2Val; 
     int g = t3Val; 

     t1 = new Thread(new ParameterizedThreadStart(loopRed)); 
     t2 = new Thread(new ParameterizedThreadStart(loopGreen)); 
     t3 = new Thread(new ParameterizedThreadStart(loopBlue)); 

     // It will avoid proble if you exit app when threads are working 
     t1.IsBackground = true; 
     t2.IsBackground = true; 
     t3.IsBackground = true; 


     t1.Start(r); 
     t2.Start(y); 
     t3.Start(g); 

    } 
    private void updateRed(int val) 
    { 
     pictureBox1.Width++; 
     pictureBox1.Left--; 
     pictureBox1.Refresh(); 
    } 

    private void updateGreen(int val) 
    { 
     pictureBox2.Height++; 
     pictureBox2.Top--; 
     pictureBox2.Refresh(); 
    } 
    private void updateBlue(int val) 
    { 
     pictureBox3.Width++; 
     pictureBox3.Refresh(); 
    } 

    private void loopRed(object o) 
    { 
     int c = (int)o; 
     CTMethod ctRed = new CTMethod(updateRed); 
     if (c < 500) 
     { 
      for (int i = 0; i < c; i++) 
      { 
       if (pictureBox1.Left > 0) 
       { 
        this.Invoke(ctRed, i); 
        Thread.Sleep(20); 
       } 
      } 
     } 

     else 
     { 
      MessageBox.Show("Enter a value less than 500 for Red Box!!!"); 
     } 
     CTFinish CTFin = new CTFinish(Threadfinish); 
     this.Invoke(CTFin, "Red"); 
    } 

    private void loopGreen(object o) 
    { 
     int c = (int)o; 
     CTMethod ctGreen = new CTMethod(updateGreen); 
     if (c < 500) 
     { 
      for (int i = 0; i < c; i++) 
      {      
       if (pictureBox2.Top > 0 && pictureBox2.Top != textBox2.Top + textBox2.Height) 
       { 
        this.Invoke(ctGreen, i); 
        Thread.Sleep(20); 
       } 
       else 
        break; 
      } 
     } 
     else 
     { 
      MessageBox.Show("Enter a valure less than 500 for Green Box!!!"); 
     } 
     CTFinish CTFin = new CTFinish(Threadfinish); 
     this.Invoke(CTFin, "Green"); 
    } 


    private void loopBlue(object o) 
    { 
     int c = (int)o; 
     CTMethod ctBlue = new CTMethod(updateBlue); 
     if (c < 500) 
     { 
      for (int i = 0; i < c; i++) 
      { 
       if (pictureBox3.Left + pictureBox3.Width < this.Width) 
       { 
        this.Invoke(ctBlue, i); 
        Thread.Sleep(20); 
       } 
       else 
        break; 
      } 
     } 
     else 
     { 
      MessageBox.Show("Enter a valure less than 500 for Blue Box!!!"); 
     } 
     CTFinish CTfin = new CTFinish(Threadfinish); 
     this.Invoke(CTfin, "Blue"); 

    } 

    private void Threadfinish(string t) 
    { 
     order.Enqueue(t); 
     if (order.Count == 3) 
     { 

      MessageBox.Show("Threads finished in this order: \n" + "1." + order.Dequeue() + "\n" + "2." + order.Dequeue() 
       + "\n" + "3." + order.Dequeue() + "\n", "finished"); 

     } 
    } 

} 
} 
+0

這很好,現在如果添加5個圖片框,並開始增長,然後遍及我如何處理,如果2個不斷增長的圖片框相撞。我想發送一條錯誤消息/停止執行。 –

+0

Okie,多數民衆贊成,我會檢查現在.. –