Dynamic Checkbox from a txt file entry on a WinForm UI

╄→尐↘猪︶ㄣ 提交于 2019-12-25 16:47:01

问题


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:


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.



来源:https://stackoverflow.com/questions/26362744/dynamic-checkbox-from-a-txt-file-entry-on-a-winform-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!