ListBox Sample Code C#


What's the syntax for a ListBox if I want to create a statement like this:

                <ListBoxItem Content="Option 1" />
                <ListBoxItem Content="Option 2" />
                <ListBoxItem Content="Option 3" />
                <ListBoxItem Content="Option 4" />

if (Condition_1 && Condition_2 && 'ListBoxItem Option3 is selected')
else if (Condition_2 && Condition_3 && 'ListBoxItem Option 4 is selected')
else if (Condition_3 && Condition_4 && 'ListBoxItem Option 2 is selected')

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

            if ((true) && (true) && this.lstDemo.SelectedIndex == 0)
                Console.Write("Option 1 is selected plus conditions are true");
            else if ((true) && (true) && this.lstDemo.SelectedIndex == 1)
                Console.Write("Option 2 is selected plus conditions are true");
            else if ((true) && (true) && this.lstDemo.SelectedIndex == 2)
                Console.Write("Option 3 is selected plus conditions are true");
            else if ((true) && (true) && this.lstDemo.SelectedIndex == 3)
                Console.Write("Option 4 is selected plus conditions are true");

Open in new window

Replace the "(true)" conditions with your conditions. Then the console.write section with the code you want to execute in its place.
Todd GerbertIT ConsultantCommented:
There are a number of ways to check which item in the ListBox is selected, e.g. use ListBox.SelectedIndex to know the index of the selected item and if(listBox.SelectedIndex == 0)... I compared the Content of the ListBoxItem in this example, but there's probably a better way depending on what's in the list box.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
		public MainWindow()

		private void button1_Click(object sender, RoutedEventArgs e)
			// Get the selected item in the listbox
			ListBoxItem selectedItem = listBox1.SelectedItem as ListBoxItem;
			if (selectedItem != null)
				// Get the text of the selected itm
				string selectedText = selectedItem.Content.ToString().ToLower();

				if (checkBox1.IsChecked.Value && checkBox2.IsChecked.Value && selectedText == "option 1")
					MessageBox.Show("Statement 1");
				else if (checkBox2.IsChecked.Value && checkBox3.IsChecked.Value && selectedText == "option 4")
					MessageBox.Show("Statement 2");
				else if (checkBox3.IsChecked.Value && checkBox4.IsChecked.Value && selectedText == "option 2")
					MessageBox.Show("Statement 3");
					MessageBox.Show("Statement N");

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Above 2 solutions would worked, but you can also use switch case statement.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Todd GerbertIT ConsultantCommented:
No, you can't use the switch statement in this case (technically you can, but it would be silly to do so and would make the code difficult to read).
>>but it would be silly
yes, but depends on your application,  but it would be silly _if_ you have a code system like this;
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            switch (listBox1.SelectedIndex) 
                case 0:{
                        //if (Condition_1 && Condition_2)   { }  else
                        this.Text = listBox1.SelectedItem.ToString(); break;
                case 1:{
                        //if (Condition_3 && Condition_4)
                        this.Text = listBox1.SelectedItem.ToString(); break;
                case 2:{
                        //if (Condition_3 && Condition_4)
                        this.Text = listBox1.SelectedItem.ToString(); break;
                        this.Text = "nothing"; break;

Open in new window

It would be simpler in this way;
string globalvar;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            if (Condition_1 && Condition_2){
                this.Text = "a";
                globalvar = "a";
            else if (Condition_3 && Condition_4){
                this.Text = "b";
               globalvar = "b";
            else if (Condition_5 && Condition_6)
                this.Text = "c";
                globalvar = "c";
                this.Text = "nothing";
                globalvar = "x";

Open in new window

But really your code does good too.
petaganayrAuthor Commented:
I chose this one because I am an absolute beginner and this solution's easy to read and understand.

Thank you for everyone's sample code.
Todd GerbertIT ConsultantCommented:
The only issue I have with such a switch statement is that it's dependant on being able to compare the SelectedIndex value, which might not be the case (e.g. the ListboxItems are added dynamically at run-time, so "Option 1" might be index 2) - which is why I specifically pointed out that there are a number of ways to determine which item is selected, but demonstrated comparing their Content.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.