c# 删除基于按钮单击的动态创建的复选框 [英] c# Remove a dynamically created checkbox based on a button click

查看:37
本文介绍了c# 删除基于按钮单击的动态创建的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm lost. I want to remove a value from a text file. The value is a checkbox.Name. I want to open a text file, find the corresponding username in the textfile, remove it and save the file based on a button click.

这是我获取复选框的方法.名称

Here is how I get the checkbox.Name

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

I can access the checkbox.Name based on a click event so I know I'm getting the correct 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

I've been searching for this most of the morning and all day Friday. Can anyone point me in the right direction or possibly suggest some sample code. Please Please. Thank you in advance. I truly do appreciate it. :-D

推荐答案

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

这篇关于c# 删除基于按钮单击的动态创建的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆