[英]c# Dynamic textbox control with CheckBox and List
Yesterday, I asked a similar question and people told me to use a List and a controller object. 昨天,我问了一个类似的问题,有人告诉我要使用List和控制器对象。 I changed my code accordingly now added a MyContols Class and a List in order to control my items in a better way. 我相应地更改了代码,现在添加了MyContols类和列表以更好地控制我的项目。
This is my class which hold my checkbox and textboxes: 这是我的课程,其中包含我的复选框和文本框:
private List<MyControls> _myControls = new List<MyControls>();
class MyControls
{
int x=5;
int y=30;
public CheckBox cb = new CheckBox();
public TextBox tb1 = new TextBox();
public TextBox tbSpecs = new TextBox();
public TextBox tb3 = new TextBox();
public TextBox tb4 = new TextBox();
public void initElements(String name)
{
cb.Width = 10;
cb.Height = 10;
cb.Name = "cb_" + name;
cb.Location = new Point(x, y+5);
Form.ActiveForm.Controls.Add(cb);
x += 15;
tb1.Width = 50;
tb1.Height = 20;
tb1.Location = new Point(x, y);
tb1.Name = "tb1_" + name;
Form.ActiveForm.Controls.Add(tb1);
x += 60;
tbSpecs.Width = 150;
tbSpecs.Height = 20;
tbSpecs.Name = "tb2_" + name;
tbSpecs.Location = new Point(x, y);
Form.ActiveForm.Controls.Add(tbSpecs);
x += 160;
tb3.Width = 40;
tb3.Height = 20;
tb3.Name = "tb3_" + name;
tb3.Location = new Point(x, y);
Form.ActiveForm.Controls.Add(tb3);
x += 50;
tb4.Width = 450;
tb4.Height = 20;
tb4.Name = "tb4_" + name;
tb4.Location = new Point(x, y);
Form.ActiveForm.Controls.Add(tb4);
x = 0;
}
public int SetX(int i)
{
x = i;
return x;
}
public int SetY(int Y)
{
y = Y;
return y;
}
}
also I have a ProductForm Class which is a second from in my application which let me view data in textboxes and edit and delete these data. 我还有一个ProductForm类,它是应用程序中的第二类,它使我可以查看文本框中的数据并编辑和删除这些数据。
My problem is how can I delete a row? 我的问题是如何删除行?
My Second question is for each row do I have to create a new instance of my MyControl class? 我的第二个问题是我必须为MyControl类的新实例创建每一行吗? Probably my questions are very simple. 我的问题可能很简单。 Sorry for taking your time guys! 抱歉,抽出宝贵的时间!
This function is just for test purposes because I am still trying to add an delete my rows. 此功能仅用于测试目的,因为我仍在尝试添加删除行。
public void CreateFormElements()
{
ProductForm form2 = new ProductForm();
form2.Visible = true;
form2.Activate();
MyControls mc = new MyControls();
_myControls.Add(mc);
_myControls[0].initElements("1");
mc = new MyControls();
_myControls.Add(mc);
mc.SetY(55);
_myControls[1].initElements("2");
}
How can I delete an entire row if the checkbox corresponding is selected? 如果选中对应的复选框,如何删除整行?
can you tell me a code which can do that? 你能告诉我一个可以做到的代码吗?
In your Init, add a handler for cb.Checked event and then delete the row. 在您的Init中,为cb.Checked事件添加一个处理程序,然后删除该行。
In Init: 初始化中:
cb.CheckedChanged += cb_CheckedChanged;
Handler - not sure of the exact implementation in your case but here is some rough code: 处理程序-不确定您所用的具体实现,但是下面是一些粗略的代码:
private void cb_CheckedChanged(Object sender, EventArgs e) {
string NameSet = (sender as CheckBox).Name.Split(new char[]{'_'})[1];
Form.ActiveForm.Controls.Remove("ch_" + NameSet);
Form.ActiveForm.Controls.Remove("tb1_" + NameSet);
Form.ActiveForm.Controls.Remove("tb2_" + NameSet);
Form.ActiveForm.Controls.Remove("tb3_" + NameSet);
Form.ActiveForm.Controls.Remove("tb4_" + NameSet);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.