Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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
将ListBoxView的选定值设置为字符串(C#)_C#_.net_Windows_String - Fatal编程技术网

将ListBoxView的选定值设置为字符串(C#)

将ListBoxView的选定值设置为字符串(C#),c#,.net,windows,string,C#,.net,Windows,String,当在ListBoxView中选择一个值时,如何将选择存储为字符串/变量,这样我就可以调用另一个将使用此字符串执行操作的方法?如果ListBoxView中的项目都是字符串,您可以使用 System.Windows.Forms.ListBox lb= new ListBox(); lb.SelectedItem.ToString(); 否则,我们需要知道您添加到列表框中的项目的类型。如果您使用列表框获取所选项目,如下所示: string item = listBox1.Se

当在ListBoxView中选择一个值时,如何将选择存储为字符串/变量,这样我就可以调用另一个将使用此字符串执行操作的方法?

如果ListBoxView中的项目都是字符串,您可以使用

System.Windows.Forms.ListBox lb= new ListBox();
            lb.SelectedItem.ToString();

否则,我们需要知道您添加到列表框中的项目的类型。

如果您使用列表框获取所选项目,如下所示:

string item = listBox1.SelectedItem.ToString();
如果使用ListView根据MultiSelect属性获取选定项:

  • 如果MultiSelect属性=false,请使用此代码

    string itemName = listView.SelectedItems[0].Name;
    
  • 如果MultiSelect属性=true,则使用此代码

    foreach (ListViewItem item in ltvMainMenu.SelectedItems)
    {
       string itemName = item.Name;    
    }
    

  • 没有listboxview控件。你在用什么listview或listbox?@Reniuz对不起,这是listview