【问题标题】:How to get particular infomation stored in list如何获取存储在列表中的特定信息
【发布时间】:2017-05-23 10:32:29
【问题描述】:

我正在使用一个现有的网络服务,它进行邮政编码搜索,然后将值存储在列表框中:“ID”、“文本”、“突出显示”、“光标”、“描述”、“下一个"。 我需要尝试访问一个特定的字符串值,即 ID & Next 参数,并在以后使用它进行验证。当我单击列表框时,我希望存储特定数据,然后访问我需要的两条信息。如何访问列表框特定行的信息并在以后使用?

try
{
    int myMaxResultValue = (int)nud_MaxResults.Value;
    int myMaxSuggestValue = (int)nud_MaxSuggestions.Value;
    findResults = objBvSoapClient.CapturePlus_Interactive_Find_v2_10("Dak4-KZ62-AAdd87-X55", txt_Search.Text, txt_LastId.Text, cb_SearchFor.Text, text_Country.Text, text_LanguagePreference.Text, myMaxResultValue, myMaxSuggestValue);

    if (txt_Search.Text.Length <= 2)// if less than two letters are entered nothing is displayed on the list.
    {
        ls_Output.Items.Clear();// Clear LstBox
        ls_Output.Items.Add(String.Format(allDetails, "ID", "Text", "Highlight", "Cursor", "Description", "Next"));
        MessageBox.Show("Please enter more than 2 Chars!!");
    }
    else if (txt_Search.Text.Length >= 3)// if greater than or equal to 3 letters in the search box  continue search.
    {
        // Get Results and store in given array.
        foreach (var items in findResults)
        {  //Loop through our collection of found results and change resulting value.
            ls_Output.Items.Add(String.Format(allDetails, items.Id, items.Text.ToString(), items.Highlight, items.Cursor, items.Description, items.Next));
        }
    }
}

【问题讨论】:

  • 为什么不使用 PostCodeSearchResult 类的对象?
  • @devil_coder 我已经从 mycu rrent 服务获得了地址,我需要做的是找出如何访问列表框特定行的信息并在以后使用它
  • 你试过 int index = listBox1.FindString(searchString); ?

标签: c# winforms collections


【解决方案1】:

作为旁注,您的 string.Format 缺少字符串中的变量。应该是这样的

int id = 30;
string text = "Hello";
string.Format("This is the ID {0}. Here is some text {1}.", id, text);

输出将是“这是 ID 30。这是一些文本 Hello。”。

要回答您的问题,您必须对其进行解析以提取您想要的部分。您可以使用 regex.split 来执行此操作。例如,如果它是用空间分隔的,你可以做这样的事情

string[] data = Regex.Split(operation, @"\s+");

那你就可以这样访问了

string required = data[3];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 2015-12-13
    相关资源
    最近更新 更多