I have made a dictionary which contains two values: a DateTime
and a string
.
Now I want to print everything from the dictionary to a Textbox. Does anybody know how to do this?
I have used this code to print the dictionary to the console:
private void button1_Click(object sender, EventArgs e) { Dictionary<DateTime, string> dictionary = new Dictionary<DateTime, string>(); dictionary.Add(monthCalendar1.SelectionStart, textBox1.Text); foreach (KeyValuePair<DateTime, string> kvp in dictionary) { //textBox3.Text += ("Key = {0}, Value = {1}", kvp.Key, kvp.Value); Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } }
Since a dictionary has a dynamic structure, you can add new key-value pairs to it at any time.
The keys of a dictionary can be any kind of immutable type, which includes: strings, numbers, and tuples: mydict = {"hello": "world", 0: "a", 1: "b", "2": "not a number" (1, 2, 3): "a tuple!"}
Just to close this
foreach (KeyValuePair<DateTime, string> kvp in dictionary) { //textBox3.Text += ("Key = {0}, Value = {1}", kvp.Key, kvp.Value); Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); }
Changes to this
foreach (KeyValuePair<DateTime, string> kvp in dictionary) { //textBox3.Text += ("Key = {0}, Value = {1}", kvp.Key, kvp.Value); textBox3.Text += string.Format("Key = {0}, Value = {1}", kvp.Key, kvp.Value); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With