自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#Winform怎樣實現(xiàn)動態(tài)生成控件

開發(fā) 后端
窗體設(shè)計中,需要什么控件就從工具箱里拖出一個控件“掛”在窗體上。其實,窗體上的控件,不但可以“拖”,還可以用代碼動態(tài)生成。動態(tài)生成控件,對需要相同的大量的控件還是比較有用的。

[[387388]]

 

本文轉(zhuǎn)載自微信公眾號「UP技術(shù)控」,作者conan5566 。轉(zhuǎn)載本文請聯(lián)系UP技術(shù)控公眾號。 

概述

動態(tài)創(chuàng)建添加控件,可以在一個大容器里一次性放入多個控件。例:根據(jù)文本框中輸入的數(shù)字 給flowLayoutPanel1 添加多少個button。窗體設(shè)計中,需要什么控件就從工具箱里拖出一個控件“掛”在窗體上。其實,窗體上的控件,不但可以“拖”,還可以用代碼動態(tài)生成。動態(tài)生成控件,對需要相同的大量的控件還是比較有用的。

實現(xiàn)方式

1、加載數(shù)據(jù),往panel添加Label 控件。

  1. private void LoadRoomType() 
  2.         { 
  3.             DataTable dtRoomType = _roomTypeBLL.GetModelList("""Code"); 
  4.             pnlRoomType.Controls.Clear(); 
  5.             int padding = 5; 
  6.             int x = padding, y = padding; 
  7.             pnlRoom.Controls.Clear(); 
  8.             foreach (DataRow item in dtRoomType.Rows
  9.             { 
  10.                 Label lbl = new Label(); 
  11.                 lbl.Text = string.Format("{0}", item["Names"]); 
  12.                 lbl.Image = btnRoomType.Image; 
  13.                 lbl.Cursor = Cursors.IBeam; 
  14.                 lbl.TextAlign = btnRoomType.TextAlign; 
  15.                 lbl.Font = btnRoomType.Font; 
  16.                 lbl.ForeColor = btnRoomType.ForeColor; 
  17.                 lbl.Size = btnRoomType.Size
  18.                 lbl.Location = new Point(x, y); 
  19.                 lbl.Tag = item; 
  20.                 lbl.Click += new EventHandler(lbl_Click); 
  21.                 lbl.MouseEnter += new EventHandler(lbl_MouseEnter); 
  22.                 lbl.MouseLeave += new EventHandler(lbl_MouseLeave); 
  23.                 x += lbl.Width + padding; 
  24.                 if (x + lbl.Width > pnlRoomType.Width) 
  25.                 { 
  26.                     x = padding; 
  27.                     y += lbl.Height + padding; 
  28.                 } 
  29.                 pnlRoomType.Controls.Add(lbl); 
  30.             } 
  31.             int height = y + (x != padding ? pnlRoomType.Height : 0) + padding; 
  32.             int addHeight = height - pnlRoomType.Height; 
  33.             pnlRoom.Top = pnlRoom.Top + addHeight; 
  34.             pnlRoom.Height = pnlRoom.Height - addHeight; 
  35.             pnlRoomType.Height = pnlRoomType.Height + addHeight; 
  36.             if (dtRoomType.Rows.Count > 0) 
  37.                 LoadRoomByTypeID(dtRoomType.Rows[0], 0); 
  38.         } 

2、定義Label 的點擊事件。

  1. void lbl_Click(object sender, EventArgs e) 
  2.         { 
  3.             try 
  4.             { 
  5.                 Label lbl = sender as Label; 
  6.                 DataRow row = lbl.Tag as DataRow; 
  7.                 LoadRoomByTypeID(row, 0); 
  8.                 
  9.             } 
  10.             catch (Exception ex) 
  11.             { 
  12.  
  13.                ; 
  14.             } 
  15.  
  16.         } 

3、定義Label 的鼠標事件。

  1. #region lbl_MouseLeave 
  2.         void lbl_MouseLeave(object sender, EventArgs e) 
  3.         { 
  4.             Label lbl = sender as Label; 
  5.             lbl.Font = new Font(lbl.Font, FontStyle.Regular); 
  6.             lbl.Cursor = Cursors.Default
  7.             lbl.ForeColor = btnRoomType.ForeColor; 
  8.         } 
  9.         #endregion 
  10.         #region lbl_MouseEnter 
  11.         void lbl_MouseEnter(object sender, EventArgs e) 
  12.         { 
  13.             Label lbl = sender as Label; 
  14.             lbl.Font = new  Font(lbl.Font, FontStyle.Bold); 
  15.             lbl.Cursor = Cursors.IBeam; 
  16.         } 
  17.         #endregion 

效果

 

責任編輯:武曉燕 來源: UP技術(shù)控
相關(guān)推薦

2024-03-05 10:39:42

2009-09-01 10:35:59

C# WinForm控

2009-09-11 10:41:20

C# WinForm控

2009-09-11 12:52:09

C# WinForm控編輯器

2009-10-10 14:54:44

treeView1控件

2009-09-11 12:31:15

C# WinForm控設(shè)置默認值

2009-09-11 11:33:58

C# WinForm控Attribute

2009-09-11 12:07:12

C# WinForm控

2009-09-07 03:58:42

WinForm傳值

2009-09-02 18:41:18

C#定義動態(tài)控件數(shù)組

2009-08-17 15:48:47

C# WinForm進

2009-09-11 11:04:23

C# WinForm自

2010-01-25 11:05:14

C++Test

2012-05-14 10:14:42

WinForm

2009-12-28 10:40:13

WPF調(diào)用Winfor

2009-08-20 09:30:03

C#開發(fā)WinForm

2009-08-20 10:24:52

C#開發(fā)WinForm

2009-08-07 13:03:10

C#控件數(shù)組

2020-04-15 11:07:31

C語言對象思想

2010-01-20 15:00:09

Visual C++開
點贊
收藏

51CTO技術(shù)棧公眾號