2013-02-07 29 views
1

我對C sharp編程很陌生。我有如下圖所示的設計。用一些百分比填充圖片框

我的概念是我必須設置在「傳送卷」文本框(例如100),一些體積,然後按「確定」按鈕。它自動設置圖片框的比例,它工作正常。

現在我想填充顏色的圖片框當我點擊「再生」按鈕。圖片框中要填充的顏色的百分比應該是文本框中關於顏色或液體的數字。

例如 如果設置了氣相= 5;烴液體= 5;水= 5;油基泥= 5;水基泥漿= 5;未鑑定= 75

那麼圖片具有以填充用75%的用「未識別」的顏色和顏色氣相用5%e.t.c.

我寫了一些代碼,如下所示。

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; 


    namespace test 
    { 
    public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void txtTransferVolume_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void txtTransferSet_Click(object sender, EventArgs e) 
    { 
     string value = txtTransferVolume.Text; 
     double midvalue = Convert.ToDouble(value); 
     lblTransferBottleMax.Text = value; 
     lblTransferBottleMid.Text = (midvalue/2).ToString(); 

    } 

    private void chkTransferManual_CheckedChanged(object sender, EventArgs e) 
    { 


    } 

    private void btnTransferBottleRegenerate_Click(object sender, EventArgs e) 
    { 

    } 

    } 
} 

請幫我如何填寫我想要的。

回答

1

這可以通過直接在PictureBox的控制繪製或創建在PictureBox的存儲器,並顯示一個位圖相當容易地實現。

例子:

private void DrawPercentages(int[] percentages, Color[] colorsToUse) 
{ 
    // Create a Graphics object to draw on the picturebox 
    Graphics G = pictureBox1.CreateGraphics(); 

    // Calculate the number of pixels per 1 percent 
    float pixelsPerPercent = pictureBox1.Height/100f; 

    // Keep track of the height at which to start drawing (starting from the bottom going up) 
    int drawHeight = pictureBox1.Height; 

    // Loop through all percentages and draw a rectangle for each 
    for (int i = 0; i < percentages.Length; i++) 
    { 
     // Create a brush with the current color 
     SolidBrush brush = new SolidBrush(colorsToUse[i]); 
     // Update the height at which the next rectangle is drawn. 
     drawHeight -= (int)(pixelsPerPercent * percentages[i]); 
     // Draw a filled rectangle 
     G.FillRectangle(brush, 0, drawHeight, pictureBox1.Width, pixelsPerPercent * percentages[i]); 
    }  
} 

當然你得檢查兩個數組是相同的長度,等等。我只是想給你如何做到這一點的基本思想。

下面是如何在數組中獲取數據並將它們傳遞給函數的概念。由於您爲每個值使用了不同的文本框,因此很難對它們進行迭代,所以現在我正在向您展示如何使用您現在具有的6個值執行此操作。

private void btnTransferBottleRegenerate_Click(object sender, EventArgs e) 
{ 
    int[] percentages = new int[6]; 
    percentages[0] = int.Parse(txtTransferNotIdentified.Text); 
    percentages[1] = int.Parse(txtTransferWater.Text); 
    // And so on for every textbox 

    Color[] colors = new Color[6]; 
    colors[0] = Color.Red; 
    colors[1] = Color.Yellow; 
    // And so on for every color 

    // Finally, call the method in my example above 
    DrawPercentages(percentages, colors); 
} 

如果你的百分比並不總是總計爲100,您可以使用指定的和第三個參數,並在DrawPercentages方法的價值100f改變這個值。

+0

我只是通過一個例子向你展示繪圖的概念。不管你喜歡什麼,都可以自由地解釋它,但是我沒有看到如何收集數據並將其作爲數組傳遞給客戶。你也可以使用Graphics對象在它自己的行上繪製每個矩形,但我總是喜歡保持它的可縮放性。以後任何數據的添加都不會對我的方法造成影響,因爲爲數據中的每個項目編寫一行可能需要額外的代碼。 –

+0

是的你是對的,我也在想如何從那裏收集數據(文本框和標籤)並將它傳遞給數組。 – PRV

+0

你知道如何將這些輸入的值傳遞給百分比數組嗎? – PRV