Arrays 如果用户选择的项目少于3项,如何将此作为if语句? private void CheckoutButton\u单击(对象发送者,路由目标) { 字符串[]chkItems=listBox1.SelectedItems.OfType().ToArray(); textBlock1.Text=chkItems[0]+Environment.NewLine+chkItems[1]+Environment.NewLine+chkItems[2]+ 环境新线; }
我正在创建的应用程序中有这段代码。我有一个列表框,其中显示了一些选项,它们最多可以选择3个项目,但如果它们不选择3个项目,我的应用程序就会崩溃。有没有一种方法可以使这个if语句成为允许他们选择1、2甚至3项的语句?您可以通过Arrays 如果用户选择的项目少于3项,如何将此作为if语句? private void CheckoutButton\u单击(对象发送者,路由目标) { 字符串[]chkItems=listBox1.SelectedItems.OfType().ToArray(); textBlock1.Text=chkItems[0]+Environment.NewLine+chkItems[1]+Environment.NewLine+chkItems[2]+ 环境新线; },arrays,if-statement,listbox,Arrays,If Statement,Listbox,我正在创建的应用程序中有这段代码。我有一个列表框,其中显示了一些选项,它们最多可以选择3个项目,但如果它们不选择3个项目,我的应用程序就会崩溃。有没有一种方法可以使这个if语句成为允许他们选择1、2甚至3项的语句?您可以通过foreach循环来完成它 private void CheckoutButton_Click(object sender, RoutedEventArgs e) { string[] chkItems = listBox1.SelectedIt
foreach
循环来完成它
private void CheckoutButton_Click(object sender, RoutedEventArgs e)
{
string[] chkItems = listBox1.SelectedItems.OfType<string>().ToArray();
textBlock1.Text = chkItems[0] + Environment.NewLine + chkItems[1] + Environment.NewLine + chkItems[2] +
Environment.NewLine;
}
string[]chkItems=listBox1.SelectedItems.OfType().ToArray();
textBlock1.Text=string.Empty;
foreach(chkItems中的字符串项)
{
textBlock1.Text=textBlock1.Text+item+Environment.NewLine;
}
string[] chkItems = listBox1.SelectedItems.OfType<string>().ToArray();
textBlock1.Text=string.Empty;
foreach(string item in chkItems)
{
textBlock1.Text=textBlock1.Text + item +Environment.NewLine;
}