0

I have 2 classes: Multimedia and MultimediaList and 2 windows: my main window and the input window where I insert my data in order to add new items to the listbox.

my Multimedia class looks like this:

public class Multimedia : INotifyPropertyChanged
{
    public enum MediaType
    {
        CD,
        DVD
    };

    private string _title;
    private string _artist;
    private string _genre;
    private MediaType _type;

    public Multimedia(string title, string artist, string genre, MediaType type)
    {
        _title = title;
        _artist = artist;
        _genre = genre;
        _type = type;
    }

    public String Title
    {
        get { return this._title; }
        set { this._title = value; }
    }
    public String Artist
    {
        get { return this._artist; }
        set { this._artist = value; }
    }
    public String Genre
    {
        get { return this._genre; }
        set { this._genre = value; }
    }
    public MediaType Type
    {
        get { return this._type; }
        set { this._type = value; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

and this is my MultimediaList class:

 public class MultimediaList : ObservableCollection<Multimedia>
{
    public MultimediaList()
    {
        Add(new Multimedia("Blaster", "UFK", "Dubstep", Multimedia.MediaType.CD));
        Add(new Multimedia("Hello", "Habstrakt", "Dubstep", Multimedia.MediaType.DVD));
        Add(new Multimedia("Black Veil", "Straight Line Stich", "Metal", Multimedia.MediaType.CD));
        Add(new Multimedia("Ridiculous", "Dope D.O.D", "Hip-Hop", Multimedia.MediaType.DVD));
    }

    public void addItem(string title, string artist, string genre, Multimedia.MediaType type)
    {
        Add(new Multimedia(title, artist, genre, type));                      
    }
}

The listbox is populated with the items that were setup manually in the programme,therefore that works but now I want to use my second window to add items to the listbox.

this is my second window setup:

private MultimediaList myList = new MultimediaList();

    public InputWindow()
    {
        InitializeComponent();
        PopulateComboBox();
    }

    private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        myList.addItem(textBox.Text, textBox1.Text, textBox2.Text, Multimedia.MediaType.CD);
        //Close();
    }

XML:

<ListBox DisplayMemberPath="Title" Name="listBox1" ...></ListBox>

Whenever I press the save button, the listbox from my main window is not populated with the new data. Can anyone help me with this?

And this is my main window code:

public MainWindow()
    {
        InitializeComponent();
        myList = new MultimediaList();
        listBox1.ItemsSource = myList;
    }

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        ModalWindow input = new ModalWindow();
        input.ShowDialog();
    }
har07
  • 88,338
  • 12
  • 84
  • 137
Alin St
  • 29
  • 6
  • have you set breakpoints and stepped through the code.. all the code is there you need to take the time to debug your code.. – MethodMan Oct 09 '15 at 17:32
  • Yes, I have debugged the code and when I insert the new data and then press save..it does update the Multimedia class but for some reason it does not return anything to the listbox. – Alin St Oct 09 '15 at 17:36
  • `myList.addItem(textBox.Text, textBox1.Text, textBox2.Text, Multimedia.MediaType.CD)` when you inspect the myList, does the Enum store as an actual string in the List..? – MethodMan Oct 09 '15 at 17:39
  • The issue is that it doesn't return anything to the list. It goes through that line and that's it. In my XML code for Listbox I am only getting the Title. – Alin St Oct 09 '15 at 17:44
  • is this XAML application.? if so ..show the full xml in the markup code – MethodMan Oct 09 '15 at 18:28
  • I am only using the XML to create/steup the listbox, textboxes etc. The rest of the code is made in C# – Alin St Oct 09 '15 at 18:36
  • how many times does this constructor get called hit in your code `public class MultimediaList : ObservableCollection` – MethodMan Oct 09 '15 at 18:38
  • 2 times. In my main windows and in the second window where I insert my own data. – Alin St Oct 09 '15 at 19:16
  • are you sure that you are not trying to populate based on the same modal window..? take a look at this similar question to see if it helps shed light on your current issue http://stackoverflow.com/questions/26929410/implement-oncollectionchanged-so-that-listbox-updates-automatically-wpf – MethodMan Oct 09 '15 at 19:19
  • No. I am creating a MultimediaList object in my main Window and then another one in the second window – Alin St Oct 09 '15 at 19:25
  • almost sounds like a scope issue can you change the `private MultimediaList myList = new MultimediaList();` to be `public MultimediaList myList = new MultimediaList();` – MethodMan Oct 09 '15 at 19:29
  • I did changed it to public. I think the problem is that I am creating 2 objects of type MultimediaList, one in my main window and another one in my second window. I just need to figure it out how to avoid that – Alin St Oct 09 '15 at 20:46

1 Answers1

1

You have to forms and need to pass data between the two forms using instances of the forms. See my sample code below

Form 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 form2;
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            form2.Show();
            string  results = form2.GetData();
        }
    }
}
​

Form 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 nform1)
        {
            InitializeComponent();

            this.FormClosing +=  new FormClosingEventHandler(Form2_FormClosing);
            form1 = nform1;
            form1.Hide();
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //stops form from closing
            e.Cancel = true;
            this.Hide();
        }
        public string GetData()
        {
            return "The quick brown fox jumped over the lazy dog";
        }

    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • I have to send the data from my second window to the first one. I am using an WPF application. I have a button on my main window where I use it to open the second window. After I finish inserting my data, I am pressing a button which should send the data to the Multimedia class and then refresh the listbox. – Alin St Oct 09 '15 at 19:41
  • It worked after converting your code into what I needed. Thank you very much. – Alin St Oct 10 '15 at 10:24