Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:11:10+00:00 2026-05-24T11:11:10+00:00

Sorry if the Syntax is off, i typed this on Notepad, (temp. issues with

  • 0

Sorry if the Syntax is off, i typed this on Notepad, (temp. issues with computer with VS)

i have a class Movie with a Title property, i have

 Dim movieList = New ObservableCollection(of Movie) 

 Private Sub SelectMovie(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)

  For Each m As Movie in movieList
    If lb1.SelectedItem = "New" Then
      m.Add(New Movie()) 
    End If
   Next

 End Sub 

movieList was initialized with temp values, last one being “New”.
In the constructor i iterated through movielist and put stuff in the listbox.

lb1.Items.Add(m)

In the MainWindow.xaml i have

 <Grid Name="moviePage" > 
   <ListBox Name="lb1" SelectionChanged="SelectMovie">
 </Grid>

I’m aware that this approach is wrong as I’m modifying a list while using it, but i can’t seem to find a way around this.

The idea is to simply have the listbox show a bunch of movies, with the ability to add a new movie if “New” is selected from the list.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:11:11+00:00Added an answer on May 24, 2026 at 11:11 am

    Here’s a working code sample:

    public MainWindow()
    {
        InitializeComponent();
    
        _movies = new ObservableCollection<Movie>(
            new[]
            {
                new Movie { Name = "Foo" },
                new Movie { Name = "Bar" },
                new Movie { Name = "(New)" },
            });
        lb1.ItemsSource = _movies;
    }
    
    ObservableCollection<Movie> _movies;
    
    private void SelectMovie(object sender, SelectionChangedEventArgs e)
    {
        var selectedMovie = lb1.SelectedItem as Movie;
        if (selectedMovie == null) return;
    
        if (selectedMovie.Name == "(New)")
        {
            var newMovie = new Movie { Name = "Untitled" };
            _movies.Insert(_movies.Count - 1, newMovie);
            lb1.SelectedItem = newMovie;
            e.Handled = true;
        }
    }
    

    Having said that, I don’t believe this is the right approach for what you’re doing. I think you’re better off changing the template for the ListBox so that it shows the list of items it’s bound to, and then a “New” button or link at the bottom (outside of the list).

    Here’s a very simple example:

    <ListBox x:Name="lb1" DisplayMemberPath="Name" SelectionChanged="SelectMovie">
        <ListBox.Template>
            <ControlTemplate>
                <StackPanel>
                    <ItemsPresenter />
                    <Button Click="AddMovie>New Movie</Button>
                </StackPanel>
            </ControlTemplate>
        </ListBox.Template>
    </ListBox>
    

    So now you have a ListBox which includes a button to create a new movie. In your AddMovie event handler you can add a new Movie instance to the list and select it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for this question but I am stuck. I have folowing syntax: class xx
Let's say I have an array of lots of values (C++ syntax, sorry): vector<double>
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry for the vague title, but I'm not sure what this is called. Say
Sorry for my inaccurate syntax. New to this. I need to select part numbers
what's wrong with this syntax? sorry for the newbie question. source: Level::Level() { NintyDegreeDirections[4]
what's wrong with this syntax? sorry for the newbie question. source: Level::Level() { NintyDegreeDirections[4]
(Sorry if it's a trivial question.) I have documents that looks like this (Python
Sorry, but I can't find answer to this. If I have the following line
Sorry if this has been asked but can't find anything. I have a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.