C# - 打印字典 [英] C# - Print dictionary

查看:821
本文介绍了C# - 打印字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个包含DateTime和一个字符串的两个值的字典。
现在我想打印从字典到文本框的所有内容。
有人知道如何做到这一点。
我使用这个代码将字典打印到控制台:

  private void button1_Click(object sender,EventArgs e )
{
字典< DateTime,string> dictionary = new Dictionary< DateTime,string>();
dictionary.Add(monthCalendar1.SelectionStart,textBox1.Text);

foreach(KeyValuePair< DateTime,string>字典中的kvp)
{
//textBox3.Text + =(Key = {0},Value = {1} ,kvp.Key,kvp.Value);
Console.WriteLine(Key = {0},Value = {1},kvp.Key,kvp.Value);
}
}


解决方案

关闭这个

  foreach(KeyValuePair< DateTime,字符串> kvp在字典中)
{
// textBox3.Text + =(Key = {0},Value = {1},kvp.Key,kvp.Value);
Console.WriteLine(Key = {0},Value = {1},kvp.Key,kvp.Value);
}

更改此

  foreach(KeyValuePair< DateTime,string> kvp在字典中)
{
//textBox3.Text + =(Key = {0},Value = 1},kvp.Key,kvp.Value);
textBox3.Text + = string.Format(Key = {0},Value = {1},kvp.Key,kvp.Value);
}


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

解决方案

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

这篇关于C# - 打印字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆