
private void Dewery_Decimal_System_Load(object sender, EventArgs e)
{
Random random = new Random();
Dictionary<int,string > dictionary = new Dictionary<int, string>();
dictionary.Add(000, "Genral Knowlege");
dictionary.Add(100, "philosophy & Psycology");
dictionary.Add(200, "Religion");
dictionary.Add(300, "Social Sciences");
dictionary.Add(400, "Languages");
dictionary.Add(500, "Science");
dictionary.Add(600, "Techynology");
dictionary.Add(700, "Arts & Recriation");
dictionary.Add(800, "Litrature");
dictionary.Add(900, "History and geography");
for (int i = 0; i < 9; i)
{
int index = random.Next(dictionary.Count);
KeyValuePair<int, string> pair = dictionary.ElementAt(index);
Console.WriteLine("key: " pair.Key ", value: " pair.Value);
label28.Text = (dictionary[pair.Key]);
}
for (int i = 0; i < 9; i)
{
int index = random.Next(dictionary.Count);
KeyValuePair<int, string> pair = dictionary.ElementAt(index);
label29.Text = (dictionary[pair.Key]);
}
for (int i = 0; i < 9; i)
{
int index = random.Next(dictionary.Count);
KeyValuePair<int, string> pair = dictionary.ElementAt(index);
label30.Text = (dictionary[pair.Key]);
}
for (int i = 0; i < 9; i)
{
int index = random.Next(dictionary.Count);
KeyValuePair<int, string> pair = dictionary.ElementAt(index);
label31.Text = (dictionary[pair.Key]);
}
}
private void button15_Click(object sender, EventArgs e)
{
}
**I would want my end result to be that the application displays 4 call numbers from the dictionary and lets the user select the corresponding description **
I've never worked with a data dictionary before so I'm sorry for the mess iv created but I'm trying to better my skills doing these challenges but this particular one is really giving me grief
uj5u.com熱心網友回復:
根據問題和評論,您的請求是允許用戶從字典中的一組預定義鍵值對中選擇一個值。我將假設您在 windows 表單上使用的組件是一個組合框。
Dictionary<int, string> dictionary = new Dictionary<int, string>();
dictionary.Add(000, "Genral Knowlege");
dictionary.Add(100, "philosophy & Psycology");
dictionary.Add(200, "Religion");
dictionary.Add(300, "Social Sciences");
dictionary.Add(400, "Languages");
dictionary.Add(500, "Science");
dictionary.Add(600, "Techynology");
dictionary.Add(700, "Arts & Recriation");
dictionary.Add(800, "Litrature");
dictionary.Add(900, "History and geography");
//Repeat this step for all your comboboxes
foreach (KeyValuePair<int, string> item in dictionary)
{
comboBox1.Items.Add(item.Value);
comboBox1.ValueMember = item.Value.ToString();
comboBox1.DisplayMember = item.Key.ToString();
comboBox1.SelectedIndex = 0;
}
現在,每個組合框都填充了字典值,用戶可以鍵入或從建議值中進行選擇。
uj5u.com熱心網友回復:
好的,這是我承諾的答案,但不知道評論中的詳細資訊。這是一個有效且快速的方法。你真的應該考慮兩次你所做的事情的目的。
注意代碼中的重復,可以不重復(在回圈中),但我暫時離開了你,希望更明確:
void Main()
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("000", "Genral Knowlege");
dictionary.Add("100", "philosophy & Psycology");
dictionary.Add("200", "Religion");
dictionary.Add("300", "Social Sciences");
dictionary.Add("400", "Languages");
dictionary.Add("500", "Science");
dictionary.Add("600", "Techynology");
dictionary.Add("700", "Arts & Recriation");
dictionary.Add("800", "Litrature");
dictionary.Add("900", "History and geography");
Form f = new Form();
var label28 = new Label { Top = 10, Left = 10, Width = 100 };
var label29 = new Label { Top = 40, Left = 10, Width = 100 };
var label30 = new Label { Top = 70, Left = 10, Width = 100 };
var label31 = new Label { Top = 100, Left = 10, Width = 100 };
var cmb28 = new ComboBox { Top = 10, Left = 130, DataSource = dictionary.ToList(), DisplayMember = "Key", ValueMember = "Key" };
var cmb29 = new ComboBox { Top = 40, Left = 130, DataSource = dictionary.ToList(), DisplayMember = "Key", ValueMember = "Key" };
var cmb30 = new ComboBox { Top = 70, Left = 130, DataSource = dictionary.ToList(), DisplayMember = "Key", ValueMember = "Key" };
var cmb31 = new ComboBox { Top = 100, Left = 130, DataSource = dictionary.ToList(), DisplayMember = "Key", ValueMember = "Key" };
f.Controls.AddRange(new Control[] { label28, label29, label30, label31, cmb28, cmb29, cmb30, cmb31 });
f.Load = (sender, args) => {
var randomTop4 = dictionary.ToList().OrderBy(d => Guid.NewGuid()).Take(4).ToList();
cmb28.SelectedValue = randomTop4[0].Key;
label28.Text = randomTop4[0].Value;
cmb28.SelectedValueChanged = (s, a) => {
var v = (string)(s as ComboBox).SelectedValue;
if (v != null)
{
label28.Text = dictionary[v];
}
};
cmb29.SelectedValue = randomTop4[1].Key;
label29.Text = randomTop4[1].Value;
cmb29.SelectedValueChanged = (s, a) =>
{
var v = (string)(s as ComboBox).SelectedValue;
if (v != null)
{
label29.Text = dictionary[v];
}
};
cmb30.SelectedValue = randomTop4[2].Key;
label30.Text = randomTop4[2].Value;
cmb30.SelectedValueChanged = (s, a) =>
{
var v = (string)(s as ComboBox).SelectedValue;
if (v != null)
{
label30.Text = dictionary[v];
}
};
cmb31.SelectedValue = randomTop4[3].Key;
label31.Text = randomTop4[3].Value;
cmb31.SelectedValueChanged = (s, a) =>
{
var v = (string)(s as ComboBox).SelectedValue;
if (v != null)
{
label31.Text = dictionary[v];
}
};
};
f.Show();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/355145.html
標籤:c# winforms data-dictionary
