2015-02-24 59 views
0

我在C#中編寫了一個表單,在其中您在NumericUpDown框中輸入一個值,並且在進度條下單擊該按鈕時將增加輸入的值。ProgressBar按照NumericUpDown框中的值增加

我使用的計時器對於本與代碼之中:

private void ProgressBar_Tick(object sender, EventArgs e) 
    { 
     if (filesize <= 60) 
      sixtyfree.Increment(filesize); 

    } 

文件大小是的NumericUpDown框的名稱,我這個值轉換爲int,使得其將與這個工作。我面對的問題是,無論60歲以下我輸入什麼數字,都會填滿進度條,而不是隻輸入「文件大小」中輸入的數量,任何人都可以幫我解決這個問題嗎?

呀ofcourse,這裏是我的全碼:

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

namespace Worst_fit_algorithm_GUI 
{ 
public partial class Form1 : Form 
{ 

    int filesize; 
    int progressBarSetValue = 40; //The set value of first progressBar 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void downloadfile_Click(object sender, EventArgs e) 
    { 
     this.ProgressBar.Start(); 


    } 

    private void ProgressBar_Tick(object sender, EventArgs e) 
    { 
     if (sixtyfree.Value <= sixtyfree.Maximum - progressBarSetValue) 
     { 
      sixtyfree.Value = progressBarSetValue + (int)filesize; 
      ProgressBar.Stop(); 
     } 


    } 

    private void FileSize_ValueChanged_1(object sender, EventArgs e) 
    { 
     filesize = Convert.ToInt32(FileSize.Value); 

    } 

    private void sixtyfree_Click(object sender, EventArgs e) 
    { 

    } 

    private void label1_Click(object sender, EventArgs e) 
    { 

    } 
} 
} 
+0

什麼進度的最大價值?絕對是60左右吧? – 2015-02-24 19:02:36

+0

它有40設定值已經和最大值爲100,我想輸入的號碼時就加入到這個40 – Max 2015-02-24 20:58:31

+0

sixtyfree看起來像NumericUpDown控件。如果文件大小不會改變,那麼進度條將始終增加,直到達到最大值。我認爲* *你想要的東西像'如果(sixtyfree.Value +文件大小<= 60)' – LarsTech 2015-02-24 21:07:25

回答

0

我使用ValueChanged事件來設置進度條。您可以使用計時器你幹得:

int progressBarSetValue = 40; //The set value of progressBar you want 

private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
{ 
    if (numericUpDown1.Value <= progressBar1.Maximum - progressBarSetValue) 
    { 
     progressBar1.Value = progressBarSetValue + (int)numericUpDown1.Value; 
    } 
} 

的代碼是比較通用的,這意味着你不必使用固定60值。只要progressBarSetValue不大於progressBar1.Maximum,您可以更改進度條的最大值,而不必關心數值下降值是否會溢出進度條。

編輯

NumericUpDown控件的名字=文件大小,進度控制名稱= sixtyfree

private void ProgressBar_Tick(object sender, EventArgs e) 
{ 
    if (filesize <= sixtyfree.Maximum - sixtyfree.Value) 
    { 
     sixtyfree.Value += filesize; 
    } 
    else 
    { 
     sixtyfree.Value = sixtyfree.Maximum; 
    } 

    ProgressBar.Stop(); 
} 
+0

我已經添加了這個,它的工作魅力,但有60個空閒插槽,並可以說我加40進度條會增加,但如果我添加另一個值後,讓我們說20它應該完成進度條,但一旦沒有再次發生任何事情發生,我按了按鈕後,無論我輸入的值如何。 – Max 2015-02-25 13:18:21

+0

@ user3596350您正在嘗試的按鈕是什麼?你在哪裏添加40和20的值?你能顯示你的代碼嗎? – 2015-02-25 13:42:39

+0

好吧,我已將我的完整代碼添加到我的原始代碼中,對不起,我不打算編輯您的代碼並將其添加到該代碼中。 基本上有一個按鈕位於NumericUpDown框下,這樣當按下時,用戶輸入的數字將被添加到進度條,直到達到其最大值 – Max 2015-02-25 13:50:25

-1

使用 ProgressBar.Value =文件大小;