1
Answer

How to Pass ComboBox items as Cnstructor Parameter

Behrouz Hosseini

Behrouz Hosseini

14y
5.4k
1
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:

//======================================= 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);
}
}
}
}
//========================================================

Answers (1)