我已将信息保存在字典中。和用户更新的字典。如何从字典中检索信息。我做了一些代码。我不知道如何从字典中获取信息。
//Create the dictionaries
Dictionary<int, int> waytosave = new Dictionary<int, int>();
Dictionary<int ,int> numberControls = new Dictionary<int,int>();
private void btnRun_Click(object sender, EventArgs e)
{
///Setting up the coordinates
int xCoor;
int yCoor;
Random coor = new Random();
int value =7;
for (int x = 0; x < value; x++)
{
//Creating Random NumeircalUpdown.
//Using those the user can change the values.
NumericUpDown numiNumber = new NumericUpDown();
xCoor = coor.Next(0, 500);
yCoor = coor.Next(0, 500);
numiNumber.Name = x.ToString();
numiNumber.Location = new Point(xCoor, yCoor);
numiNumber.Size = new System.Drawing.Size(50, 15);
numiNumber.Maximum = 100;
numiNumber.Minimum = 0;
//Saveing the numericalUpdowns
numberControls.Add(x, 0);
this.pnlNodes.Controls.Add(numiNumber);
//Make it respond to the clicking event
numiNumber.Click += new EventHandler(GetNumUpDownValue);
}
}
//Get the values for the NumericUpDown
public void GetNumUpDownValue(object sender, EventArgs e)
{
int iname = int.Parse(((NumericUpDown)sender).Name);
int ivalue = (int)((NumericUpDown)sender).Value;
//check and update the list
if (waytosave.ContainsKey(iname))
{
waytosave[iname] = ivalue;
}
else
{
waytosave.Add(iname, ivalue);
}
txtOutputs.Text += "\r\r\n" + " Node # " + iname + " = " + waytosave[iname].ToString();
}
private void btnRoundRobin_Click(object sender, EventArgs e) {
//how can i get the saved information from the waytosave dictionary
//Can you advise me please????.
}
答案 0 :(得分:1)
这是一种方法,假设您并不真正关心密钥的排序。
// Untested Code
int index = -1;
private void btnRoundRobin_Click(object sender, EventArgs e)
{
var keys = waytosave.Keys;
if(keys.Count == 0) return;
index = (index + 1) % keys.Count;
int key = keys[index];
int value = waytosave[key];
}