【问题标题】:How do i add Point coordinate as item to listView1?如何将点坐标作为项目添加到 listView1?
【发布时间】:2014-01-07 16:50:45
【问题描述】:

这是代码:

for (int i = 0; i < CloudEnteringAlert.PointsInt.Count; i++)
{
    listView1.Items.Add(CloudEnteringAlert.PointsInt[i]);
}

PointsInt 是一个列表 例如在索引 0 我看到: {X = 120 , Y = 130}

在使用 listView 之前,我使用了 listBox,它工作正常,我可以添加它。 但是当我将其更改为 listView 时,我得到了错误:

错误 2 参数 1:无法从 'System.Drawing.Point' 转换为 'System.Windows.Forms.ListViewItem'

最后我应该在 listView 中看到例如在索引 0 中:X = 128 , Y = 130

【问题讨论】:

  • 您可以将实际的ListViewItem设置为每个值的文本,然后将PointsInt对象添加到ListViewItem标签中,以便稍后检索,如果您以后需要检索它,当然:)
  • 更正:当我使用 listBox 时,我在索引 0 中看到例如:120、130,这就是 listView 应该是这样的,但我得到了错误。

标签: c# winforms listview


【解决方案1】:

试试:

listView1.Items.Add(CloudEnteringAlert.PointsInt[i].ToString());

编辑:
正如@LokiSinclair 还建议的那样,如果您可以访问.X.Y 成员,那么您可以这样做:

listView1.Items.Add(CloudEnteringAlert.PointsInt[i].X.ToString() + ", " + CloudEnteringAlert.PointsInt[i].Y.ToString());

【讨论】:

  • gnarlybracket 我试过你的答案,我也这样做了 string t = CloudEnteringAlert.PointsInt[i].ToString();并添加了变量 t。我的解决方案和您的解决方案中的问题是它添加: {X = 120 , Y = 130} 并且我希望它只添加: 120 , 130
  • @user3163653 是否有可用于CloudEnteringAlert.PointsInt[i].X.Y 选项?
  • 如果它继承自 Point,那么您将可以访问 .X 和 .Y 成员(如 @user3163653 所述)。在这种情况下,您可以使用:CloudEnteringAlert.PointsInt[i].X + ", " + CloudEnteringAlert.PointsInt[i].Y
猜你喜欢
  • 1970-01-01
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多