listbox以某种方式添加项目

时间:2013-05-24 00:02:00

标签: c#

我有这个学校项目来创建一个“电影库”。将“电影”添加到库并查看我输入的信息非常有用。

当我调试它时,我会输入有关移动的信息,然后按添加(“Läggtill”按钮)将其保存在列表数组中。它工作得很好,但除了一个问题之外的一切...第二次我把一个电影添加到库中......这是信息的两倍:/

代码:

    string title = "";
    string genre = "";
    string fakta = "";
    string lengd = "";
    string bluray = "Bluray";
    string dvd = "";
    string bildlänk = "";
    string box1;

    public void button1_Click(object sender, EventArgs e)
    {

            if (radioButton1.Checked || radioButton2.Checked)
            {
                title = textBox1.Text;
                genre = textBox4.Text;
                lengd = textBox3.Text;
                fakta = richTextBox1.Text;
                bildlänk = textBox5.Text;
                dvd = radioButton2.Text;
                bluray = radioButton1.Text;
                if (radioButton1.Checked)
                {
                    array.Add(a = new Arbetare(title, genre, lengd, fakta, bluray, dvd, bildlänk));
                    listBox1.Items.Add(title);
                }
                else                                        
                {
                    array.Add(a = new Arbetare(title, genre, lengd, fakta, bluray, dvd, bildlänk));
                    listBox1.Items.Add(title);
                }

             }

        else
        {
            MessageBox.Show("Du kanske ska skriva in något?");
        }
    }

    public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (radioButton1.Checked)
        {
            int index = listBox1.SelectedIndex;
            title = array[index].Title;
            genre = array[index].Genre;
            lengd = array[index].Lengd;
            fakta = array[index].Fakta;
            bluray = array[index].Bluray;
            box1 = " Title: " + title + "\n Genre: " + genre + "\n Längd: " + lengd + "\n Fakta: " + fakta + "\n " + bluray;
            richTextBox1.Text = box1;
            string text = openFileDialog1bild.FileName;
            textBox5.Text = text;
            pictureBox1.ImageLocation = array[index].Bildlänk;
        }
        else
        {
            int index = listBox1.SelectedIndex;
            title = array[index].Title;
            genre = array[index].Genre;
            lengd = array[index].Lengd;
            fakta = array[index].Fakta;
            dvd = array[index].Dvd;
            box1 = " Title: " + title + "\n Genre: " + genre + "\n Längd: " + lengd + "\n Fakta: " + fakta + "\n " + dvd;
            richTextBox1.Text = box1;
            string text = openFileDialog1bild.FileName;
            textBox5.Text = text;
            pictureBox1.ImageLocation = array[index].Bildlänk;
        }

    } 

Arbetare班:

  public class Arbetare : Person
    {

    public Arbetare(string title, string genre, string fakta, string lengd, string bluray, string dvd, string bildlänk)
    :base (genre, fakta, lengd, title, bluray, dvd, bildlänk)
    {

    }

}

人员类:

public class Person
    {

  protected string title;
  protected string genre;
  protected string lengd;
  protected string fakta;
  protected string bluray;
  protected string dvd;
  protected string bildlänk;

  public string Title
  {
      get { return title; }
      set { title = value; }
  }
  public string Bildlänk
  {
      get { return bildlänk; }
      set { bildlänk = value; }
  }

    public string Genre
    {
        get { return genre; }
        set { genre = value; }
    }

    public string Lengd
    {
        get { return lengd; }
        set { lengd = value; }
    }

    public string Fakta
    {
        get { return fakta; }
        set { fakta = value; }
    }

    public string Bluray
    {
        get { return bluray; }
        set { bluray = value; }
    }
    public string Dvd
    {
        get { return dvd; }
        set { dvd = value; }
    }


    public Person(string title, string genre, string lengd, string fakta, string bluray, string dvd, string bildlänk)
    {
        this.Title = title;
        this.Genre = genre;
        this.Lengd = lengd;
        this.Fakta = fakta;
        this.Bluray = bluray;
        this.Dvd = dvd;
        this.bildlänk = bildlänk;
    }

    public Person()
    {
        this.Title = "";
        this.Genre = "";
        this.Lengd = "";
        this.Fakta = "";
        this.Bluray = "Blu-ray";
        this.Dvd = "DVD";
        this.bildlänk = "";
    }


}

添加一部电影时的图片: https://imageshack.us/scaled/large/12/98427392.jpg

添加第二部电影时的图片: https://imageshack.us/scaled/large/443/28077346.jpg

如果很难读到其中的一部分,我真的很抱歉......其中一些东西都是瑞典语。

0 个答案:

没有答案