[英]Is there a way to access and change a value in a custom control that was added, programmatically, to a panel?
How can I change the check box "checked" value, in a custom control, after programmatically adding it to a table? 在将自定义控件中的复选框“ checked”值以编程方式添加到表后,如何更改它?
I have a custom control that has a text box and a check box. 我有一个具有文本框和复选框的自定义控件。 (I don't have enough of a reputation to post an image but here's a shot) https://imgur.com/a/yyXG7p8
(我没有足够的声誉来发布图像,但这是镜头) https://imgur.com/a/yyXG7p8
It has a check box and a textbox. 它具有一个复选框和一个文本框。
There's a public method, in that class, that allows for setting the checkbox to either true or false: 在该类中,有一个公共方法可以将复选框设置为true或false:
public void setCheckBox(bool set)
{
checkBox1.Checked = set;
}
In another class, the main class, I have a loop that adds a bunch of these custom controls to a panel: 在另一个类(主类)中,我有一个循环,将一堆这些自定义控件添加到面板中:
private void DrawInputBits()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 32; j++)
{
currentTag = tagsFile[tagIndex];
tagIndex++;
CustomControl.BitsControl newBits = new CustomControl.BitsControl(i, j, currentTag, false);
InputBitsTable.Controls.Add(newBits);
}
}
}
If you can't tell, Each check box holds the value of a bit in a 32-bit number for 4 numbers (hence the nested loops 0-31, 4 times) and the point is to show or control the values. 如果您无法分辨,每个复选框将32位数字中的一位值保留为4个数字(因此,嵌套循环0-31,共4次),关键是显示或控制这些值。 You can ignore the code about the tags.
您可以忽略有关标签的代码。 All that's doing is reading text from a file and populating the text box with that read text.
所有要做的就是从文件中读取文本,并使用读取的文本填充文本框。
When the code is executing, if a bit's state changes, the checkbox should also change state. 在执行代码时,如果某个位的状态发生更改,则复选框也应更改状态。 The following loop will be modified to go through an array of int's (4 ints) and update each check box after reading the state of each bit.
下面的循环将被修改以遍历int的数组(4个int),并在读取每个位的状态后更新每个复选框。 Currently, the loop is just set to change all bits to be "true".
当前,仅将循环设置为将所有位更改为“ true”。 That's not what I want in the end, but the point is, I can't even reference the custom controls in the panel to change the check box at all.
这最终不是我想要的,但重点是,我什至无法引用面板中的自定义控件来完全更改复选框。 I thought the following would work but nothing I've tried yet has.
我认为以下方法会起作用,但是我没有尝试过。
private void UpdateInputsScreen(int[] inputDINTS)
{
for(int i = 0; i < InputBitsTable.Controls.Count; i++)
{
for(int j = 0; j < 32; j++)
{
InputBitsTable.Controls[i].setCheckBox(true);//this line gives an error.
}
}
}
This is the class for the Custom Control. 这是自定义控件的类。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace CustomControl
{
public partial class BitsControl: UserControl
{
int bit = 0;
int dint = 0;
string tag = "";
BitsControl(int dint, int bit, string tag, bool isEnabled)
{
InitializeComponent();
this.dint = dint;
this.bit = bit;
this.tag = makeTag(tag, dint, bit);
this.textBox1.Text = this.tag;
checkBox1.Enabled = isEnabled;
}
private string makeTag(string tag, int dint, int bit)
{
string newTag = tag + ":[" + dint + "]." + bit;
return newTag;
}
public void setCheckBox(bool set)
{
checkBox1.Checked = set;
}
}
}
The main class is a little too long to provide but I can upon request. 主课程太长了,无法提供,但我可以应要求提供。
What about keeping a list or dictionary to your controls: 如何保留列表或字典到您的控件:
var dict = new Dictionary<string, CustomControl.BitsControl>();
When creating the components add it to the dictionary: 创建组件时,将其添加到字典中:
dict.Add(currentTag, newBits);
And then try something like: 然后尝试类似:
foreach(var tag in tagsFile)
{
dict[tag].setCheckBox(true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.