在文本框中添加计时器

时间:2014-05-31 10:03:09

标签: c# timer textbox

我创建了2个词典。一本字典我正在保存它的信息。然后我将根据值对该字典中的信息进行排序。然后,我将把信息保存在第二个字典中。我将显示第二个词典中的信息。我想要的是,每3秒就会出现一条信息。我能这样做吗?

//intialize to get and set the coodrdinats 
int xCoor;
int yCoor;
int prioritySaver;
int timeSaver;

//Intialize the Dictionaries
Dictionary<int, int> pQueuValues = new Dictionary<int, int>();

//Intialize the Lists
List<int> saveRandomTime = new List<int>();
List<int> randomListxCoor = new List<int>();
List<int> randomListyCoor = new List<int>(); 

private void PQueue(int nodenum)
{
    for (int x = 1; x <= nodenum; x++)
    {
        xCoor = coor.Next(0, 700);
        yCoor = coor.Next(0, 730);
        if (!randomListxCoor.Contains(xCoor))
        {
            randomListxCoor.Add(xCoor);
        }
        if (!randomListyCoor.Contains(xCoor))
        {
            randomListyCoor.Add(yCoor);
        }
        prioritySaver = pQueuNumbers.Next(1, nodenum*3);
        timeSaver = randomTime.Next(1, 201);

        //Add the values t the Dictionaries and the List.
        pQueuValues.Add(x, prioritySaver);
    }

    //Sort the Dictionary 
    foreach (KeyValuePair<int, Int32> savePQ in pQueuValues.OrderBy(key => key.Value))
    {
        txtOutput.Text += "\r\n\n" + "The node number  :" + savePQ.Key + "  Has the Prority:  " + savePQ.Value;
    }
    //I want the timer the show the infroamtion from the sorted dictionary.

}
}              
我正在考虑做这样的事情。但是我在下面的代码中遇到问题,该代码打印字典中的所有值。我希望它一次打印一个值。

//Sort the Dictionary 
foreach (KeyValuePair<int, Int32> savePQ in pQueuValues.OrderBy(key => key.Value))
{
    pQueueOrdered.Add(savePQ.Key, savePQ.Value);

    //txtOutput.Text += "\r\n\n" + "The node number  :" + savePQ.Key + "  Has the Prority:  " + savePQ.Value;
}

var timer = new System.Windows.Forms.Timer();
int track = 0;
timer.Tick += (timerObject, timerArgs) =>
{
    timer.Interval = 3000;
    txtOutput.Text += "\r\n\n" + "The node number  :" + pQueueOrdered.ContainsKey(track) + "  Has the Prority:  " + pQueueOrdered[track];
}
++track;
if (track > nodenum+1)
{
    timer.Stop();
    timer.Dispose();
    this.txtOutput.Text += "\r\r\n" + "  Ends x " + track.ToString();
}
};
timer.Start();
}

2 个答案:

答案 0 :(得分:0)

基于您现在拥有的代码和模糊的描述以及您的代码blob不可编译的事实,我认为这接近您的需求。我假设你想点击一个按钮启动计时器并保留按钮的OnClick事件中包含的所有内容。

    public void Button1_Click(object sender,EventArgs e)
    {
        var timer = new System.Windows.Forms.Timer();
        timer.Interval = 3000;    // every 3 seconds

        int track = 0; // where are we, aka the index

        txtOutput.Text = String.Empty;
        var output = new StringBuilder(); // use a stringbuilder 

        timer.Tick += (timerObject, timerArgs) =>
        {
             // check if track exists 
             // I couldn't find the type of pQueueOrdered so
             // I hope this works
             // otherwise track<=nodeNum might be an alternative
             if (pQueueOrdered.ContainsKey(track))
             {
                 output.AppendFormat(String.Format(
                     "\r\n\nThe node number  :{0}   Has the Prority:  {1}",  
                     pQueueOrdered.ContainsKey(track),
                     pQueueOrdered[track]));
                 txtOutput.Text = output.ToString();
                 // increase track to get the next
                 // item at the next Tick
                 track++;
             }
             // stop if done... 
             if (track > nodenum+1)
             {
                 timer.Stop();
                 output.AppendFormat(String.Format(
                    "\r\r\n  Ends x {0}",
                    track));
                 txtOutput.Text =  output.ToString();
             }
         }

         timer.Start();
    }

答案 1 :(得分:0)

您的代码有点难以理解。例如,您正在更新未使用的变量。

在任何情况下,您都应该考虑使用Microsoft的Reactive Framework。 (NuGet“Rx-Main”。)

以下是您最需要的代码:

private Random rnd = new Random();

private void PQueue(int nodenum)
{
    var txtOutput_Text = "";

    var queue =
        from x in Enumerable.Range(1, nodenum)
        let v = new { x, ps = rnd.Next(1, nodenum * 3) }
        orderby v.ps
        select v;

    Observable
        .Interval(TimeSpan.FromSeconds(1.0))
        .Zip(queue.ToObservable(), (i, v) => v)
        .ObserveOn(txtOutput)
        .Subscribe(v =>
        {
            txtOutput.Text +=
                "\r\n\n"
                + "The node number  :" + v.x
                + "  Has the Prority:  " + v.ps;
        }, () =>
        {
            txtOutput.Text +=
                "\r\r\n"
                + "  Ends x " + nodenum.ToString();
        });
}