我正在尝试填写表格(电影信息,如标题,年份和流派),然后在文本框中显示(enable = false)。 我有2个类和一个Form(有2个文本框(用于用户输入),2个按钮,1个用于注册电影信息,1个用于显示第3个文本框中的信息(已禁用)。
class Movies
{
public string Title { get; set; }
public int Year { get; set; }
public string ReturnMovie()
{
return Title + " (" + Year.ToString() + ")";
}
}
class Genres
{
public string Genre { get; set; }
public string ReturnGenre()
{
return Genre;
}
}
的MainForm:
bool btnRegisterClicked = false;
Movies m1 = new Movies();
Genres g1 = new Genres();
private void btnRegister_Click(object sender, EventArgs e)
{
btnRegisterClicked = true;
if (btnRegisterClicked == true)
{
if (txtTitle.Text.Length == 0)
{
MessageBox.Show("Enter a title for the movie.");
btnRegisterClicked = false;
}
else if (txtYear.Text.Length < 4)
{
MessageBox.Show("Enter a valid year.");
btnRegisterClicked = false;
}
else if (chkGenre.SelectedIndex == -1)
{
MessageBox.Show("Select the genre(s) of the movie.");
btnRegisterClicked = false;
}
else
{
txtTitle.Text = m1.Title;
txtYear.Text = m1.Year.ToString();
chkGenre.Text = g1.Genre;
}
txtTitle.Clear();
txtYear.Clear();
chkGenre.ClearSelected();
}
}
private void btnShow_Click(object sender, EventArgs e)
{
txtShow.Text = m1.ReturnMovie() + g1.ReturnGenre();
}
}
我做错了什么?
答案 0 :(得分:5)
else
区块中的分配是向后的。您已将TextBox
设置为m1
和g1
的未初始化内容,然后立即.Clear()
将其删除。
else
{
m1.Title = txtTitle.Text;
m1.Year = int.Parse(txtYear.Text);
g1.Genre = chkGenre.Text
}
答案 1 :(得分:2)
您需要为变量赋值而不是控件...,更改后面的
else
{
txtTitle.Text = m1.Title;
txtYear.Text = m1.Year.ToString();
chkGenre.Text = g1.Genre;
}
到
else
{
m1.Title = txtTitle.Text;
m1.Year = int.Parse(txtYear.Text);
g1.Genre = chkGenre.Text;
}