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
    文章千古事,得失寸心知。


  • 相关阅读:
    node.js
    重学css
    MongoDB
    改写radio样式
    js系统总结
    vue+koa2商城实战学习笔记
    在Linux上配置unixODBC和FreeTDS访问MS SQL Server
    ASP.NET中文件上传下载方法集合
    SQL SERVER 分页查询存储过程
    Delphi7调用C#写的Webservice
  • 原文地址:https://www.cnblogs.com/bimgoo/p/2437367.html
Copyright © 2011-2022 走看看