1
votes

edit edit: I figured out the issue. We were putting the : inbetween the case and the value, it needed to be after. example: case "General" :

Thanks for your time and input everyone.

Original:

This has to be a lot more simple than I'm attempting to make it. Simply I have a listbox with say...6 different "items"(General/oSnaps/blah/blah/blah), when someone clicks one of the items in the listbox, I want it to change Panels. I have 6 different Panels(panel1,panel2,etc) stacked on top of each other, on load only the first one set to visible.

I'm sure it's just a matter of setting which Panel is visible, but how do I actually link that to the "items" in the listbox? SelectedIndexChanged is the answer I presume, but I'm very very new to programming.

MSDN only demonstrates how to switch between items in different listbox's.

Any help would be appreciated.

Edit: here's my current code

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) {

           string curItem = listBox1.SelectedItem.ToString(); 

           curItem = listBox1.SelectedItem.ToString(); 
            MessageBox.Show(curItem);

           switch(curItem)
        {
        case : "General"   //error here is: Only assignment, call,increment,await,and new object expressions can be used as a statement
        panel1.Visible = false;
        panel2.Visible = true;
        break;
        case : "E-Snaps" // same as above
        panel2.Visible = false;
        panel3.Visible = true;
        break;
        case : "blah" // same as above
        panel3.Visible = false;
        panel4.Visible = true;
        }
        }

Clicking on the different items in the listBox does bring up a message box with the correct content(General/E-Snaps/blah/etc). So it's reading it obviously, how do we make it switch panels now?

Shouldn't I be able to do something like this also?

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) {

           string curItem = listBox1.SelectedItem.ToString();

           curItem = listBox1.SelectedItem.ToString();

            if curItem = "General"
            {
                panel1.Visible = true;
            }

}

Not that I can get either to work...but it seems logical. If I could get some form of string/bool conversion going...

2

2 Answers

1
votes

you could start by using a switch statement on the selectedindex changed

so it would look like this

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    // Get the currently selected item in the ListBox. 
    string curItem = listBox1.SelectedItem.ToString();

    switch(curItem)
    {
        case "blah": 
            panel1.visible = false;
            panel2.visible = true;
            break;
        case "blah": 
            panel2.visible = false;
            panel3.visible = true;
            break;
        case "blah": 
            panel3.visible = false;
            panel4.visible = true;
            break;
    }
}

and so on, I hope this helps

Explanation : "blah" is what ever value you want to eval so if I was doing fruits

"blah" would = apple or bannana or pineapple etc

1
votes

I would use an IValueConverter in the binding -- that will let you bind each panel's visibility directly to the ListBox.SelectedIndex.

<Grid>
    <Grid.Resources>
        <local:IndexToVisibilityConverter x:Key="IndexToVisibilityConverter" />
    </Grid.Resources>
    <ListBox x:Name="listBox"> ... </ListBox>

    <Border x:Name="panel1" 
            Visibility="{Binding ElementName=listBox,
                                 Path=SelectedIndex,
                                 Converter={StaticResource IndexToVisibilityConverter}
                                 ConverterParameter='0'}"
    />
    <Border x:Name="panel2" 
            Visibility="{Binding ElementName=listBox,
                                 Path=SelectedIndex,
                                 Converter={StaticResource IndexToVisibilityConverter}
                                 ConverterParameter='1'}"
    />
</Grid>

The "IndexToVisibilityConverter" should return "Visibility.Visible" if the index matches the parameter:

public class IndexToVisibilityConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((int)value == int.Parse((string)parameter) ? Visibility.Visible : Visibility.Collapsed);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Alternatively, note that if you want to use transitions (like cross-fade, slide in/out, etc), then you'll probably want to use an event trigger on the SelectionChanged event, along with a Storyboard or VisualState change.