1、实现一个可以通过鼠标左键按下时动态扩展大小的按钮?
public partial class Form1 : Form{
public Form1(){
InitializeComponent();
}
Point point;
Size size;
Button button;
private void Form1_Load(object sender, EventArgs e){
button = new Button();
button.Text = "测试";
button.MouseDown += Button_MouseDown;
button.MouseUp += Button_MouseUp;
this.Controls.Add(button);
}
private void Button_MouseDown(object sender, MouseEventArgs e){
if (e.Button == MouseButtons.Left){
point = Cursor.Position;
size = button.Size;
button.MouseMove += Button_MouseMove;
}
}
private void Button_MouseMove(object sender, MouseEventArgs e){
button.Width = size.Width + Cursor.Position.X - point.X;
button.Height = size.Height + Cursor.Position.Y - point.Y;
}
private void Button_MouseUp(object sender, MouseEventArgs e){
button.MouseMove -= Button_MouseMove;
}
}
2、实现一个可以将窗体上所有TextBox清空的方法?
foreach (var item in this.Controls){
if (item.GetType().Name == "textbox"){
(item as TextBox).Text = null;
}
}
|