我正在使用现有的Web服务进行邮政编码搜索,然后将其存储在列表框中的值为:&#34; ID&#34;,&#34; Text&#34;,&#34;突出显示&#34;,&#34;光标&#34;,&#34;描述&#34;,&#34;下一步&#34;。我需要尝试访问特定的字符串值,即< em> ID&amp;下一步参数,稍后使用它进行验证。当我点击列表框时,我希望存储特定数据,然后访问我需要的两条信息。如何访问列表框中特定行的信息并在以后使用它?
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));
}
}
}
答案 0 :(得分: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];