DragDrop between controls - c#

I have a problem with DragDrop.
private void Form0_Load(object sender, EventArgs e)
{
PictureBox panel1 = new PictureBox();
PictureBox panel2 = new PictureBox();
mainPanel.Dock = DockStyle.Fill;
this.Controls.Add(mainPanel);
panel1.Location = new Point(10, 10);
panel1.Size = new System.Drawing.Size(500, 300);
panel1.BorderStyle = BorderStyle.FixedSingle;
Button b2 = new Button();
b2.Location = new Point(10, 10);
panel2.Controls.Add(b2);
panel2.Location = new Point(10, 10);
panel2.Size = new System.Drawing.Size(200, 100);
panel2.BorderStyle = BorderStyle.FixedSingle;
foreach (Control c in panel1.Controls)
{
c.MouseDown += new MouseEventHandler(control_MouseDown);
c.MouseMove += new MouseEventHandler(control_MouseMove);
c.MouseUp += new MouseEventHandler(control_MouseUp);
c.AllowDrop = true;
}
panel1.AllowDrop = true;
panel1.DragEnter += new DragEventHandler(container_DragEnter);
panel1.DragDrop += new DragEventHandler(container_DragDrop);
panel1.DragOver += new DragEventHandler(container_DragOver);
foreach (Control c in panel2.Controls)
{
c.MouseDown += new MouseEventHandler(control_MouseDown);
c.MouseMove += new MouseEventHandler(control_MouseMove);
c.MouseUp += new MouseEventHandler(control_MouseUp);
c.AllowDrop = true;
}
panel2.AllowDrop = true;
panel2.DragEnter += new DragEventHandler(container_DragEnter);
panel2.DragDrop += new DragEventHandler(container_DragDrop);
panel2.DragOver += new DragEventHandler(container_DragOver);
mainPanel.Controls.Add(panel1);
mainPanel.Controls.Add(panel2);
mainPanel.Controls.Add(pb);
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
isDragging = true;
clickOffsetX = e.X;
clickOffsetY = e.Y;
}
private void control_MouseMove(object sender, MouseEventArgs e)
{
Control c = sender as Control;
if (isDragging == true)
{
c.Left = e.X + c.Left - clickOffsetX;
c.Top = e.Y + c.Top - clickOffsetY;
if (c.Location.X + clickOffsetX > c.Parent.Width ||
c.Location.Y + clickOffsetY > c.Parent.Height ||
c.Location.X + clickOffsetX < 0 ||
c.Location.Y + clickOffsetY < 0)
c.DoDragDrop(c, DragDropEffects.Move);
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
void container_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void container_DragEnter(object sender, DragEventArgs e)
{
//e.Effect = DragDropEffects.Move;
//if (e.Data.GetDataPresent(typeof(Bitmap)))
//{
// e.Effect = DragDropEffects.Copy;
//}
//else
//{
// e.Effect = DragDropEffects.None;
//}
}
private void container_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
PictureBox p = sender as PictureBox;
mycontrol = c;
isDragging = false;
if (c != null)
{
c.Location = p.PointToClient(new Point(e.X, e.Y));
p.Controls.Add(c);
}
}
This is a working example. But I can't do drop Controls from parent to child control. What is a magic? How to drop control to another control (from panel1 to panel2 in my example).

There are some answers here in SO, which may help you:
See this Move controls when Drag and drop on panel in C#
this is a complete example on how to host the Form Designer:
Tailor Your Application by Building a Custom Forms Designer with .NET
Check this one also for simple lable drag drop:
Basic drag and drop in WinForms

Related

Control flicker while dragging in run time

Stack Overflow users. In a C# VS 2010 Windows Form project I have a problem regarding control flicker when dragging a user created control around on a tab page during run time. I used the following code:
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
Point newPoint = ((Control)sender).PointToScreen(new Point(e.X,
e.Y));
newPoint.Offset(ptOffset);
((Control)sender).Location = newPoint;
((Control)sender).Refresh();
}
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragged = true;
Point ptStartPosition = ((Control)sender).PointToScreen(new
Point(e.X, e.Y));
ptOffset = new Point();
ptOffset.X = ((Control)sender).Location.X - ptStartPosition.X;
ptOffset.Y = ((Control)sender).Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
((Control)sender).Refresh();
isDragged = false;
}
private void createButton_PB_Click(object sender, EventArgs e)
{
int ctrlExists = 0;
string btnName = btnName_TB.Text;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Text == btnName)
{
ctrlExists = 1;
}
}
if (btnName_TB.Text != "" && ctrlExists == 0)
{
Button newButton = new Button();
newButton.Name = btnName.Replace(" ", String.Empty);
newButton.Name += "u";
newButton.Text = btnName;
tabControl1.SelectedTab.Controls.Add(newButton);
newButton.Left = 10;
newButton.Top = 420;
lastBtnClicked = newButton;
}
SetupClickEvents(tabControl1.SelectedTab);
}
So, the problem is that I can add a button and drag it around in run time. But, when I add another Button and drag it around...after I've done that, and go back to trying to drag the first button, that button flickers and acts as if it is trying to move all over the place. Sometimes it disappears. I feel like this has something to do with the fact that the controls are inside a tab page. Perhaps I am not properly calculating the "newPoint" variable. Any ideas guys?
Ok, so I found a few fundamental flaws related to the button creation and the events that were being added at the time of creation. I made some considerable changes and the issue seems to have gone away. Following is the updated code.
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
Point newPoint = ((Control)sender).PointToScreen(new Point(e.X,
e.Y));
newPoint.Offset(ptOffset);
((Control)sender).Location = newPoint;
((Control)sender).Refresh();
}
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && checkBox1.Checked)
{
isDragged = true;
((Control)sender).MouseMove += new
MouseEventHandler(control_MouseMove);
Point ptStartPosition = ((Control)sender).PointToScreen(new
Point(e.X, e.Y));
ptOffset = new Point();
ptOffset.X = ((Control)sender).Location.X - ptStartPosition.X;
ptOffset.Y = ((Control)sender).Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
((Control)sender).MouseMove -= control_MouseMove;
((Control)sender).Refresh();
isDragged = false;
}
private void SetupClickEvents(Control control)
{
control.Click += new EventHandler(StoreLastClick);
control.MouseDown += new MouseEventHandler(control_MouseDown);
//control.MouseMove += new MouseEventHandler(control_MouseMove);
control.MouseUp += new MouseEventHandler(control_MouseUp);
}
private void createButton_PB_Click(object sender, EventArgs e)
{
ctrlExists = 0;
string btnName = btnName_TB.Text;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Name == btnName)
{
ctrlExists = 1;
}
}
if (btnName_TB.Text != "" && ctrlExists == 0)
{
Button newButton = new Button();
newButton.Name = btnName.Replace(" ", String.Empty);
newButton.Text = btnName;
tabControl1.SelectedTab.Controls.Add(newButton);
newButton.Left = 10;
newButton.Top = 420;
SetupClickEvents(newButton);
}
}
private void deleteButton_PB_Click(object sender, EventArgs e)
{
ctrlExists = 0;
if (lastCtrlClicked != null)
{
string btnName = lastCtrlClicked.Name;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Name == btnName)
{
ctrlExists = 1;
}
}
}
if (ctrlExists == 1 && lastCtrlClicked != null)
{
tabControl1.SelectedTab.Controls.Remove(lastCtrlClicked);
lastCtrlClicked.Dispose();
ctrlExists = 0;
}
lastCtrlClicked = null;
}

How to identify dynamically created panels in c#?

I am doing a Windows Form Application in c# in which I create a panel for every click i do in "Panel_Inside".
But my problem is when I have to select one especific panel. I do not know how can I identify each panel.
Also I'm having problems when Dragging and Dropping for the same reason.
public void Coloco_Figura(string figura, int Punto_X, int Punto_Y)
{
panel_foto = new Panel();
panel_foto.BackColor = Color.Transparent; //saco fondo
panel_foto.BackgroundImage = Image.FromFile(figura); //asigno imagen al panel
panel_foto.BackgroundImageLayout = ImageLayout.Stretch;
panel_foto.Size = new Size(45, 45);
panel_foto.Location = new Point(Punto_X - 10, Punto_Y - 10);
panel_foto.BringToFront();
panel1.SendToBack();
panel_inside.Controls.Add(panel_foto);
dame_x = Punto_X;
this.panel_foto.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDown);
this.panel_foto.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUp);
this.panel_foto.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MouseMove);
}
here are mouse events
private void MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
# region Borrar Notas
if (Estado_Borro == true)
{
foreach (Panel p in panel_inside.Controls)
{
if (p is Panel)
{
panel_inside.Controls.Remove(p);
if (panel_inside.Controls.Count == 0)
{
listanotas.Clear();
}
else
{
for (int i = 0; i < listanotas.Count; i++)
{
Notas nota = listanotas[i];
if (nota.posX == dame_x)
{
listanotas.Remove(nota);
}
}
}
}
}
Estado_Borro = false;
}
if (e.Button == MouseButtons.Left)
{
drag = true;
x = e.X;
y = e.Y;
}
}
public void MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
drag = false;
}
public void MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (drag)
{
this.panel_foto.Location = new Point(Cursor.Position.X-this.Left, Cursor.Position.Y-this.Top);
}
To identify each panel, first you must set the Name or Tag property:
panel_foto.Name = "panel_foto1";
Then in your event handler,do something like this:
Panel p=(Panel)sender;
if (p.Name == "panel_foto1")
{
//Do your staff
}

C# Drawing Lines with TextBox

I have an application where I can add a textBox on the screen and move it.
When I add more than two textBox, I double-click on top of two textbox and a line connects both.
My question is: How to make the line move along with the textBox?
code below:
public partial class principal : Form
{
int posMouseFormX, posMouseFormY;
int posMouseTXT_X, posMouseTXT_Y;
int posActTXT_X, posActTXT_Y;
bool txtPressionado = false;
int qntClick;
Pen myPen = new Pen(System.Drawing.Color.DarkGreen, 1);
Graphics Tela;
List<TextBox> listaNós = new List<TextBox>();
List<Point> origem = new List<Point>();
List<Point> destino = new List<Point>();
Point ponto1, ponto2;
ContextMenuStrip menu;
public principal()
{
InitializeComponent();
menu = new ContextMenuStrip();
menu.Items.Add("Remover");
menu.ItemClicked += new ToolStripItemClickedEventHandler(contextMenuStrip1_ItemClicked);
}
//TextBox event when the mouse moves over the TXT
private void txtMover_MouseMove(object sender, MouseEventArgs e)
{
TextBox textBox = sender as TextBox;
posMouseFormX = textBox.Location.X + e.Location.X;
posMouseFormY = textBox.Location.Y + e.Location.Y;
if (txtPressionado == true) moverTxt(textBox);
}
//Retrieve the X and Y coordinates where clicked within the component.
private void txtMover_MouseDown(object sender, MouseEventArgs e)
{
posMouseTXT_X = e.Location.X;
posMouseTXT_Y = e.Location.Y;
txtPressionado = true;
}
private void txtMover_MouseUp(object sender, MouseEventArgs e)
{
txtPressionado = false;
}
private void moverTxt(TextBox a)
{
a.Location = new System.Drawing.Point(posMouseFormX - posMouseTXT_X, posMouseFormY - posMouseTXT_Y);
posActTXT_X = a.Location.X;
posActTXT_Y = a.Location.Y;
System.Drawing.Graphics graphicsObj;
graphicsObj = this.CreateGraphics();
}
//insert new TextBox
private void sb_Inserir_No_Click(object sender, EventArgs e)
{
TextBox noFilho = new TextBox();
noFilho = new System.Windows.Forms.TextBox();
noFilho.Location = new System.Drawing.Point(379, 284);
noFilho.Size = new System.Drawing.Size(100, 30);
noFilho.TabIndex = 20;
noFilho.Text = "";
noFilho.BackColor = Color.White;
posActTXT_X = noFilho.Location.X;
posActTXT_Y = noFilho.Location.Y;
this.Controls.Add(noFilho);
noFilho.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
noFilho.DoubleClick += new System.EventHandler(this.textBox1_Click);
noFilho.MouseUp += new System.Windows.Forms.MouseEventHandler(txtMover_MouseUp);
noFilho.MouseDown += new System.Windows.Forms.MouseEventHandler(txtMover_MouseDown);
noFilho.MouseMove += new System.Windows.Forms.MouseEventHandler(txtMover_MouseMove);
noFilho.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
noFilho.ContextMenuStrip = menu;
}
//event to resize the txt on the screen as the content.
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox textBox1 = sender as TextBox;
Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
textBox1.Width = size.Width + 10;
textBox1.Height = size.Height;
}
//Event to control the connection between two give us when double click on the textbox
private void textBox1_Click(object sender, EventArgs e)
{
TextBox textBox1 = sender as TextBox;
int meio = textBox1.Size.Width / 2;
Tela = CreateGraphics();
qntClick = qntClick + 1;
if (this.qntClick == 1)
{
origem.Add(ponto1);
ponto1 = new Point(textBox1.Location.X + meio, textBox1.Location.Y);
}
if (this.qntClick == 2)
{
qntClick = 0;
destino.Add(ponto2);
ponto2 = new Point(textBox1.Location.X + meio, textBox1.Location.Y);
DesenhaSeta(Tela, ponto1, ponto2);
}
}
//draw arrow between two TXT
void DesenhaSeta(Graphics Tela, Point x, Point y)
{
myPen.StartCap = LineCap.Triangle;
myPen.EndCap = LineCap.ArrowAnchor;
Tela.DrawLine(myPen, x, y);
}
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
ContextMenuStrip menu = sender as ContextMenuStrip;
//recuperando o controle associado com o contextmenu
Control sourceControl = menu.SourceControl;
DialogResult result = MessageBox.Show("Tem Certeza que deseja remover o nó selecionado?", "Excluir", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(result == DialogResult.Yes)
{
sourceControl.Dispose();
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
// Determine whether the key entered is the F1 key. Display help if it is.
if (e.KeyCode == Keys.Space)
{
TextBox textBox1 = sender as TextBox;
novoNó tela = new novoNó(textBox1.Text);
tela.Show();
}
}
}
Each control, TextBox included has a Move, event. Put an Invalidate() call there!
The lines should be drawn in the Paint event of the container that holds the TextBoxes, probably the Form; if it is the Form indeed call this.Invalidate().
Please move the line drawing code out of the DoubleClick event into the Paint event or else the lines will not persist, say minimize/maximize events or other situation, when the system has to redraw the application!
You probably will need to create a data structure to maintain information about which TextBox-pairs need to be connected, maybe a List<Tuple> or a List<someStructure>. This would get filled/modified in the DoubleClick event, then call this.Invalidate() and in the Form.Paint you have a foreach loop over the list of TextBox-pairs..
If you are drawing on the Form do make sure to turn DoubleBuffered on!
Update: To compare the reults here is a minimal example the expects two TextBoxes on a Form:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.DoubleBuffered = true;
pairs.Add(new Tuple<Control, Control>(textBox1, textBox2));
}
List<Tuple<Control, Control>> pairs = new List<Tuple<Control, Control>>();
Point mDown = Point.Empty;
private void Form2_Paint(object sender, PaintEventArgs e)
{
foreach (Tuple<Control, Control> cc in pairs)
drawConnection(e.Graphics, cc.Item1, cc.Item2);
}
void drawConnection(Graphics G, Control c1, Control c2)
{
using (Pen pen = new Pen(Color.DeepSkyBlue, 3f) )
{
Point p1 = new Point(c1.Left + c1.Width / 2, c1.Top + c1.Height / 4);
Point p2 = new Point(c2.Left + c2.Width / 2, c2.Top + c2.Height / 4);
G.DrawLine(pen, p1, p2);
}
}
void DragBox_MouseDown(object sender, MouseEventArgs e)
{
mDown = e.Location;
}
void DragBox_MouseMove(object sender, MouseEventArgs e)
{
TextBox tb = sender as TextBox;
if (e.Button == MouseButtons.Left)
{
tb.Location = new Point(e.X + tb.Left - mDown.X, e.Y + tb.Top - mDown.Y);
}
}
void DragBox_MouseUp(object sender, MouseEventArgs e)
{
mDown = Point.Empty;
}
private void DragBox_Move(object sender, EventArgs e)
{
this.Invalidate();
}
}

C# drag drop reselecting a picturebox to mve from form

So i am having a project where i need to drag and drop different pictureboxes and make an existing copy of them to the form when i drag drop them. My problem is that i cannot move the selected "picture box" after it is created on the form. I would like to have an option where i can move any picturebox that is dragged not like dragging at a position and keeping it at that position.
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
p = (PictureBox)sender;
p.Tag = p.Location;
downPoint = e.Location;
p.Parent = this;
p.BringToFront();
}
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
p = (PictureBox)sender;
if (e.Button == MouseButtons.Left)
{
p.Left += e.X - downPoint.X ;
p.Top += e.Y - downPoint.Y ;
}
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
p = (PictureBox)sender;
PictureBox PB = new PictureBox();
Control c = GetChildAtPoint(new Point(p.Left -1, p.Top));
if (c == null) c = this;
Point newLoc = c.PointToClient(p.Parent.PointToScreen(p.Location));
PB.Parent = c;
PB.Location = newLoc;
;
p.Parent.Controls.Add(PB); // <-- add new PB to the form!
p.Location = (Point)p.Tag;
// put the original back where it started:
}
Put the events to the new picturebox
PB.MouseMove += new MouseEventHandler(pictureBox_MouseMove);
PB.MouseDown += new MouseEventHandler(picturebOX_MouseDown);
PB.MouseUp += new MouseEventHandler(picturebOX_MouseUp);

How to Drag and Drop(copy) from one panel to another panel

I have two panels on a form.
One panel has some controls like buttons or images and second panel is empty. I want to drag a control from panel 1 and drop it to panel 2, but it should create a copy of control, and while dragging a rectangle should be shown of same size as control and when dropped in panel 2 the dragged shape should appear there at mouse position
Actually I want to create a simulator like thing. That has some tools in panel 1 and when someone drag and drop a tool on panel 2 it should appear there at mouse position.
Language doesn't matter may be C# or VB.NET
Did you try something like this ?
private void Form5_Load(object sender, EventArgs e)
{
this.panel1.AllowDrop = true;
foreach (Control c in this.panel1.Controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
}
this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.Move);
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
if (c != null)
{
c.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
this.panel1.Controls.Add(c);
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
VB.NET
Private Sub Form5_Load(sender As Object, e As EventArgs)
Me.panel1.AllowDrop = True
For Each c As Control In Me.panel1.Controls
c.MouseDown += New MouseEventHandler(AddressOf c_MouseDown)
Next
Me.panel1.DragOver += New DragEventHandler(AddressOf panel1_DragOver)
Me.panel1.DragDrop += New DragEventHandler(AddressOf panel1_DragDrop)
End Sub
Private Sub c_MouseDown(sender As Object, e As MouseEventArgs)
Dim c As Control = TryCast(sender, Control)
c.DoDragDrop(c, DragDropEffects.Move)
End Sub
Private Sub panel1_DragDrop(sender As Object, e As DragEventArgs)
Dim c As Control = TryCast(e.Data.GetData(e.Data.GetFormats()(0)), Control)
If c IsNot Nothing Then
c.Location = Me.panel1.PointToClient(New Point(e.X, e.Y))
Me.panel1.Controls.Add(c)
End If
End Sub
Private Sub panel1_DragOver(sender As Object, e As DragEventArgs)
e.Effect = DragDropEffects.Move
End Sub
Source
Am changing a little of code of #Shim.
Here is the updated code in which a copy of your control will be placed in another panel
Random rnd = new Random();
private void Form5_Load(object sender, EventArgs e)
{
this.panel1.AllowDrop = true;
foreach (Control c in this.panel1.Controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
}
this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.Move);
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
// Here, you get a copy of your drag drop button and dynamically new button is created
Button btn = new Button();
btn.Name = "Button" + rnd.Next();
btn.Size = c.Size;
if (c != null)
{
// Add the newly created button to you Panel
btn.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
this.panel1.Controls.Add(btn);
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
this solution will drag the button(or any other component selected) while moving the mouse and place it on the place where you drop it
private SimpleButton selectedButton;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
xtraScrollableControl2.AllowDrop = true;
xtraScrollableControl2.DragEnter += XtraScrollableControl_DragEnter;
xtraScrollableControl2.DragDrop += XtraScrollableControl_DragDrop;
xtraScrollableControl2.DragOver += XtraScrollableControl_DragOver;
}
private void XtraScrollableControl_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.Data.GetDataPresent(typeof(Bitmap)) ? DragDropEffects.Copy : DragDropEffects.None;
}
private void XtraScrollableControl_DragDrop(object sender, DragEventArgs e)
{
var simpleButton = e.Data.GetData(e.Data.GetFormats()[0]) as SimpleButton;
if (simpleButton == null) return;
if (simpleButton.Parent != sender)
{
var btn = new SimpleButton
{
Dock = DockStyle.None,
Size = new Size(125, 50),
Text = simpleButton.Text,
Location = ((XtraScrollableControl) sender).PointToClient(new Point(e.X, e.Y)),
ImageList = simpleButton.ImageList,
ImageIndex = simpleButton.ImageIndex,
ImageLocation = simpleButton.ImageLocation,
Parent = ((XtraScrollableControl)sender)
};
btn.MouseDown += simpleButton_MouseDown;
((XtraScrollableControl)sender).Controls.Add(btn);
}
else
{
((XtraScrollableControl)sender).Controls.Remove(simpleButton);
simpleButton.Location = ((XtraScrollableControl)sender).PointToClient(new Point(e.X, e.Y));
((XtraScrollableControl)sender).Controls.Add(simpleButton);
}
selectedButton = null;
}
private void XtraScrollableControl_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
selectedButton.Location = ((XtraScrollableControl)sender).PointToClient(new Point(e.X, e.Y));
}
private void simpleButton_MouseDown(object sender, MouseEventArgs e)
{
var btn = sender as SimpleButton;
if (btn == null) return;
selectedButton = btn;
btn.DoDragDrop(btn, DragDropEffects.Copy);
}
hope this will help some
i used DevExpress components
but for the standart
DevExpress
XtraScrollableControl
SimpleButton
Microsoft
Panel
Button

Categories