0

我有一个包含字符串和双精度的字典 myDataItems。我将值从 2 个文本框传递给字典,1 个文本框传递字符串值,另一个传递双精度值。我想知道如何在我用于输出目的的第三个文本框(txtOutput)中显示字典项目?

public partial class Form1 : Form 
{
   private Dictionary<string, double> myDataItems = new Dictionary<string, double>();
   private string dataName;
   private double dataCost;

   private void dataItemSend_Click(object sender, EventArgs e)
   {
      dataName = dataNameInput.Text;
      dataCost = Convert.ToDouble(dataCostInput.Text);
      myDataItems.Add(dataName, dataCost);
      txtOutput.Text = myDataItems.ToString();
   }
}
4

2 回答 2

1

一个简单的方法:

var lines = myDataItems.Select(kv => kv.Key + ": " + kv.Value.ToString());
txtOutput.Text = string.Join(Environment.NewLine, lines);

记得添加using System.Linq;

于 2012-12-29T14:52:11.590 回答
0
txtOutput.Lines = myDataItems.Select(x => x.Key + " : " + x.Value).ToArray();
于 2012-12-29T15:51:00.123 回答