c# 删除基于按钮单击的动态创建的复选框

本文关键字:单击 动态 创建 复选框 按钮 删除 于按钮 | 更新日期: 2023-09-27 18:27:21

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

这是我获取复选框的方式。名字

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

我可以访问该复选框。根据点击事件命名,以便我知道我得到了正确的复选框。名字

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

c# 删除基于按钮单击的动态创建的复选框

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