Home > OS >  Match the columns using a data dictionary in C# winForm
Match the columns using a data dictionary in C# winForm

Time:11-10

This is suppose to be a match the columns type interphase but as you can see I have the keys and descriptions swapped and it doesn't really preform any function I'm not sure how to format the question so that it works

 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

CodePudding user response:

Based on the questions and comments, your request is to allow a user selecting a value from a set of predefined key-value pairs within a dictionary. I will assume that the component you are using on the windows form is a combobox.

        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;
        }

Now, each combobox is filled with the dictionary values, the user can either type or choose from the suggested value.

CodePudding user response:

Ok here is the answer I promised without knowing the details in comment. This is a working and quick throw at it. You should really think about the purpose of what you are doing twice.

Note the repetition in code, it could be made not to repeat (in a loop) but I left that you for now and wanted to be more explicit:

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();
}
 
  • Related