简体   繁体   English

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

[英]c# Remove a dynamically created checkbox based on a button click

I'm lost. 我迷路了。 I want to remove a value from a text file. 我想从文本文件中删除一个值。 The value is a checkbox.Name. 该值是一个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();

}

I can access the checkbox.Name based on a click event so I know I'm getting the correct checkbox.Name 我可以基于click事件访问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
    {

    }


}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM