C# 将计时器添加到文本框
我已经创建了两本字典。一本字典我正在保存它的信息。然后我将根据值对字典中的信息进行排序。然后,我将把信息保存在第二本词典中。我将显示第二本词典中的信息。我想要的是每3秒钟就会出现一条信息。我可以这样做吗C# 将计时器添加到文本框,c#,timer,textbox,C#,Timer,Textbox,我已经创建了两本字典。一本字典我正在保存它的信息。然后我将根据值对字典中的信息进行排序。然后,我将把信息保存在第二本词典中。我将显示第二本词典中的信息。我想要的是每3秒钟就会出现一条信息。我可以这样做吗 //intialize to get and set the coodrdinats int xCoor; int yCoor; int prioritySaver; int timeSaver; //Intialize the Dictionaries Dictionary<int,
//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.
}
}
//初始化以获取和设置坐标
int xCoor;
int yCoor;
int优先保护程序;
int时间节省器;
//使字典初始化
字典pQueuValues=新字典();
//初始化列表
List saveRandomTime=新列表();
List randomListxCoor=新列表();
List randomListyCoor=新列表();
私有void PQueue(int nodenum)
{
对于(int x=1;x key.Value))
{
txtOutput.Text+=“\r\n\n”+”节点号“+savePQ.Key+”的优先级为“+savePQ.Value”;
}
//我想让计时器显示排序字典中的信息。
}
}
我也在考虑做这样的事情。但是我遇到了以下代码的问题,这些代码打印字典中的所有值。我希望它一次打印一个值
//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();
}
//对字典排序
foreach(pQueuValues.OrderBy(key=>key.Value)中的KeyValuePair保存pq)
{
pQueueOrdered.Add(savePQ.Key,savePQ.Value);
//txtOutput.Text+=“\r\n\n”+”节点号“+savePQ.Key+”的优先级为“+savePQ.Value”;
}
var timer=new System.Windows.Forms.timer();
int轨道=0;
timer.Tick+=(timerObject,timerArgs)=>
{
定时器间隔=3000;
txtOutput.Text+=“\r\n\n”+”节点号:“+pqueOrdered.ContainsKey(track)+”的优先级为:“+pqueOrdered[track];
}
++轨道;
如果(轨迹>节点枚举+1)
{
timer.Stop();
timer.Dispose();
this.txtOutput.Text+=“\r\r\n”+”结束x”+track.ToString();
}
};
timer.Start();
}
基于您现在拥有的代码和模糊的描述,以及您的代码块不可编译的事实,我认为这接近您所需要的。我假设您希望单击一个按钮来启动计时器,并将所有内容都保存在按钮的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();
}
public void按钮1\u单击(对象发送者,事件参数e)
{
var timer=new System.Windows.Forms.timer();
timer.Interval=3000;//每3秒
int track=0;//我们在哪里,也就是索引
txtOutput.Text=String.Empty;
var output=new StringBuilder();//使用StringBuilder
timer.Tick+=(timerObject,timerArgs)=>
{
//检查轨道是否存在
//我找不到PQUESO的类型
//我希望这能奏效
//否则,跟踪nodenum+1)
{
timer.Stop();
output.AppendFormat(String.Format(
“\r\r\n结束x{0}”,
轨道);
txtOutput.Text=output.ToString();
}
}
timer.Start();
}
您的代码有点难以理解。例如,您正在更新未使用的变量
在任何情况下,您都应该考虑使用Microsoft的反应式框架来实现这一点。(NuGet“Rx主”。)
以下是我最能确定的您需要的代码:
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();
});
}
感谢您编辑@HiteshSalianThanks@Onik上一次编辑,您让它更清晰易懂。不,我只有一个按钮可以完成所有操作