Dynamic Checkbox from a txt file entry on a WinFor

2019-08-30 19:08发布

Good Morning;

Actually have 2 questions. My first is what is this called? A Program? A Module?

WhatIsThisCalled()
 {
   //workToBeDone
 }

I'm trying to create dynamic checkbox(s) from each entry in a text file. I'm trying to reuse the code so I have tried to create the module in a logic file. I feel like I've done this correctly, but I can't test it. I can not figure out how to reference

this.Controls.Add(chk[I]); 

to the winForm I want to call it on. The error I get is about it being illegal in a static method. I'm only trying to clear the error (last one) so I can see if it will actually put the checkboxes onto the correct winForm Permissions.cs. Here is my Logic.cs module.

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

        string line = null;

        do
        {
            line = reader.ReadLine();
            if (line == null)
            {
                break;
            }

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

            try
        {

            int userCount;

            userCount = parts.Length;

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

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

                chk[i].Name = parts.ToString();

                chk[i].Text = parts.ToString();

                chk[i].TabIndex = i;

                chk[i].AutoCheck = true;

                chk[i].Bounds = new Rectangle(15, 30 + padding + height, 150, 22);

                this.Controls.Add(chk[i]);

                height += 22;

            }

        }

        catch
        {
        }
        } while (true);


    }

There is one global int userCount = 0; above that module. On Permissions.cs I have this code (with no errors).

    public Permissions()
    {
        InitializeComponent();

    }

    private void Permissions_Load(object sender, EventArgs e)
    {
        WidgetLogic.getPermText();
    }

Can anyone please direct me as to how, or if it is possible, to replace Permissions with this in a dynamic format?? I think??

Thank you very much in advance for all that look or help. I really appreciate it. Have a Great Day!! :)

1条回答
倾城 Initia
2楼-- · 2019-08-30 19:32

I tired very hard to understand what you wanted to say. I think you just want to reference the form where the CheckBoxes should create.

So you should better pass the reference the of the form on which you want to create the controls:

public static void getPermText(System.Windows.Forms.Form targetForm)
{
    //code
    targetForm.Controls.Add(chk[i]);  //changed "this" to "targetForm"

To call the method:

WidgetLogic.getPermText(this);  //here "this" refers to the current form

Now where ever you will call this method it will create the controls on your form (the one you're passing as the parameter).

Notify me if I got your question wrong.

查看更多
登录 后发表回答