Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中在运行时修改listBox项#_C#_Fonts_Listbox_Runtime_Items - Fatal编程技术网

C# 在c中在运行时修改listBox项#

C# 在c中在运行时修改listBox项#,c#,fonts,listbox,runtime,items,C#,Fonts,Listbox,Runtime,Items,我编写了一段代码,在运行时将一些项添加到列表框中,现在我不知道如何访问这些项的属性(特别是FontSize) 这里有一些代码 for (int i = 0; i < list.Count; i++) { trainerRoutine Item = list[i]; routines_lst.Items.Add(Item.name); } for(int i=0;i

我编写了一段代码,在运行时将一些项添加到列表框中,现在我不知道如何访问这些项的属性(特别是FontSize)

这里有一些代码

 for (int i = 0; i < list.Count; i++)
      {
          trainerRoutine Item = list[i];
          routines_lst.Items.Add(Item.name);
      }
for(int i=0;i
考虑到像
ListBox
项一样,您添加了一个
项.Name
,我假设它是一个简单的字符串,我想说,对于
Font
属性,您不应该查看项的内部,而应该查看
ListBox
本身

很难看出“trainerRoutine”与字体大小有什么关系。通常,列表框存储对象。您正在使用Item.name将字符串放入Items集合。这很麻烦,您不能那么容易地从该字符串返回到原始对象。而且它不是必需的,只需添加项即可。您必须做的一件事是重写Item类的ToString()方法,这就是ListBox用来生成可读字符串的方法。因此:

class DontKnowWhat {
   // properties and methods
   //...
   public override string ToString() {
      return name;
   }
}
现在,您只需将ListBox.Items集合强制转换为DontKnowWhat,即可从该集合中获取原始对象



如果您正在讨论更改项目(crystal ball解释编号2)的字体大小,则需要将ListBox的DrawMode属性设置为DrawMode.OwnerDrawVariable。MSDN文章中有一个很好的例子。

有关类似问题,请参阅;您可以处理DrawItem或MeasureItem事件;添加项目时无法执行此操作。我认为操作是在如何更改列表框中项目的字体大小之后进行的;与ASP.Net不同,WinForms(假设它是WinForms)的实现方式稍微复杂一些。我没有添加其他详细信息,只是因为它们没有用处。重点是我需要为每个项目设置不同的大小,这可能吗?如果您使用
wpf
,这是可能的,但是您需要处理
项目模板
以及使用
GC.MeasureString