https://youtu.be/MjciVKubIXY
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SampleCode
{
public partial class AddControlToPanel : Form
{
public AddControlToPanel()
{
InitializeComponent();
}
private void AddControlToPanel_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
/*
// method 1
if(comboBox1.Text == "Label")
{
Label label = new Label();
label.BackColor = Color.White;
label.Text = comboBox1.Text;
panel1.Controls.Add(label);
label.BringToFront();
label.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Control_MouseDown);
label.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Control_MouseMove);
}
else if(comboBox1.Text == "Button")
{
Button button = new Button();
button.BackColor = Color.White;
button.Text = comboBox1.Text;
panel1.Controls.Add(button);
button.BringToFront();
button.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Control_MouseDown);
button.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Control_MouseMove);
}
*/
/*
// method 2
Control control = new Control();
if(comboBox1.Text == "Label")
control = new Label();
else if(comboBox1.Text == "Button")
control = new Button();
else if(comboBox1.Text == "RadioButton")
control = new RadioButton();
else if(comboBox1.Text == "CheckBox")
control = new CheckBox();
else if(comboBox1.Text == "ComboBox")
control = new ComboBox();
else if(comboBox1.Text == "GroupBox")
control = new GroupBox();
else if(comboBox1.Text == "Panel")
control = new Panel();
else if(comboBox1.Text == "PictureBox")
control = new PictureBox();
else if(comboBox1.Text == "ListBox")
control = new ListBox();
control.BackColor = Color.White;
control.Text = comboBox1.Text;
panel1.Controls.Add(control);
control.BringToFront();
control.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Control_MouseDown);
control.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Control_MouseMove);
*/
// Note that if you're using a strongly named assembly (like Form in this case)
// you must include all the assembly information - versioning, public key token etc.
// method 3
string typeName = $"System.Windows.Forms.{comboBox1.Text}, System.Windows.Forms, "
+ "Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
Type conType = Type.GetType(typeName);
if(conType==null)
{
MessageBox.Show($"Type Invalid {comboBox1.Text}");
return ;
}
Control control = (Control)Activator.CreateInstance(conType);
control.BackColor = Color.White;
control.Text = comboBox1.Text;
panel1.Controls.Add(control);
control.BringToFront();
control.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Control_MouseDown);
control.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Control_MouseMove);
}
Point mousePoint ;
Size controlSize ;
private void Control_MouseDown(object sender, MouseEventArgs e)
{
Control con = sender as Control;
mousePoint = new Point(e.X, e.Y);
controlSize = con.Size;
}
private void Control_MouseMove(object sender, MouseEventArgs e)
{
Control con = sender as Control;
int xDiff = con.Size.Width - e.X;
int yDiff = con.Size.Height - e.Y;
if(xDiff<20 && yDiff<20)
{
Cursor.Current = Cursors.SizeNWSE;
}
if( (e.Button&MouseButtons.Left)!=MouseButtons.Left) return ;
if(xDiff<20 && yDiff<20)
{
con.Size = new Size(controlSize.Width - (mousePoint.X-e.X),
controlSize.Height - (mousePoint.Y-e.Y));
return ;
}
con.Location = new Point(con.Left - (mousePoint.X-e.X),
con.Top - (mousePoint.Y-e.Y));
}
}
}