C# 找不到类型或命名空间名称“列表项”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/925106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type or namespace name 'List Item' could not be found
提问by
I am making a code to move the elements of a list box up and down. For that, I have made two buttons to move the element up (Move Up button) and down (Move Down button). Here is my code:
我正在编写一个代码来上下移动列表框的元素。为此,我制作了两个按钮来向上移动元素(向上移动按钮)和向下移动(向下移动按钮)。这是我的代码:
if (m_lbOPFfiles.SelectedIndex != m_lbOPFfiles.Items.Count && m_lbOPFfiles.SelectedIndex != -1)
{
ListItem item = m_lbOPFfiles.SelectedItem;
int index = m_lbOPFfiles.SelectedIndex;
m_lbOPFfiles.Items.RemoveAt(index);
lstResdetails.Items.Insert(index + 1, item);
}
Now I am getting the namespace error for ListItem. Can anyone help me rectify it?
现在我收到 ListItem 的命名空间错误。任何人都可以帮我纠正它吗?
回答by thr
I suspect you're missing the appropriate using-directive at the top of your .cs-file, which would be using System.Web.UI.WebControls;. You might also be missing the correct assembly (though I doubt it) and should add a reference to the System.Web assembly in your project.
我怀疑您在.cs 文件的顶部缺少适当的using-directive,这将使用 System.Web.UI.WebControls;. 您可能还缺少正确的程序集(尽管我对此表示怀疑)并且应该在您的项目中添加对 System.Web 程序集的引用。
回答by Jon Skeet
Well, you haven't said what type of application you're writing - Windows Forms? ASP.NET? WPF? Assuming it's ASP.NET, you need:
好吧,您还没有说您正在编写什么类型的应用程序——Windows 窗体?ASP.NET?WPF?假设它是 ASP.NET,你需要:
using System.Web.UI.WebControls;
at the top of your code. If it's WPF you might want:
在代码的顶部。如果是 WPF,您可能需要:
using System.Windows.Documents;
EDIT: If it's Windows Forms then there isn't a ListItem
class. ListBox.SelectedItem
returns object
, not ListItem
. What aspect of a ListItem
type would you want to use with the ListBox
? (I'm assuming you're using a ListBox
.) You can add items to ListBox.Items
just as objects.
编辑:如果是 Windows 窗体,则没有ListItem
类。ListBox.SelectedItem
返回object
,不是ListItem
。ListItem
您希望与ListBox
?一起使用类型的哪个方面?(我假设您使用的是ListBox
。)您可以将项目添加ListBox.Items
为对象。
回答by OregonGhost
If you are using Visual Studio, you can just move the cursor onto the identifier and a smart tag will appear. Open it by pressing Ctrl+. or by clicking on it and select whether you want to add the using directive or use a fully qualified name.
如果您使用的是 Visual Studio,只需将光标移动到标识符上,就会出现一个智能标记。按 Ctrl+ 打开它。或者通过单击它并选择是要添加 using 指令还是使用完全限定名称。
On the other hand, if you're using Visual Studio, Intellisense would have told you in its own way that the identifier is currently not available :)
另一方面,如果您使用的是 Visual Studio,Intellisense 会以自己的方式告诉您标识符当前不可用:)
回答by Echeban
In Windows Forms, if you add objects with ListBox.Items.Add(Object), where Object is an object that you created, the ListBox will be populated by the name of the objects, not by text. Unless you add ListBox.Items.Add(Object.Text), in which case you get the text in the ListBox but you loose the rest of the information in the objects. What we want is a way to emulate what the ListItem class implements in ASP.
在 Windows 窗体中,如果您使用 ListBox.Items.Add(Object) 添加对象,其中 Object 是您创建的对象,则 ListBox 将填充对象的名称,而不是文本。除非您添加 ListBox.Items.Add(Object.Text),在这种情况下,您会在 ListBox 中获得文本,但会丢失对象中的其余信息。我们想要的是一种模拟 ListItem 类在 ASP 中实现的内容的方法。
回答by CodeEmpower
You are using windows forms, this is why you were not able to access the ListItem Class in the Controls Name space, do the following:
-Add "PresentationFrameWork" (.NET Reference) as a reference to your solution
- add using System.Windows.Controls;
to your class file
您正在使用 Windows 窗体,这就是您无法访问控件名称空间中的 ListItem 类的原因,请执行以下操作: -添加“PresentationFrameWork”(.NET 参考)作为对您的解决方案的引用 - 添加 using System.Windows.Controls;
到您的类文件