1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pass ComboBox items as Constructor Param

Discussion in 'C#' started by Behseini, Jul 7, 2010.

  1. Behseini

    Behseini New Member

    Joined:
    Jul 7, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi
    Could you please let me know how I can pass a combobox items as constructor parameter?
    As you can see from the codes I have a combobox which is loaded by enum FilmType.now I want to create a movie object by constructor Movie() like:
    Movie film = new Movie(textBox1.Text, comboBox1.SelectedItem.ToString());
    but I can not convert the item type to enum type!
    I Have following Classes:
    Code:
    //======================================= Movie.cs
     public class Movie
        {
            public enum FilmType
            {
                Action,
                Comedy,
                Horor,
            }
            private string title;
            private FilmType type;
    
            public Movie(string title, FilmType type)
            {
                this.title = title;
                this.type = type;
            }
            public Movie() { }
    
            public string Title
            {
                get { return title; }
                set { this.title = value; }
            }
            public FilmType MovieType
            {
                get { return type; }
                set { this.type = value; }
            }
            public override string ToString()
            {
                return Title;
            }
        }
    and I have a Form application as below:
    //================================== Form1.cs
     public partial class Form1 : Form
        {
           
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Movie film = new Movie(textBox1.Text, comboBox1.SelectedItem.ToString()); 
                istBox3.Items.Add(film); 
                textBox1.Clear();
                textBox1.Select();
                comboBox1.SelectedIndex = -1;
                
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                {
                    foreach (string movieType in Enum.GetNames(typeof(Movie.FilmType)))
                    {
                        comboBox1.Items.Add(movieType);
                    }
                }
            }
        }
    //========================================================
     

Share This Page