[英]How can I get the item ID in a CheckedListBox?
I have a method that pulls a url name(varchar), a urlID(int) and its Enabled status(bit) from a database and populates the results to a CheckedListBox on a foreach loop. 我有一个方法可以从数据库中提取一个URL名称(varchar),一个urlID(int)及其启用状态(位),并将结果填充到foreach循环上的CheckedListBox中。 The problem I have is the checkedboxlist only seems to take a name and its checked status. 我的问题是,checkboxboxlist似乎只有一个名称及其检查状态。 What i need to be able to do is when a user has finished with there selections on a button event it reads CheckedListBox and gets the URL ID, and enabled status so I can then write this back to the database. 我需要做的是,当用户完成按钮事件的选择后,它会读取CheckedListBox并获取URL ID和启用状态,以便随后将其写回到数据库。
This is the code I am using: 这是我正在使用的代码:
/// <summary>
/// Create url names in check box list.
/// </summary>
/// <param name="rows"></param>
private void CreateURLCheckBoxes(DataRowCollection rows)
{
try
{
int i = 0;
foreach (DataRow row in rows)
{
//Gets the url name and path when the status is enabled. The status of Enabled / Disabled is setup in the users option page
string URLName = row["URLName"].ToString();
int URLID = Convert.ToInt32(row["URLID"]);
bool enabled = Convert.ToBoolean(row["Enabled"]);
//Adds the returned to rows to the check box list
CBLUrls.Items.Add(URLName, enabled);
}
i++;
}
catch (Exception ex)
{
//Log error
Functionality func = new Functionality();
func.LogError(ex);
//Error message the user will see
string FriendlyError = "There has been populating checkboxes with the urls ";
Classes.ShowMessageBox.MsgBox(FriendlyError, "There has been an Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Step 1: Create a class to hold the Name and Id with a ToString() override that returns the Name 步骤1:创建一个类来保存名称和带有ToString()的返回ID的ID
public class UrlInfo
{
public string Name;
public int Id;
public bool Enabled;
public override string ToString()
{
return this.Name;
}
}
Step 2: Add instances of this class to your CheckedListBox 步骤2:将此类的实例添加到CheckedListBox中
UrlInfo u1 = new UrlInfo { Name = "test 1", Id = 1, Enabled = false };
UrlInfo u2 = new UrlInfo { Name = "test 2", Id = 2, Enabled = true };
UrlInfo u3 = new UrlInfo { Name = "test 3", Id = 3, Enabled = false };
checkedListBox1.Items.Add(u1, u1.Enabled);
checkedListBox1.Items.Add(u2, u2.Enabled);
checkedListBox1.Items.Add(u3, u3.Enabled);
Step 3: Cast SelectedItem to UrlInfo and retrieve the .Id 步骤3:将SelectedItem强制转换为UrlInfo并检索.Id
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
UrlInfo urlInfo = checkedListBox1.Items[e.Index] as UrlInfo;
if (null != urlInfo)
{
urlInfo.Enabled = e.NewValue == CheckState.Checked;
Console.WriteLine("The item's ID is " + urlInfo.Id);
}
}
You'd better create a simple class containing a string
(your url) and an int
(the id), override the ToString()
method to return the url, and add those objects to the Items
property of the CheckedListBox
. 您最好创建一个包含string
(您的url)和一个int
(id)的简单类,重写ToString()
方法以返回url,然后将这些对象添加到CheckedListBox
的Items
属性中。 When you get the selected object, you just have to cast it into your new class, and you can access both properties. 当您获得选定的对象时,只需将其强制转换为新类,就可以访问这两个属性。
Something like: 就像是:
public class MyClass
{
public string Url { get; set; }
public int Id { get; set; }
public override string ToString()
{
return this.Url;
}
}
And then when you add the objects : 然后,当您添加对象时:
CBLUrls.Items.Add(new MyClass { Id = URLID, Url = URLName }, enabled);
This control has a Value Member and a Display member. 此控件具有一个值成员和一个显示成员。 I think you can do what you need if you use the Name as the display member and the ID as the value member. 我认为如果使用Name作为显示成员,而将ID作为value成员,则可以执行所需的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.