2 c#
我正在编写代码来上下移动列表框的元素。为此,我制作了两个按钮来向上移动元素(向上移动按钮)和向下移动元素(向下移动按钮)。这是我的代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
现在我收到 ListItem 的命名空间错误。谁能帮我纠正它吗?
好吧,您还没有说您正在编写什么类型的应用程序 - Windows 窗体?ASP.NET?世界和平基金会?假设它是 ASP.NET,您需要:
using System.Web.UI.WebControls;
Run Code Online (Sandbox Code Playgroud)
在代码的顶部。如果是 WPF,您可能需要:
using System.Windows.Documents;
Run Code Online (Sandbox Code Playgroud)
编辑:如果它是 Windows 窗体,则没有类ListItem。ListBox.SelectedItem返回object,不ListItem。ListItem您希望将类型的哪些方面与 一起使用ListBox?(我假设您使用的是ListBox。)您可以将项目添加ListBox.Items为对象。