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. I changed my code accordingly now added a MyContols Class and a List in order to control my items in a better way.
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.
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? 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.
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);
}
精彩评论