C#中怎么使用Winform動態(tài)生成控件,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
十載的博山網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整博山建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“博山網(wǎng)站設(shè)計”,“博山網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
實現(xiàn)方式
1、加載數(shù)據(jù),往panel添加Label 控件。
private void LoadRoomType() { DataTable dtRoomType = _roomTypeBLL.GetModelList("", "Code"); pnlRoomType.Controls.Clear(); int padding = 5; int x = padding, y = padding; pnlRoom.Controls.Clear(); foreach (DataRow item in dtRoomType.Rows) { Label lbl = new Label(); lbl.Text = string.Format("{0}", item["Names"]); lbl.Image = btnRoomType.Image; lbl.Cursor = Cursors.IBeam; lbl.TextAlign = btnRoomType.TextAlign; lbl.Font = btnRoomType.Font; lbl.ForeColor = btnRoomType.ForeColor; lbl.Size = btnRoomType.Size; lbl.Location = new Point(x, y); lbl.Tag = item; lbl.Click += new EventHandler(lbl_Click); lbl.MouseEnter += new EventHandler(lbl_MouseEnter); lbl.MouseLeave += new EventHandler(lbl_MouseLeave); x += lbl.Width + padding; if (x + lbl.Width > pnlRoomType.Width) { x = padding; y += lbl.Height + padding; } pnlRoomType.Controls.Add(lbl); } int height = y + (x != padding ? pnlRoomType.Height : 0) + padding; int addHeight = height - pnlRoomType.Height; pnlRoom.Top = pnlRoom.Top + addHeight; pnlRoom.Height = pnlRoom.Height - addHeight; pnlRoomType.Height = pnlRoomType.Height + addHeight; if (dtRoomType.Rows.Count > 0) LoadRoomByTypeID(dtRoomType.Rows[0], 0); }
2、定義Label 的點擊事件。
void lbl_Click(object sender, EventArgs e) { try { Label lbl = sender as Label; DataRow row = lbl.Tag as DataRow; LoadRoomByTypeID(row, 0); } catch (Exception ex) { ; } }
3、定義Label 的鼠標事件。
#region lbl_MouseLeave void lbl_MouseLeave(object sender, EventArgs e) { Label lbl = sender as Label; lbl.Font = new Font(lbl.Font, FontStyle.Regular); lbl.Cursor = Cursors.Default; lbl.ForeColor = btnRoomType.ForeColor; } #endregion #region lbl_MouseEnter void lbl_MouseEnter(object sender, EventArgs e) { Label lbl = sender as Label; lbl.Font = new Font(lbl.Font, FontStyle.Bold); lbl.Cursor = Cursors.IBeam; } #endregion
看完上述內(nèi)容,你們掌握C#中怎么使用Winform動態(tài)生成控件的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!