【问题标题】:C# WinForm how to link a command to a dynamically made checkbox? [duplicate]C# WinForm 如何将命令链接到动态制作的复选框? [复制]
【发布时间】:2016-11-25 10:58:48
【问题描述】:

我目前正在 C# Winform 中制作闪存驱动器锁(它隐藏/取消隐藏文件夹)。

我已经准备好把它做成 C# 控制台,所以我有大部分需要的命令。我有一个 dir 命令获取所有文件夹的路径

public static dynamic getFolder()
{
    Dictionary<dynamic, List<dynamic>> dictionary = new Dictionary<dynamic, List<dynamic>>();

    int selector = 1;
    bool isHidden;
    foreach (string folderPath in Directory.GetDirectories(Form2.driveLetter))
    {
        List<dynamic> data = new List<dynamic>();
        string folderName = folderPath.Substring(3);
        if (folderName == "#") { continue; }
        if (folderName == "System Volume Information") { continue; }
        isHidden = Status(folderPath);

        data.Add(folderPath.Substring(3, folderPath.Length - 3));
        data.Add(isHidden);
        data.Add(folderPath);

        dictionary.Add(selector, data);
        selector++;
    }

    dictionary.Add(selector++, Vault());
    return dictionary;
}
then I have the Dictionary piped into a different method that gets the names of the folder(and hidden state) then turns them into checked boxes; if the folder is hidden, then the box is checked, and if it isn't, then its unhidden (I will in time make this part more efficient)

然后我将 Dictionary 导入另一种获取文件夹名称(和隐藏状态)的方法,然后将它们转换为复选框;如果文件夹被隐藏,则选中该框,如果不是,则取消隐藏(我会及时使这部分更有效)

private void CreateBox(Dictionary<dynamic, List<dynamic>> dictionary)
{
    int y = 0;
    int z = 0;
    CheckBox box;
    for (int x = 1; x < dictionary.Count(); x++)
    {
        List<dynamic> folder = dictionary[x];
        box = new CheckBox();
        box.Text = folder[0];
        box.AutoSize = true;
        box.Checked = folder[1];

        if (y == 180)
        {
            box.Location = new Point(370, 39 + z);
            z += 20;
        }
        else
        {
            box.Location = new Point(198, 39 + y);
            y += 20;
        }
        this.Controls.Add(box);
        CheckBoxes.Add(box);
    }
}

How do I link a SetAttributes command to the check box, so when the box is checked, it hides the respective folder and when the box is unchecked, it unhides the folder

【问题讨论】:

  • 请不要链接到外部网站。
  • 您的问题是否只是询问如何将事件处理程序附加到动态创建的控件?
  • 听起来不错,我正在尝试将命令链接到取消/选中特定框的时间。因此,当音乐标签旁边的框未选中时,它会在 E:\Music 处触发取消隐藏命令,如果再次选中,它将在 E:\Music 处触发隐藏命令
  • 我对在此处设置复选框时如何操作有所了解,但在动态制作时我不知道如何操作。

标签: c# winforms checkbox dynamic


【解决方案1】:

您需要做的就是在创建复选框时挂钩事件。

private void CreateBox(Dictionary<dynamic, List<dynamic>> dictionary)
{
    int y = 0;
    int z = 0;
    CheckBox box;
    for (int x = 1; x < dictionary.Count(); x++)
    {
        List<dynamic> folder = dictionary[x];
        box = new CheckBox();
        box.CheckedChanged += box_CheckedChanged; // here
        box.Text = folder[0];
        box.AutoSize = true;
        box.Checked = folder[1];
        this.Controls.Add(box);
    }
}

private void box_CheckChanged(object sender, EventArgs e)
{
    // some code here.
}

【讨论】:

    【解决方案2】:

    选中或不选中复选框后调用方法的最佳方式是执行事件处理程序,特别是鼠标单击。

    看起来您正在使用 Visual Studio 来执行 winform,因此我建议让 Visual Studio 处理事件处理程序的创建。

    1. 如果您转到复选框的属性并单击顶部的小照明按钮,它将带您进入可以附加到该框的所有事件的列表。
    2. 只要找到鼠标点击事件并写下事件的名称即可。
    3. 在方法本身中,检查框的状态(是否选中)
    4. 根据状态调用你想要的方法

    这是微软关于 winform 事件处理的文档:https://msdn.microsoft.com/en-us/library/dacysss4(v=vs.110).aspx

    【讨论】:

    • 我明白你在说什么,但复选框实际上并不在基本表单上,它们是通过方法创建的。有没有办法将事件链接到方法中制作的框?我知道你在说什么,通过鼠标单击更新它并且它确实有效,但是有没有办法通过取消/选中的框让它更新?
    • 是的,发现可以为对象创建和添加事件处理程序!stackoverflow.com/questions/16035148/…
    猜你喜欢
    • 1970-01-01
    • 2016-03-20
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 2012-03-06
    相关资源
    最近更新 更多