【问题标题】:c# Remove a dynamically created checkbox based on a button clickc# 根据按钮单击删除动态创建的复选框
【发布时间】:2014-10-21 01:11:37
【问题描述】:

我迷路了。我想从文本文件中删除一个值。该值是一个复选框。名称。我想打开一个文本文件,在文本文件中找到相应的用户名,将其删除并基于按钮单击保存文件。

这是我获得复选框的方法。名称

public static void getPermText(System.Windows.Forms.Form targetForm)
{
    Stream fileStream = File.Open(dataFolder + PermFile, FileMode.Open);
    StreamReader reader = new StreamReader(fileStream);

    string line = null;

    line = reader.ReadToEnd();


    string[] parts = line.Split('\n');

    string user = userNameOnly();
    try
    {

        int userCount;

        userCount = parts.Length;

        CheckBox[] chkPerm = new CheckBox[userCount];
        int height = 1;
        int padding = 10;

        for (int i = 0; i < userCount; i++)
        {
            chkPerm[i] = new CheckBox();

            string Perm = "Perm";

            chkPerm[i].Name = parts[i].Trim('\r') + Perm;

            chkPerm[i].Text = parts[i];

            chkPerm[i].TabIndex = i;

            chkPerm[i].AutoCheck = true;

            chkPerm[i].Bounds = new Rectangle(15, 40 + padding + height, 100, 22);

            //Assigns an eventHandler to the chkPerm.CheckBox that tells you if something is clicked, then that checkBox is selected/checked.
            //Not currently in use.
            chkPerm[i].Click += new EventHandler(checkChangedPerm);


            targetForm.Controls.Add(chkPerm[i]);

            height += 22;

            //MessageBox.Show(chkPerm[i].Name);

        }



    }
    catch
    {

    }
    fileStream.Close();

}

我可以根据点击事件访问 checkbox.Name,所以我知道我得到了正确的 checkbox.Name

public static void checkChangedPerm(Object sender, EventArgs e)
{

    CheckBox c = sender as CheckBox;

    if (c.Name != null && c.Name != "")
    {
        int count = c.Name.Count();

        string trimmed = c.Name.ToString();
        string outPut = trimmed.Remove(count - 4);


    }
    else
    {

    }


}

我在星期五的大部分时间和一整天都在寻找这个。谁能指出我正确的方向或可能建议一些示例代码。请请。先感谢您。我真的很感激。 :-D

【问题讨论】:

  • 因此,您似乎正在根据文件中的名称动态创建复选框。您可以打开文件,找到字符串并将其删除,或者使用您为复选框提取的内容重新创建文件,减去您不需要的内容。我个人会选择第二个选项,但我不确定文件是什么样的。您不确定如何写入文本文件或...?或者,根据最终目的使用序列化可能更容易,因为您可以根据需要将对象反序列化/序列化为文本文件。
  • 我同意@user1274820 的建议。除了它们之外,请不要忽略所有带有空 try..catch 的异常,将您的两个 Stream 对象放入 using 子句中以确保它们被正确释放并查看 TextReader 类以读取文件逐行而不是先将整个文件加载到内存中,这也将消除手动修剪字符串中换行符的要求
  • @user1274820 我认为我的困惑是试图写回文件。我可以获得要删除的复选框的单击事件,但这并没有为我提供页面上需要保留的其他 20 个复选框。我认为最大的困难就在于此。我想我理解从字符串解析文本,但从字符串中间或从 stringArray 解析字符串。我想我不明白。
  • 这可能是支持 Bernd Linde 提议的一个很好的理由。如果您逐行阅读,则可以将每一行存储到一个数组中,当需要将其写回时,您可以跳过不需要的行。查看此链接dotnetperls.com/textreader
  • @FrankPytel,您可以通过调用c.GetContainerControl() 获取您的复选框的ContainerControl。将其转换为您的表单类型,然后遍历它的 Controls,找到每个 CheckBox,然后将其写入文件,如果您每次都采用覆盖文件的路径

标签: c# winforms streamreader


【解决方案1】:
StreamReader reader;
StreamWriter writer;

string line, newText = null;

using (reader = new StreamReader(@"..."))
{
    while ((line = reader.ReadLine()) != null)
    {
        if (line != "checkbox name")
        {
            newText += line + "\r\n";
        }
    }
}

newText = newText.Remove(newText.Length - 2); //trim the last \r\n

using (writer = new StreamWriter(@"...")) //same file
{
    writer.Write(newText);
}

【讨论】:

  • 我宁愿推荐使用StringBuilder而不是一直连接string,那么你也可以使用StringBuilder.AppendLine(string)而不是硬编码换行符
  • 我个人更喜欢字符串列表
  • @γηράσκωδ'αείπολλάδιδασκόμε 太棒了!!再次。我一直在单独玩它。你写的正是我所展示的。加工。谢谢!!
  • @γηράσκωδ'αείπολλάδιδασκόμε 你回答了我最常问的问题。我将其修复为按钮触发器,但写入不正确。每次 .Click 触发时,它都会将最后一个字符串附加到新字符串 line_。我已经大幅改变了它,所以我需要重新询问。非常感谢:-D 你很好。
  • @FrankPytel 编辑您的问题以查看问题。我们会修复它:)
猜你喜欢
  • 2016-11-28
  • 2015-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多