我有一个加载响应的方法,但是它可以加载所有内容,甚至是空的文本框, 而我恰恰需要那些只有内容的人
private void LoadAnswers()
{
var currentQuestion = _bsQuestions.Current as Question;
var bs = new BindingSource();
bs.DataSource = currentQuestion.Answers;
UserControl uc = new UserControl();
if (currentQuestion.Type == QuestionType.SingleSelect )
{
uc = new UserControlSingle(bs);
label4.Text = $"select 1 of { bs.Count} the answers";
}
else
{
uc = new UserControlMulti(bs);
label4.Text = $"select several of { bs.Count} the answers";
}
_panel.Controls.Clear();
_panel.Controls.Add(uc);
}
UserControlMulti
public partial class UserControlMulti : UserControl
{
private readonly BindingSource _bs;
public UserControlMulti(BindingSource bindingSource)
{
InitializeComponent();
_bs = bindingSource ?? throw new ArgumentNullException(nameof(bindingSource));
SetBindings();
}
private void SetBindings()
{
for (int i = 0; i < _bs.Count; i++)
{
//textboxes
var textBox = _panelMulti.Controls
.OfType<TextBox>().First(t => t.Name.EndsWith((i + 1).ToString()));
textBox.DataBindings.Add("Text", _bs[i],
nameof(Answer.Value), true, DataSourceUpdateMode.OnPropertyChanged);
//checkboxes
var checkbox = _panelMulti.Controls
.OfType<CheckBox>().First(t => t.Name.EndsWith((i + 1).ToString()));
checkbox.DataBindings.Add("Checked", _bs[i], nameof(Answer.IsCorrect));
}
}
}
UserControlSingle完全相同
简而言之,我需要加载所有文本框,除了空白处,如何加载复选框,该复选框应与数字中的文本框相同