问题描述
早上好;
其实有两个问题。我的第一个是什么叫做?计划? A模块?
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]);
到winForm我想调用它。我得到的错误是关于它是非法的静态方法。我只是想清除错误(最后一个),所以我可以看到它是否会实际上将复选框放到正确的winForm Permissions.cs。这是我的Logic.cs模块。
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);
}
有一个全局int userCount = 0 ;上面的模块。在Permissions.cs我有这个代码(没有错误)。
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!! :)
推荐答案
我累了很难理解你想说什么。我想你只是想引用 CheckBoxes
应该创建的形式。
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"
调用方法:
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.
这篇关于动态复选框从WinForm UI上的txt文件条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!