none
Refresh or Reload the ListBox Items

    Question

  • Hello, 
    I hope someone can help me with. 

    I have a StringCollection "name" in which the entries are for my ListBox items. These I go through in a foreach loop and assign it to the corresponding item. When I first run the program, everything works just fine. However, when I click the button in which the new values ​​are updated for the items, the error "System.ArgumentOutOfRangeException" appears, that the value was out of range. This was understandable as I have a set breakpoint under the variable "index", which returns the value -1. 

    If I leave out "lstbWahl.Items.Clear ();" , the error message does not appear, but the ListBox items are piled on each other.

    private void Update(){ lstbWahl.Items.Clear(); foreach (String sc in name) { lstbWahl.Items.Add(sc); } } //My ListBox_Choice private void lstbWahl_SelectionChanged(object sender, SelectionChangedEventArgs e) { int index = lstbWahl.SelectedIndex;

    TextBox1.Text = textblock[index]; } //My Button_Click private void btnUpdate_Click(object sender, RoutedEventArgs e) { Update(); }





    • Edited by spry64 Sunday, April 06, 2014 6:14 PM
    Sunday, April 06, 2014 5:53 PM

Answers

  • I am going to suggest that you read about databinding in WPF.  Without using databinding you gain all the problems that you can encounter with WPF with none of the benefits.

    Once you have seen how databinding works it will be easy to create data and have it presented.

    I would suggest that you read the following :

    http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx


    Lloyd Sheen

    Sunday, April 06, 2014 10:12 PM
  • If you have set the ItemsSource property of the lstbWahl ListBox to the name StringCollection, the Items collection will also be cleared when you call the lstbWahl.Items.Clear() method.

    If you are adding items to the Items property of the ListBox directly, you should set the ItemsSource property to NULL before adding the first item:

            private void Update()
            {
                lstbWahl.ItemsSource = null;
    
                foreach (String sc in name)
                {
                    lstbWahl.Items.Add(sc);
                }
            }
    

    Monday, April 07, 2014 10:18 AM

All replies

  • "name" and "textblock" are objects of the class StringCollection
    Sunday, April 06, 2014 6:39 PM
  • Please help someone. How can I use ObservableCollection to solve this problem
    Sunday, April 06, 2014 7:04 PM
  • Can you show us the properties that you have defined and the XAML you are using?  You seem if you are using databinding for the (I guess its a listbox) you don't need the selectionchanged method.

    If you use an ObservableCollection your code will change quite a bit.

    Looking at StringCollection there are no properties name or textblock and if somehow you created them at least in the case of textblock that is the name of a control so you are in jeopardy of having naming problems. 


    Lloyd Sheen

    Sunday, April 06, 2014 8:27 PM
  • I defined a few variables globally. These two, for example,

    StringCollection name; 
    StringCollection textblock;


    And using it in a Function:

    name = new StringCollection();
    textblock = new StringCollection();

    Using it like this

    name.Add(text1);

    This is my XAML-Code. I have not changed there anything, cause I don't know how Databinding works. Yes it is a ListBox named lstbWahl

    <Window x:Class="USBSecure_V1._0.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="USBSecure Version 1.0" Height="450" Width="625">
        <Grid>
            <ListBox x:Name="lstbWahl" HorizontalAlignment="Left" Height="281" Margin="29,31,0,0" VerticalAlignment="Top" Width="186" SelectionChanged="lstbWahl_SelectionChanged"/>
            <TextBox x:Name="txtbAusgabe" HorizontalAlignment="Left" Height="351" Margin="263,31,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="313" VerticalScrollBarVisibility="Visible" KeyboardNavigation.AcceptsReturn="True"/>
            <Button x:Name="btnSpeichern" Content="Speichern" HorizontalAlignment="Left" Height="41" Margin="29,341,0,0" VerticalAlignment="Top" Width="186" Click="btnSpeichern_Click"/>
    
        </Grid>
    </Window>


    • Edited by spry64 Sunday, April 06, 2014 9:16 PM
    Sunday, April 06, 2014 9:01 PM
  • It is not necessarily to solve the problem with ObservableCollection. Mainly a way that leads to the solution.
    Sunday, April 06, 2014 9:08 PM
  • I am going to suggest that you read about databinding in WPF.  Without using databinding you gain all the problems that you can encounter with WPF with none of the benefits.

    Once you have seen how databinding works it will be easy to create data and have it presented.

    I would suggest that you read the following :

    http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx


    Lloyd Sheen

    Sunday, April 06, 2014 10:12 PM
  • If you have set the ItemsSource property of the lstbWahl ListBox to the name StringCollection, the Items collection will also be cleared when you call the lstbWahl.Items.Clear() method.

    If you are adding items to the Items property of the ListBox directly, you should set the ItemsSource property to NULL before adding the first item:

            private void Update()
            {
                lstbWahl.ItemsSource = null;
    
                foreach (String sc in name)
                {
                    lstbWahl.Items.Add(sc);
                }
            }
    

    Monday, April 07, 2014 10:18 AM