我有windows form app
动态生成
tabpages
,controls
,datagridviews
以及所有events
都是动态的
如果control
生成GDI objects
而User objects
生成objects
则不会
每次当前objects
加上之前的dispose
以及之前的controls
清理我的应用程序将会崩溃。我可以
clear
所有controls
,force
全部garbage
,OBJECT
至ARRAY
GDI Objects
收集器,NULL UserObjects
public TabPage[] tab = new TabPage[20];
public DataGridView[] rtb = new DataGridView[20];
public TextBox[] txt = new TextBox[20];
public TextBox[] txt1 = new TextBox[20];
public TextBox[] aoid = new TextBox[20];
public TextBox[] soid = new TextBox[20];
public Button[] getar = new Button[20];
public Button[] getsi = new Button[20];
private TextBox[] txt2 = new TextBox[20];
private TextBox[] txt3 = new TextBox[20];
private Label[] lbl = new Label[20];
private Label[] lbl1 = new Label[20];
private Label[] lbl2 = new Label[20];
private Label[] lbl3 = new Label[20];
private Label[] pwl = new Label[20];
private TextBox[] pwt = new TextBox[20];
private TextBox[] txt4 = new TextBox[20];
private Label[] pc_carton = new Label[20];
private TextBox[] pcs_carton = new TextBox[20];
private Label[] un = new Label[20];
private TextBox[] units = new TextBox[20];
private Label[] refno_l = new Label[20];
private TextBox[] refno_t = new TextBox[20];
private Label[] pro_l = new Label[20];
private TextBox[] pro_t = new TextBox[20];
private Label[] inlay_l = new Label[20];
public TextBox[] inlay_t = new TextBox[20];
public TextBox[] inlay_i = new TextBox[20];
private Button[] getin = new Button[20];
private Label[] yarn_l = new Label[20];
public TextBox[] yarn_t = new TextBox[20];
public TextBox[] yarn_i = new TextBox[20];
private Button[] getyrn = new Button[20];
private Label[] order_l = new Label[20];
public TextBox[] order_t = new TextBox[20];
//private Label[] inlayl = new Label[8];
//private TextBox[] inlayt = new TextBox[8];
public TextBox[] txt5 = new TextBox[6];
public TextBox[] txt6 = new TextBox[6];
private Label[] kd_l = new Label[20];
public TextBox[] kd_t = new TextBox[20];
private Label[] date_l = new Label[20];
private DateTimePicker[] date_p = new DateTimePicker[20];
private CheckBox[] checkb = new CheckBox[20];
private TextBox[] checkt = new TextBox[20];
private Label[] spec_l = new Label[20];
public RichTextBox[] spec_t = new RichTextBox[20];
private Label[] ean_l = new Label[20];
public RichTextBox[] ean_t = new RichTextBox[20];
内存等等我能做的一切
但在我的Senario工作时确实要清除try
{
if (e.KeyCode == Keys.Enter)
{
k = 0;
//s = 0;
tabControl2.TabPages.Clear();
k = Convert.ToInt32(textBox9.Text);
k1 = k - tabControl2.TabPages.Count;
//cc = Convert.ToInt32(textBox9.Text);
if (k > 20)
{
MessageBox.Show("You Exceed Limit");
}
else
{
for (int i = 0; i < k; i++)
{
tab[i] = new TabPage();
//tab[i] = new TabPage();
rtb[i] = new DataGridView();
//Start Gridview
rtb[i] = new System.Windows.Forms.DataGridView();
rtb[i].Location = new System.Drawing.Point(0, 110);
rtb[i].Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
rtb[i].Size = new System.Drawing.Size(1020, 230);
rtb[i].AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
foreach (DataGridViewColumn dcol in rtb[i].Columns)
{
dcol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
//ID Column
rtb[i].Columns.Add("tr_id", "ID");
rtb[i].Columns["tr_id"].ReadOnly = true;
rtb[i].Columns["tr_id"].Width = 1;
rtb[i].Columns["tr_id"].Visible = false;
//rtb[i].Columns["tr_id"].ReadOnly = true;
rtb[i].Columns.Add("Counter", "SNo");
rtb[i].Columns["Counter"].ReadOnly = true;
//rtb[i].Columns["tr_id"].ReadOnly = true;
//rtb[i].Columns["tr_id"].Width = 10;
rtb[i].Columns.Add("color_id", "id");
rtb[i].Columns["color_id"].Visible = false;
// Color Column
rtb[i].Columns.Add("color_name", "Color Name");
rtb[i].Columns["color_name"].ReadOnly = true;
rtb[i].Columns.Add("color_no", "Pantone No.");
rtb[i].Columns["color_no"].ReadOnly = true;
//Quantity Column
rtb[i].Columns.Add("quantity", "Quantity");
//Piece Weight Column
//rtb[i].Columns.Add("piece_weight", "Piece Weight");
//Total Weight Carton
rtb[i].Columns.Add("total_weight", "Total Weight");
rtb[i].Columns["total_weight"].ReadOnly = true;
//Piece in Carton Column
//rtb[i].Columns.Add("pcs_carton", "Pcs/Carton");
//No Of Carton Column
rtb[i].Columns.Add("no_of_carton", "Total Cartons");
//rtb[i].Columns["no_of_carton"].ReadOnly = true;
//Unit Name Column
//rtb[i].Columns.Add("unit_name", "Unit Name");
//Rate Column
rtb[i].Columns.Add("rate", "Rate");
//Amount Column
rtb[i].Columns.Add("amount", "Amount");
rtb[i].Columns["amount"].ReadOnly = true;
//Ship Qty Column
rtb[i].Columns.Add("ship_qty", "Ship Qty.");
//kdnr column
// rtb[i].Columns.Add("kdnr", "KDNR");
//reference No Column
rtb[i].Columns.Add("ref_no", "Reference No./Ean No");
//Article No/Product No
//rtb[i].Columns.Add("artno", "Art. No./Prd. No.");
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "deleterow";
checkColumn.HeaderText = "Delete Row";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10; //if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values
checkColumn.Visible = false;
rtb[i].Columns.Add(checkColumn);
//DataGridViewButtonColumn btcol2 = new DataGridViewButtonColumn();
//btcol2.HeaderText = "Delete";
//btcol2.Name = "delete";
//btcol2.Text = "Delete";
//btcol2.Width = 50;
//btcol2.UseColumnTextForButtonValue = true;
//rtb[i].Columns.Add(btcol2);
//End Gridview
rtb[i].AllowUserToAddRows = false;
//Start txt0
txt[i] = new TextBox();
txt[i].Location = new System.Drawing.Point(70, 0);
txt[i].Size = new System.Drawing.Size(169, 20);
txt[i].Enabled = false;
//End txt0
getar[i] = new Button();
getar[i].Location = new System.Drawing.Point(241, 0);
getar[i].Size = new System.Drawing.Size(35, 20);
getar[i].Text = "Get";
aoid[i] = new TextBox();
aoid[i].Location = new System.Drawing.Point(70, 0);
aoid[i].Size = new System.Drawing.Size(10, 20);
aoid[i].Visible = false;
//Start Label0
lbl[i] = new Label();
lbl[i].Location = new System.Drawing.Point(0, 0);
lbl[i].Text = "Article Name:";
lbl[i].Size = new System.Drawing.Size(70, 20);
//End Label0
//Start Label1
lbl1[i] = new Label();
lbl1[i].Location = new System.Drawing.Point(280, 0);
lbl1[i].Text = "Size:";
lbl1[i].Size = new System.Drawing.Size(30, 20);
//End
soid[i] = new TextBox();
soid[i].Location = new System.Drawing.Point(320, 0);
soid[i].Size = new System.Drawing.Size(10, 20);
soid[i].Visible = false;
//start txt1
txt1[i] = new TextBox();
txt1[i].Location = new System.Drawing.Point(320, 0);
txt1[i].Size = new System.Drawing.Size(169, 20);
txt1[i].ReadOnly = true;
//txt1[i].Text = "asdf";
//end txt1
getsi[i] = new Button();
getsi[i].Location = new System.Drawing.Point(489, 0);
getsi[i].Size = new System.Drawing.Size(35, 20);
getsi[i].Text = "Get";
////Start Label2
//lbl2[i] = new Label();
//lbl2[i].Location = new System.Drawing.Point(320, 0);
//lbl2[i].Text = "Special Instruction:";
//lbl2[i].Size = new System.Drawing.Size(100, 20);
////End Label2
////start txt2
//txt2[i] = new TextBox();
//txt2[i].Location = new System.Drawing.Point(420, 0);
//txt2[i].Size = new System.Drawing.Size(250, 50);
//txt2[i].Multiline = true;
////end txt2
////start lbl3
lbl3[i] = new Label();
lbl3[i].Location = new System.Drawing.Point(530, 0);
lbl3[i].Text = "Packs :";
lbl3[i].Size = new System.Drawing.Size(50, 20);
////end lbl3
////start txt3
txt3[i] = new TextBox();
txt3[i].Location = new System.Drawing.Point(580, 0);
txt3[i].Text = "1";
////end txt3
////start lbl3
pwl[i] = new Label();
pwl[i].Location = new System.Drawing.Point(690, 0);
pwl[i].Text = "Piece Weight";
pwl[i].Size = new System.Drawing.Size(75, 20);
////end lbl3
////start txt3
pwt[i] = new TextBox();
pwt[i].Location = new System.Drawing.Point(770, 0);
pwt[i].Size = new System.Drawing.Size(50, 20);
//pwt[i].Text = "1";
////end txt3
////start lbl3
pc_carton[i] = new Label();
pc_carton[i].Location = new System.Drawing.Point(820, 0);
pc_carton[i].Text = "Pcs Carton";
pc_carton[i].Size = new System.Drawing.Size(60, 20);
////end lbl3
////start txt3
pcs_carton[i] = new TextBox();
pcs_carton[i].Location = new System.Drawing.Point(890, 0);
pcs_carton[i].Size = new System.Drawing.Size(50, 20);
//pwt[i].Text = "1";
////end txt3
////start lbl3
un[i] = new Label();
un[i].Location = new System.Drawing.Point(940, 0);
un[i].Text = "Unit Name";
un[i].Size = new System.Drawing.Size(60, 20);
////end lbl3
////start lbl3
pro_l[i] = new Label();
pro_l[i].Location = new System.Drawing.Point(0, 30);
pro_l[i].Text = "Program No.:";
pro_l[i].Size = new System.Drawing.Size(70, 20);
////end lbl3
////start txt3
pro_t[i] = new TextBox();
pro_t[i].Location = new System.Drawing.Point(70, 30);
pro_t[i].Size = new System.Drawing.Size(30, 20);
pro_t[i].MaxLength = 1;
//pro_t[i].Text = "1";
////end txt3
//Start txt0
inlay_t[i] = new TextBox();
inlay_t[i].Location = new System.Drawing.Point(137, 30);
inlay_t[i].Size = new System.Drawing.Size(169, 20);
inlay_t[i].Enabled = false;
//End txt0
getin[i] = new Button();
getin[i].Location = new System.Drawing.Point(306, 30);
getin[i].Size = new System.Drawing.Size(35, 20);
getin[i].Text = "Get";
inlay_i[i] = new TextBox();
inlay_i[i].Location = new System.Drawing.Point(137, 30);
inlay_i[i].Size = new System.Drawing.Size(10, 20);
inlay_i[i].Visible = false;
//Start Label0
inlay_l[i] = new Label();
inlay_l[i].Location = new System.Drawing.Point(105, 30);
inlay_l[i].Text = "Inlay:";
inlay_l[i].Size = new System.Drawing.Size(32, 20);
//End Label0
//Start Label1
yarn_l[i] = new Label();
yarn_l[i].Location = new System.Drawing.Point(346, 30);
yarn_l[i].Text = "Yarn:";
yarn_l[i].Size = new System.Drawing.Size(32, 20);
//End
yarn_i[i] = new TextBox();
yarn_i[i].Location = new System.Drawing.Point(378, 30);
yarn_i[i].Size = new System.Drawing.Size(10, 20);
yarn_i[i].Visible = false;
//start txt1
yarn_t[i] = new TextBox();
yarn_t[i].Location = new System.Drawing.Point(378, 30);
yarn_t[i].Size = new System.Drawing.Size(169, 20);
yarn_t[i].ReadOnly = true;
//txt1[i].Text = "asdf";
//end txt1
getyrn[i] = new Button();
getyrn[i].Location = new System.Drawing.Point(547, 30);
getyrn[i].Size = new System.Drawing.Size(35, 20);
getyrn[i].Text = "Get";
order_l[i] = new Label();
order_l[i].Location = new System.Drawing.Point(580, 30);
order_l[i].Text = "Order No.";
order_l[i].Size = new System.Drawing.Size(70, 20);
order_t[i] = new TextBox();
order_t[i].Location = new System.Drawing.Point(650, 30);
order_t[i].Size = new System.Drawing.Size(129, 20);
// order_t[i].ReadOnly = true;
//////start txt3
//refno_t[i] = new TextBox();
kd_l[i] = new Label();
kd_l[i].Location = new System.Drawing.Point(785, 30);
kd_l[i].Text = "KDNR";
kd_l[i].Size = new System.Drawing.Size(40, 20);
kd_t[i] = new TextBox();
kd_t[i].Location = new System.Drawing.Point(825, 30);
kd_t[i].Size = new System.Drawing.Size(129, 20);
//refno_t[i].Location = new System.Drawing.Point(990, 0);
//refno_t[i].Size = new System.Drawing.Size(300, 40);
date_l[i] = new Label();
date_l[i].Location = new System.Drawing.Point(955, 30);
date_l[i].Text = "Ship. Date";
date_l[i].Size = new System.Drawing.Size(60, 20);
//refno_t[i].Multiline = true;
////pwt[i].Text = "1";
date_p[i] = new DateTimePicker();
// date_p[i].Name = "DyanmicDateTimePicker";
date_p[i].Location = new System.Drawing.Point(1020, 30);
date_p[i].Size = new System.Drawing.Size(90, 25);
date_p[i].Format = DateTimePickerFormat.Short;
date_p[i].Text = dateTimePicker_shipmentdate.Text;
////end txt3
checkb[i] = new CheckBox();
checkb[i].Location = new Point(1115, 30);
checkb[i].Text = "";
checkb[i].Size = new System.Drawing.Size(20, 20);
////start txt4
checkt[i] = new TextBox();
checkt[i].Location = new System.Drawing.Point(1140, 30);
checkt[i].Size = new System.Drawing.Size(30, 25);
checkt[i].ReadOnly = true;
checkt[i].Text = "1";
//txt3[i].Text = "1";
////end txt4
spec_l[i] = new Label();
spec_l[i].Location = new System.Drawing.Point(0, 60);
spec_l[i].Text = "Special Instruction:";
spec_l[i].Size = new System.Drawing.Size(100, 20);
spec_t[i] = new RichTextBox();
spec_t[i].Location = new Point(101, 60);
spec_t[i].Size = new System.Drawing.Size(200, 50);
ean_l[i] = new Label();
ean_l[i].Location = new System.Drawing.Point(365, 60);
ean_l[i].Text = "Ean Codes:";
ean_l[i].Size = new System.Drawing.Size(80, 20);
ean_t[i] = new RichTextBox();
ean_t[i].Location = new Point(445, 60);
ean_t[i].Size = new System.Drawing.Size(200, 50);
////start txt4
txt4[i] = new TextBox();
txt4[i].Location = new System.Drawing.Point(1050, 30);
txt4[i].Size = new System.Drawing.Size(1, 1);
//txt3[i].Text = "1";
////end txt4
tab[i].Controls.Add(lbl[i]);
tab[i].Controls.Add(txt[i]);
tab[i].Controls.Add(lbl1[i]);
tab[i].Controls.Add(txt1[i]);
tab[i].Controls.Add(lbl2[i]);
tab[i].Controls.Add(txt2[i]);
tab[i].Controls.Add(lbl3[i]);
tab[i].Controls.Add(txt3[i]);
tab[i].Controls.Add(pwl[i]);
tab[i].Controls.Add(pwt[i]);
tab[i].Controls.Add(pc_carton[i]);
tab[i].Controls.Add(pcs_carton[i]);
tab[i].Controls.Add(un[i]);
tab[i].Controls.Add(units[i]);
tab[i].Controls.Add(txt4[i]);
tab[i].Controls.Add(getar[i]);
tab[i].Controls.Add(getsi[i]);
tab[i].Controls.Add(aoid[i]);
tab[i].Controls.Add(soid[i]);
tab[i].Controls.Add(pro_l[i]);
tab[i].Controls.Add(pro_t[i]);
tab[i].Controls.Add(inlay_l[i]);
tab[i].Controls.Add(inlay_t[i]);
tab[i].Controls.Add(inlay_i[i]);
tab[i].Controls.Add(getin[i]);
tab[i].Controls.Add(yarn_l[i]);
tab[i].Controls.Add(yarn_t[i]);
tab[i].Controls.Add(yarn_i[i]);
tab[i].Controls.Add(getyrn[i]);
tab[i].Controls.Add(order_l[i]);
tab[i].Controls.Add(order_t[i]);
tab[i].Controls.Add(kd_l[i]);
tab[i].Controls.Add(kd_t[i]);
tab[i].Controls.Add(date_l[i]);
tab[i].Controls.Add(date_p[i]);
tab[i].Controls.Add(checkb[i]);
tab[i].Controls.Add(checkt[i]);
tab[i].Controls.Add(spec_l[i]);
tab[i].Controls.Add(spec_t[i]);
tab[i].Controls.Add(ean_l[i]);
tab[i].Controls.Add(ean_t[i]);
//tab[i].Controls.Add(refno_l[i]);
//tab[i].Controls.Add(refno_t[i]);
//tab[i].Controls.Add(lbl5[i]);
//tab[i].Text = txt1[i].Text;
tab[i].Controls.Add(rtb[i]);
tab[i].Location = new System.Drawing.Point(4, 22);
tab[i].Name = "tab" + i.ToString();
tab[i].Padding = new System.Windows.Forms.Padding(3);
tab[i].Size = new System.Drawing.Size(400, 242);
tab[i].Text = "Article" + i.ToString();
tab[i].UseVisualStyleBackColor = true;
tabControl2.TabPages.Add(tab[i]);
#region Auto Complete For Unit Name
try
{
//TextBox te = e.Control as TextBox;
units[i].AutoCompleteMode = AutoCompleteMode.SuggestAppend;
units[i].AutoCompleteCustomSource.AddRange(new string[] { "Piece", "Packs", "Mtr", "Kg." });
units[i].AutoCompleteSource = AutoCompleteSource.CustomSource;
//txt[i].AutoCompleteMode = AutoCompleteMode.SuggestAppend;
//txt[i].AutoCompleteSource = AutoCompleteSource.CustomSource;
//txt[i].AutoCompleteCustomSource = namesCollection1;
}
catch (Exception aa) { MessageBox.Show(aa.Message); }
#endregion
rtb[i].RowContextMenuStripNeeded += new System.Windows.Forms.DataGridViewRowContextMenuStripNeededEventHandler(this.dataGridView_RowContextMenuStripNeeded);
this.deleteRowToolStripMenuItem.Click += new System.EventHandler(this.deleteRowToolStripMenuItem_Click1);
//MessageBox.Show(""+tab[i].Text);
rtb[i].KeyDown += new KeyEventHandler(rtb_KeyDown);
rtb[i].RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView_RowEnter);
rtb[i].KeyDown += new KeyEventHandler(rtb_KeyDown);
rtb[i].EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
txt1[i].Leave += new System.EventHandler(txt1_Leave);
rtb[i].CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView_CellEndEdit);
getar[i].Click += new EventHandler(getarticle_Click);
getsi[i].Click += new EventHandler(getsize_Click);
this.pro_t[i].KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textch_KeyPress);
getyrn[i].Click += new EventHandler(getyarn_Click);
getin[i].Click += new EventHandler(getinlay_Click);
checkb[i].CheckedChanged += new System.EventHandler(CheckBoxChanged);
//this.rtb[i].DefaultValuesNeeded += new System.Windows.Forms.DataGridViewRowEventHandler(this.dataGridView_DefaultValuesNeeded);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
和{{1}}
完成我与你的伙伴共享代码plx帮助我
这是我的公共数组对象
{{1}}
这是基于TextBox值的控件生成
{{1}}
答案 0 :(得分:0)
当您使用任何控件绑定事件时,除非父项在删除do - =之前执行此操作,否则不会处置该事件。 如果你可以创建块,也可以使用关键字来创建每个创建对象的范围。