【问题标题】:c# Gather checkbox.Click events for later usec# 收集 checkbox.Click 事件供以后使用
【发布时间】:2014-12-16 17:07:03
【问题描述】:

我能够修改我提出的另一个问题中提供的此代码

c# Remove a dynamically created checkbox based on a button click

足以让按钮单击和更新文件,但我被卡住了,如果有人愿意,可以使用更多帮助。我目前拥有的代码是通过单击 EventHandler 从动态创建的复选框触发的。

chkPerm[i].Click += new EventHandler(checkChangedPerm);

当我调用 checkChangedPerm() 时,我的复选框被读取,名称被修剪和格式化,结果被放入一个字符串中,以便稍后写入 Permissions.txt 文件。 checkbox.Name 是用户名 + 文件名的修剪部分。在下面的代码中,我修剪了之前添加的部分以在文件中找到正确的用户名。

public static void checkChangedPerm(Object sender, EventArgs e)
{
    CheckBox c = sender as CheckBox;

    if (c.Name != null && c.Name != "")
    {
        StreamReader reader;
        int count = c.Name.Count();
        string trimmed = c.Name.ToString();
        string outPut = trimmed.Remove(count - 4);

        using (reader = new StreamReader(dataFolder + PermFile))
        {
            while ((permLine = reader.ReadLine()) != null)
            {
                if (permLine != outPut)
                {
                    permLine_ += permLine + "\r\n";
                }                        
            }
        }

        //writer = writePerm();
    }            
    else
    {

    }        
}

permLine 和 permLine_ 在别处声明用于其他用途。

writePerm();

调用表单权限上的按钮单击。单击按钮时,writePerm 会用新内容覆盖文件的现有内容。问题在于 checkChangedPerm() 在每个 checkbox.Click 被调用并且随后在每次点击后附加到 permLine_。

我想我明白在 .Click 事件期间我需要将内容写入 theFirstCalledString。如果 theFirstCalledString 不为空,请转到 theFirstCalledString 以查看 c.Name 是否存在,否则使用我编写的内容。只是不知道从哪里开始。

谁能指出我正确的方向?这听起来像我正确地思考这个吗?请。非常感谢您提前。我需要找到这个论坛的一部分并贡献我所知道的来回馈一些。再次感谢:-D

【问题讨论】:

  • 您能否存储最后一个 outPut 值以便在以后的调用中进行比较,如果您只对最后一个值感兴趣,可以将其存储为 String,如果您想记住所有值,可以将其存储为 List<String>以前的值?此外,您应该考虑使用StringBuilder 连接从StreamReader 读取的字符串。
  • 如果在using语句之前的checkChangedPerm()中添加permLine_ = "";或者声明permLine_ 作为本地你不会有追加新文本的问题。
  • @γηράσκωδ'αείπολλάδιδασκόμε 这肯定对一些人有所帮助。不幸的是,它只检查最后一个选中的复选框。我需要让 UI.Refresh() 工作,但事实并非如此。让我刷新 UI,看看这是否会改变它的运行方式。再一次非常感谢你。 :-D

标签: c# winforms checkbox lambda


【解决方案1】:

你可以这样做。声明一个字符串列表。它可以是本地的或私有的,没关系。如果它是私有的,您需要清除列表:

private List <string> text = new List <string>();

public static void checkChangedPerm(Object sender, EventArgs e)
{
    CheckBox c = sender as CheckBox;

    if (c.Name != null && c.Name != "")
    {
        StreamReader reader;
        int count = c.Name.Count();
        string trimmed = c.Name.ToString();
        string outPut = trimmed.Remove(count - 4);

        using (reader = new StreamReader(dataFolder + PermFile))
        {
            while ((permLine = reader.ReadLine()) != null)
            {
                if (permLine != outPut)
                {
                    text.Add(permLine + "\r\n");
                }
            }
        }

        text[text.Count - 1] = text[text.Count - 1].Replace("\r\n", "");

        permLine = String.Join(String.Empty, text);

        //and write permLine to file    
        //writer = writePerm();
    }            
    else
    {

    }     

    if (text.Count != 0)
    {
        text.Clear();
    }
}

【讨论】:

    猜你喜欢
    • 2012-10-01
    • 2021-07-23
    • 1970-01-01
    • 2021-12-17
    • 2021-01-24
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多