最近看到很多人对C#运行期控件设计的问题,于是写了这个Memo希望对大家有用
1、实现了控件自由拖动
2、实现了控件的拖动创建,右键删除等类似IDE的控件创建,当然更多功能靠大家自己完善
3、实现属性框与控件的绑定,可以在运行期修改控件的Text...
以下是部分代码
view plaincopy to clipboardprint?
- private void button2_Click(object sender, EventArgs e)
- {
-
- if (panel1.Visible == true)
- {
- button2.Text = "+ 控件框";
- panel1.Visible = false;
- }
- else
- {
- button2.Text = "- 控件框";
- panel1.Visible = true;
- }
- }
view plaincopy to clipboardprint?
- private void button3_MouseDown(object sender, MouseEventArgs e)
- {
-
- if (e.Button == MouseButtons.Left)
- {
- Button btn = (Button)(sender);
-
- btn.DoDragDrop(btn, DragDropEffects.Copy);
- }
- }
-
- private void panel4_DragDrop(object sender, DragEventArgs e)
- {
-
- Button btn = (Button)(e.Data.GetData("System.Windows.Forms.Button"));
- Button btn_new = new Button();
- btn_new.ContextMenuStrip = contextMenuStrip1;
- btn_new.Name = btn_new.Text = btn.Text + "--" + name;
- btn_new.Left = PointToClient(MousePosition).X-panel4.Left;
- btn_new.Top = PointToClient(MousePosition).Y - panel4.Top;
-
- btn_new.Click += new System.EventHandler(this.button1_Click);
- btn_new.MouseLeave += new System.EventHandler(this.button1_MouseLeave);
- btn_new.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
- btn_new.MouseMove += new System.Windows.Forms.MouseEventHandler(this.button1_MouseMove);
- btn_new.Parent = panel4;
- name++;
- }
view plaincopy to clipboardprint?
- private void panel4_DragEnter(object sender, DragEventArgs e)
- {
- e.Effect = DragDropEffects.Copy;
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- groupBox1.Text = (sender as Button).Name + "属性";
- textBox1.Text = (sender as Button).Text;
-
- }
-
- private void button4_MouseDown(object sender, MouseEventArgs e)
- {
-
-
- if (e.Button == MouseButtons.Left)
- {
- Button btn = (Button)(sender);
-
- btn.DoDragDrop(btn, DragDropEffects.Copy);
- }
- }
view plaincopy to clipboardprint?
- private void toolStripMenuItem2_Click(object sender, EventArgs e)
- {
-
- btnflag.Dispose();
- }
-
- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
- btnflag.Text = textBox1.Text;
- }
-
- private void textBox1_KeyDown(object sender, KeyEventArgs e)
- {
-
- if (e.KeyValue == 13)
- btnflag.Text = textBox1.Text;
-
- }
-
- private void button7_Click(object sender, EventArgs e)
- {
-
- if (groupBox1.Visible == true)
- {
- button7.Text = "+ 属性窗口";
- groupBox1.Visible = false;
- }
- else
- {
- button7.Text = "- 属性窗口";
- groupBox1.Visible = true;
- }
- }
实现的效果图如下
更多功能当然需要大家自己扩展,假如你有兴趣...