zoukankan      html  css  js  c++  java
  • 动态添加控件

    本程序实现在程序运行中向窗体添加Button控件,点击按钮就会在窗体上新增一个Button实例,程序界面如下。

    20120409101323

    添加鼠标滑过按钮改变按钮颜色的代码,点击按钮,TextBox中显示按钮信息,程序代码如下。

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    namespace eg42_addCtrlAtRuntimeApp
    {
    	public partial class MainForm : Form
    	{
    		private int count;
    		public MainForm()
    		{
    			InitializeComponent();
    		}
    		
    		void Btn_MouseEnter(object sender, EventArgs e)
    		{
    			Button currentButton=(Button)sender;
    			currentButton.BackColor=Color.Blue;
    		}
    		
    		void Btn_MouseLeave(object sender, EventArgs e)
    		{
    			Button currentButton=(Button)sender;
    			currentButton.BackColor=System.Windows.Forms.Control.DefaultBackColor;
    		}
    		
    		void Btn_Click(object sender, MouseEventArgs e)
    		{
    			Button currentButton=(Button) sender;
    			txt_msg.Text="你点击了"+currentButton.Text;
    		}
    		
    		void Btn_addButtonMouseClick(object sender, MouseEventArgs e)
    		{
    			count+=2;
    			int localX=this.btn_addButton.Height*count;
    			int localY=10*count;
    			Button toAddButton=new Button();
    			toAddButton.Name="Button"+count;
    			toAddButton.Text="按钮"+count+"";
    			toAddButton.Location=new Point(localX,localY);
    			toAddButton.MouseEnter+=new EventHandler(this.Btn_MouseEnter);
    			toAddButton.MouseLeave+=new EventHandler(this.Btn_MouseLeave);
    			toAddButton.MouseClick+=new MouseEventHandler(this.Btn_Click);
    			this.Controls.Add(toAddButton);
    		}
    	}
    }
    作者:codee
    文章千古事,得失寸心知。


  • 相关阅读:
    jsp.图书馆
    jsp第七次作业
    jsp第六次作业
    jsp第四次作业
    JSP第二次作业
    软件测试课堂练习
    Android第六次作业
    Android第五次作业
    Android第四次作业
    Android第三次作业
  • 原文地址:https://www.cnblogs.com/bimgoo/p/2437367.html
Copyright © 2011-2022 走看看