如何获取 CheckedListBox 中的项目 ID?

发布于 2024-10-15 10:17:11 字数 1358 浏览 4 评论 0原文

我有一个方法,可以从数据库中提取 url 名称(varchar)、urlID(int)及其启用状态(位),并将结果填充到 foreach 循环上的 CheckedListBox 中。我遇到的问题是 checkboxlist 似乎只采用名称及其检查状态。我需要做的是,当用户完成按钮事件上的选择时,它会读取 CheckedListBox 并获取 URL ID 和启用状态,以便我可以将其写回数据库。

这是我正在使用的代码:

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

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. The problem I have is the checkedboxlist only seems to take a name and its checked status. 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没︽人懂的悲伤 2024-10-22 10:17:11

步骤 1:创建一个类来保存名称和 Id,并覆盖返回名称的 ToString()

public class UrlInfo
{
    public string Name;
    public int Id;
    public bool Enabled;

    public override string ToString()
    {
        return this.Name;
    }
}

步骤 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);

步骤 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);
    }
}

Step 1: Create a class to hold the Name and Id with a ToString() override that returns the Name

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

 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

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);
    }
}
简单爱 2024-10-22 10:17:11

您最好创建一个包含 string (您的 url)和 int (id)的简单类,覆盖 ToString() 方法返回 url,并将这些对象添加到 CheckedListBoxItems 属性中。
当您获取选定的对象时,只需将其转换为新类,然后就可以访问这两个属性。

像这样:

public class MyClass
{
    public string Url { get; set; }
    public int Id { get; set; }
    public override string  ToString()
    {
         return this.Url;
    }
}

然后当您添加对象时:

CBLUrls.Items.Add(new MyClass { Id = URLID, Url = URLName }, enabled);

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.
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);
﹂绝世的画 2024-10-22 10:17:11

该控件有一个 Value 成员和一个 Display 成员。我认为如果您使用 Name 作为显示成员并将 ID 作为值成员,您就可以做您需要的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文