【问题标题】:Update ProgressBar with appropriate value使用适当的值更新 ProgressBar
【发布时间】:2012-02-26 02:15:33
【问题描述】:

数学不强所以这是我的问题:

我正在使用进度条来显示在后台执行的工作进度:

我的代码片段:

            int i = 0;
            int totalFriends = 0;

            foreach (dynamic friend in facebookFriends)
            {
                totalFriends++;
            }

            foreach (dynamic friend in facebookFriends)
            {
                i++;

                var friend = new FacebookFriend
                {
                    FbId = friend["uid"].ToString()
                };

                AccountFacebookFriendRepository.SaveOrUpdate(accountFriend);
            }

现在应用程序做的远不止这些,在这里我只执行一小部分工作: 因此,例如,在我到达本节之前,进度条的值为 7,在执行工作后它必须达到 20,我想在使用从 7 到 20 的适当值进行工作时更新它:

我对此的看法如下:

var intiProgressbarValue = 7;
var finalProgressbarvalue = 20;

foreach (dynamic friend in facebookFriends)
{
    i++;
    var friend = new FacebookFriend
                     {
                         FbId = friend["uid"].ToString()
                     };
    AccountFacebookFriendRepository.SaveOrUpdate(accountFriend);
    var calculatedValue = CalculatedValue(initProgressbarValue, finalProgressBarValue,totalFriends, i);
    UpdateProgressBar( calculatedValue);
}
//note that totalFriends can be any number lets say from 0 to 5000
private int CalculatedValue(int initVal, int finalVal, int totalFriends, int currentFriend)
{
    int progressBarVal = 0;
    //** 
       Perform logic so it will return a progress value that is bigger that 7 and smaller that 20 depending on the number of friends and currently updated friend
    **//
    progressBarVal  = 8;//this would be the result of calculation, a value from 8 to 20
    return progressBarVal;
}

非常感谢任何帮助:

【问题讨论】:

  • 这与答案无关 - 你确定你正确使用了动态吗?
  • 是的,动态效果很好,我有我需要的所有值

标签: c# progress-bar backgroundworker


【解决方案1】:

试试这个:

private int CalculatedValue(int initVal, int finalVal, int totalFriends, int currentFriend)
{
    initVal++;
    var diff = finalVal - initVal; // 20-8 = 12
    return (diff*(currentFriend+1))/totalFriends + initVal;
}

这假设 currentFriend 从 0 变为 totalFriends-1,包括在内。例如,如果 currentFriend = 99totalFriends = 300,则此函数返回的答案是 12,即 8..20(含)范围内的三分之一。

【讨论】:

  • 这会根据原始问题给出一个错误,因为currentFriend 从 1 开始,而不是 0。此外,如果 currentFriend 按照您的建议从 0 开始,则第一个值应该initVal,但是这个函数返回initVal+1
  • @AdamLiss 不清楚当前朋友从1开始。我假设范围与C#数组中的索引相同,我在答案中提到了它。我还在答案中添加了 1,以响应 OP 代码中的评论:“这将是计算的结果,一个从 8 到 20 的值”。
  • 第一个 sn-p 将 i 设置为 0,然后在 foreach 循环的顶部将其递增。这意味着i 第一次调用CalculatedValue 时为1。
  • @AdamLiss OP 没有在循环中的任何地方使用i,所以我真的怀疑暗示currentFriend 从一个开始。无论如何,这是一个小问题,因为1 就在公式中。
  • 两个答案都是正确的,将其设置为已接受的答案只是为了全面实施。
【解决方案2】:

你可以使用公式

progressBarVal = initVal + (finalVal - initVal) * (currentFriend/totalFriends);

要检查数学,请在 currentFriend 为 0 时计算 progressBarVal

initVal + (finalVal - initVal) * 0 = initVal

currentFriendtotalFriends

initVal + (finalVal - initVal) * 1 = finalVal

【讨论】:

    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多