Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse stack items in the listbox WP7

im currently working on a listbox which displays the recent sites that the user has navigated to. It is working fine, but i need the lists to be displayed stack reversed, like the newest list added should be ontop of the old item added. Im trying different methods but it doesnt seem to work,

This is my code for adding a item:

        list obj1 = new list();
        obj1.name = (string)webTab1.InvokeScript("eval", "document.title.toString()");
        obj1.phone = menuURL.Text;
        listBox1.Items.Add(obj1);

this code will keep adding items one after the other, which isnt suitable

Much appreciated

like image 912
rsharma Avatar asked Jan 18 '26 05:01

rsharma


2 Answers

This is the WPF version of what you are asking for but its code should be identical for Windows Phone. In this example you will see that the listbox is bound to a collection that is loaded. Then when you click reverse button the items in the collection are reversed.

    <Grid>
        <Button Click="Button_Click" Content="Click to reverse" Width="100" Height="100" Margin="298,106,105,105"></Button>
        <ListBox Name="lb1" HorizontalAlignment="Left" ItemsSource="{Binding urls}" Width="255">    


        </ListBox>
    </Grid>

public partial class MainWindow : Window
{
    public ObservableCollection<String> urls { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        urls = new ObservableCollection<string>();
        this.DataContext = this;
        loadURLsFirstTime();


    }

    public void loadURLsFirstTime()
    {
        urls.Add("www.First.com");
        urls.Add("www.Second.com");
        urls.Add("www.Third.com");
        urls.Add("www.Fourth.com");

    }

    public void reverseUrls()
    {
        Stack<String> stack1 = new Stack<string>();
        foreach (String item in urls)
        {
            stack1.Push(item);
        }
        urls.Clear();
        foreach (String item in stack1)
        {
            urls.Add(item);
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        reverseUrls();
    }
}
like image 194
Anthony Russell Avatar answered Jan 19 '26 19:01

Anthony Russell


listBox1.Items.Insert(0, obj1);

instead

listBox1.Items.Add(obj1);

like image 45
ibogolyubskiy Avatar answered Jan 19 '26 20:01

ibogolyubskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!