我对CheckListBox
失去了理智,我试图将所有选中的项目都放入列表中。这是我使用的代码,带有用户界面上的按钮:
public partial class formPCRBaseline : Form
{
public formPCRBaseline(List<GetBaselineSectionTasks> m_objPCRCheck)
{
InitializeComponent();
setDefaults(m_objPCRCheck);
}
private void setDefaults(List<GetBaselineSectionTasks> m_objPCRCheck)
{
checkedListBox.BackColor = Color.White;
List<GetBaselineSectionTasks> m_objCheckeditem = new List<GetBaselineSectionTasks>();
int i= 0;
foreach (GetBaselineSectionTasks i_objPCRCheck in m_objPCRCheck)
{
checkedListBox.Items.Add(i_objPCRCheck.taskname);
}
}
private void buttonConfirm_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
private void buttonClose_Click_1(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
public List<GetBaselineSectionTasks> GetCheckedItems()
{
List<GetBaselineSectionTasks> m_objCheckeditem = new List<GetBaselineSectionTasks>();
m_objCheckeditem.AddRange(checkedListBox.CheckedItems.OfType<GetBaselineSectionTasks>());
return m_objCheckeditem;
}
}
}
这里是我用来初始化组件的类:
private void InitializeComponent()
{
this.buttonClose = new System.Windows.Forms.Button();
this.buttonConfirm = new System.Windows.Forms.Button();
this.checkedListBox = new System.Windows.Forms.CheckedListBox();
this.SuspendLayout();
//
// buttonClose
//
this.buttonClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonClose.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F);
this.buttonClose.Location = new System.Drawing.Point(306, 299);
this.buttonClose.Name = "buttonClose";
this.buttonClose.Size = new System.Drawing.Size(100, 30);
this.buttonClose.TabIndex = 0;
this.buttonClose.Text = "Cancel";
this.buttonClose.UseVisualStyleBackColor = true;
this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click_1);
//
// buttonConfirm
//
this.buttonConfirm.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonConfirm.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F);
this.buttonConfirm.Location = new System.Drawing.Point(192, 299);
this.buttonConfirm.Name = "buttonConfirm";
this.buttonConfirm.Size = new System.Drawing.Size(100, 30);
this.buttonConfirm.TabIndex = 4;
this.buttonConfirm.Text = "OK";
this.buttonConfirm.UseVisualStyleBackColor = true;
this.buttonConfirm.Click += new System.EventHandler(this.buttonConfirm_Click);
//
// checkedListBox
//
this.checkedListBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.checkedListBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 5.5F);
this.checkedListBox.FormattingEnabled = true;
this.checkedListBox.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.checkedListBox.Location = new System.Drawing.Point(16, 41);
this.checkedListBox.Name = "checkedListBox";
this.checkedListBox.Size = new System.Drawing.Size(394, 244);
this.checkedListBox.TabIndex = 5;
//
// formPCRBaseline
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(418, 334);
this.Controls.Add(this.checkedListBox);
this.Controls.Add(this.buttonConfirm);
this.Controls.Add(this.buttonClose);
this.Name = "formPCRBaseline";
this.ShowIcon = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "PMIS – Project Planning";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button buttonClose;
private System.Windows.Forms.Button buttonConfirm;
private System.Windows.Forms.CheckedListBox checkedListBox;
但是它不起作用,当我使用我的断点时,它到达了foreach
,我已经选择了两个项目,但是没有< em>进入
知道我该怎么办?
答案 0 :(得分:0)
您是否正在寻找CheckedItems / CheckedIndices属性?假设CheckListBox
-checkedListBox
包含GetBaselineSectionTasks
个项目,即您按以下方式填写:
checkedListBox.Items.Add(new GetBaselineSectionTasks(...));
...
checkedListBox.Items.Add(new GetBaselineSectionTasks(...));
你可以放
List<GetBaselineSectionTasks> m_objCheckeditem = new List<GetBaselineSectionTasks();
...
m_objCheckeditem.AddRange(checkedListBox.CheckedItems.OfType<GetBaselineSectionTasks>());
,或者如果您想一次创建一个列表:
List<GetBaselineSectionTasks> result = checkedListBox.CheckedItems
.OfType<GetBaselineSectionTasks>()
.ToList();
编辑:如果正在使用string
,即您这样填充checkedListBox
:
checkedListBox.Items.Add("Some Value");
...
checkedListBox.Items.Add("Some other value");
您可以遍历选中的项目,并在尝试找出对应的项目时
foreach (int index in checkedListBox.CheckedIndices) {
string taskName = Convert.ToString(checkedListBox[index]);
// Now you have item index and item text (which is taskName). Let's try filling the list
// We can try finding the item by string
for (var item in m_objPCRCheck) {
if (item.taskname == taskName) {
m_objCheckeditem.Add(item);
break;
}
}
// Or (alternatively) you can trying to get index-th item:
// m_objCheckeditem.Add(m_objPCRCheck[index]);
}