简体   繁体   English

C#如何为列表框中的选定项目运行一些代码?

[英]C# How do I run some code for the selected item in a listbox?

I have a listbox of names, I want to click one of the names so that it is highlighted and then click a button that runs some code for the selected item. 我有一个名称列表框,我想单击其中一个名称,使其突出显示,然后单击一个为所选项目运行一些代码的按钮。 How do I call this selected item? 我怎么称呼这个选择的项目?

    private void btnEcho_Click(object sender, EventArgs e)
    {
         listbox1.SelectedItem......
    }

Many thanks 非常感谢

列表框不是很直观,因为它包含对象而不是诸如ListItem之类的东西,但是如果您只想要文本,则可以执行以下操作:

string selectedText = listbox1.SelectedItem.ToString();
string str = listbox1.SelectedValue.ToString();

here you have the what value(name) selected. 在这里,您选择了什么值(名称)。

if(str == null || str == string.empty) return;

and so on. 等等。 you can do whatever you want; 你想做什么,就可以做什么; goodluck 祝好运

String s = listbox1.SelectedItem.Value.ToString();

不要忘记进行空检查,因为如果您的列表为空或未选择任何值,这将引发错误。

Listbox1.SelectedItem gets the actual selected item. Listbox1.SelectedItem获取实际的选定项目。 You can then further call from SelectedItem to get other properties/methods such as SelectedItem.Text or SelectedItem.Value 然后,您可以从SelectedItem进一步调用以获取其他属性/方法,例如SelectedItem.TextSelectedItem.Value

If you wanted to make it all happen upon selection from the listbox (instead of pressing a button), you could just add a SelectedIndexChanged event for Listbox1 (and in ASP.NET make sure that AutoPostBack is set to TRUE) 如果要使所有操作都在列表框中进行选择(而不是按按钮),则可以为Listbox1添加SelectedIndexChanged事件(并在ASP.NET中确保将AutoPostBack设置为TRUE)。

You're question is not clearly to me. 您的问题对我来说并不明确。

Example, you have a listbox of three items: A, B and C. You have, as you do with your example, a click-event. 例如,您有一个包含三个项目的列表框:A,B和C。与示例一样,您拥有一个点击事件。 In that click-event you can use the switch-statement to handle some code for each item: 在该单击事件中,您可以使用switch语句为每个项目处理一些代码:

switch (listbox1.SelectedItem)
{
  case "A":
       // Code when select A
       break;
  case "B":
       // Code when select B
       break;
  ... (and so on).
}

Code is an example and not tested. 代码是一个示例,未经测试。 See switch for more information. 请参阅开关以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取列表框中所选项目的编号(C#) - How do I get the number of the item selected in a listbox (C#) C#如何使用代码检查列表框中的选定项目? - C# How do I check a selected item in a listbox using code? c#如何获取列表框中的选定项目是否已更改。 ? - c# how do i get check if a selected item in a listbox has changed. ? 如何检查C#中未选择ListBox的选定值? - How do I check if selected value of the ListBox is not selected in C#? C# - 如何找到列表框项的数据库键? - C# - How do I find the database key of a Listbox item? 如何单击一个按钮,该按钮将从ListBox中删除一个项目,而该列表中包含C#中ListBox的信息? - How do I click a button that will remove an item from a ListBox and the List that holds the information for the ListBox in C#? 列表框中的C#XML。 如何获得选择的项目? - C# XML in listbox. How to get which item is selected? 如何将列表框中的选定项目添加到列表 <string> 在C#中 - how to add the selected item from a listbox to list <string> in c# C#Xaml如何获取列表框所选项目的文本框值 - C# Xaml How to get listbox selected item textbox value 如何将listBox选中的项目作为KeyValuePair <string, string> 在C#? - How to get listBox selected item as KeyValuePair<string, string> in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM