I want to drag and drop between listbox and more than one textbox in both directions but when I double click an item in listbox it doubles. I don't know fow to change the drag over latency here is my code for the listbox
Mouse Down event
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (listBox1.Items.Count == 0)
return;
int index = listBox1.IndexFromPoint(e.X, e.Y);
string s = listBox1.Items[index].ToString();
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
if (dde1 == dragdropeffects.all)
{
listbox1.items.removeat(listbox1.indexfrompoint(e.x, e.y));
} ...
Drag and drop event
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
//if (e.Data.GetDataPresent(DataFormats.StringFormat))
//{
// string str = (string)e.Data.GetData(
// DataFormats.StringFormat);
// listBox1.Items.Add(str);
//}
}
Dragover event
private void listBox1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
Related
private void lbxMevcutOlmayanUnvanlar_DragOver(object sender, DragEventArgs e)
{
if (e.KeyState == 1)
{
e.Effect = DragDropEffects.Copy;
}
}
private void lbxMevcutOlmayanUnvanlar_DragDrop(object sender, DragEventArgs e)
{ MevcutOlmayanUnvanlarDataSource.Rows.Add((DataRow)e.Data.GetData(typeof(DataRow)), (DataRow)e.Data.GetData(typeof(DataRow)));//I cannot access the data in e.data
}
private void lbxMevcutUnvanlar_MouseDown(object sender, MouseEventArgs e)
{
Point point = new Point(e.X, e.Y);
int sira = lbxMevcutUnvanlar.IndexFromPoint(point);
if (e.Button == MouseButtons.Left)
{
lbxMevcutUnvanlar.DoDragDrop(lbxMevcutUnvanlar.Items[sira], DragDropEffects.Copy);
}
}
enter image description here
enter image description here
The data comes in rows one below the other. actually it should come as columns under the name of "bipkod" and "bipaciklama".
I have a problem. I have a group of picturebox to be dragged into one picturebox. How to disable a specific picturebox after being dragged?? So, it can't be dragged anymore.
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.All);
}
private void pictureBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
if ((e.Data.GetDataPresent(DataFormats.Bitmap)))
this.pictureBox2.Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));
}
Instead of dragging the PictureBox's Image, drag the PictureBox.
When dropped, set it's Tag property to true.
In the MouseDown event, check if the Tag property is null, and drag only if it is.
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && pictureBox1.Tag == null)
pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.All);
}
private void pictureBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(PictureBox)))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(PictureBox)))
{
var picturebox = ((PictureBox)e.Data.GetData(typeof(PictureBox)));
picturebox.Tag = true;
this.pictureBox2.Image = picturebox.Image;
}
}
I want to make a DragAndDrop program for my project. I would like to a drag a picture from one picturebox to another and that's working pretty nice. But it won't go back to the orignal picturebox. I am working with 6 picturebox with 3 on first row and 3 on second row. I have drag the picturebox from first row to second and combine with the right description. Is it possible to make only 3 events (mouse_down, dragenter, dragdrop) and stil every picturebox will work because now I have to make 3 events for each picturebox and like this way i need 18 events.... Please help meeee
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.AllowDrop = true;
pictureBox2.AllowDrop = true;
pictureBox3.AllowDrop = true;
pictureBox4.AllowDrop = true;
pictureBox5.AllowDrop = true;
pictureBox6.AllowDrop = true;
pictureBox1.MouseDown += MouseDown;
pictureBox2.MouseDown += MouseDown;
pictureBox1.DragEnter += pictureBox2_DragEnter;
pictureBox1.DragDrop += pictureBox2_DragDrop;
pictureBox2.DragEnter += pictureBox2_DragEnter;
pictureBox2.DragDrop += pictureBox2_DragDrop;
}
/*private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Move);
}*/
private void pictureBox2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
pictureBox2.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
pictureBox1.Image = null;
}private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
pictureBox2.DoDragDrop(pictureBox1.Image, DragDropEffects.Move);
}
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
pictureBox1.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
pictureBox2.Image = null;
}
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
pictureBox2.DoDragDrop(pictureBox1.Image, DragDropEffects.Move);
}
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
pictureBox1.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
pictureBox2.Image = null;
}
You should utilize the sender object you get in those events.
Instead of setting the drag object to an image, I changed it to the PictureBox itself:
void pictureBox_MouseDown(object sender, MouseEventArgs e) {
DoDragDrop(sender, DragDropEffects.Move);
}
Notice this isn't pictureBox1_MouseDown, but just pictureBox_MouseDown. All of your PictureBox controls should wire their MouseDown events to this one method:
pictureBox1.MouseDown += pictureBox_MouseDown;
pictureBox2.MouseDown += pictureBox_MouseDown;
// etc...
You continue this process for the other events, too:
void pictureBox_DragEnter(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.Move;
}
For the DragDrop event, cast the dragged object back into a PictureBox control:
void pictureBox_DragDrop(object sender, DragEventArgs e) {
PictureBox pb = e.Data.GetData(typeof(PictureBox)) as PictureBox;
if (pb != null) {
((PictureBox)sender).Image = pb.Image;
pb.Image = null;
}
}
[Solved] I want to drag some DataGridViewRows from a DataGridView, contained in a TabPage, to another DataGridView also contained in another TabPage. I have set the DataGridView's Event (How could I Drag and Drop DataGridView Rows under each other?), but i don't know how I can "navigate" between TabPages!
Here's a bare bone example how I can drag text from one textbox on a tab to another one on a separate tab:
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
textBox1.DoDragDrop(textBox1.Text, DragDropEffects.Copy | DragDropEffects.Move);
}
private void tabControl1_DragOver(object sender, DragEventArgs e)
{
Point location = tabControl1.PointToClient(Control.MousePosition);
for (int tab = 0; tab < tabControl1.TabCount; ++tab)
{
if (tabControl1.GetTabRect(tab).Contains(location))
{
tabControl1.SelectedIndex = tab;
break;
}
}
}
private void textBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void textBox2_DragDrop(object sender, DragEventArgs e)
{
textBox2.Text = e.Data.GetData(DataFormats.Text).ToString();
}
NOTE: You must set AllowDrop property to true on the TabControl, and of course on the destination control.
Cheers
I have solved the question by myself (and #mrlucmorin) :
internal void dgv_MouseDown(object sender, MouseEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
List<DataGridViewRow> result = new List<DataGridViewRow>();
foreach(DataGridViewRow row in dgv.SelectedRows)
{
result.Add(row);
}
dgv.DoDragDrop(result, DragDropEffects.Copy | DragDropEffects.Move);
}
private void dgv_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dgv_DragDrop(object sender, DragEventArgs e)
{
try
{
DataGridView dataGridView1 = (DataGridView)sender;
List<DataGridViewRow> rows = new List<DataGridViewRow>();
rows = (List<DataGridViewRow>)e.Data.GetData(rows.GetType());
//some stuff
}
}
I'm dragging and dropping the pictureBox in FlowlayoutPanel but the event return the position in form...I need this position in FlowLayoutPanel(the pictureBoxes are in FlowLayoutPanel and are dragging and drop there)
public Form1()
{
InitializeComponent();
flowLayoutPanel1.AllowDrop = true;
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
picBox = (PictureBox)sender;
if (picBox.Image != null)
{
var dragImage = new Bitmap((Bitmap)picBox.Image, picBox.Size);
IntPtr icon = dragImage.GetHicon();
Cursor.Current = new Cursor(icon);
DoDragDrop(picBox.Image, DragDropEffects.Copy);
}
}
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Bitmap)))
e.Effect = DragDropEffects.Copy;
}
// Occurs when the user releases the mouse over the drop target
void Form1_DragDrop(object sender, DragEventArgs e)
{
MessageBox.Show("Posicao x " + e.Y + "Posicao Y " + e.Y);//reutrn the position in form, not in flowLayoutPanel
}
You have to use this method:
var point = flowLayoutPanel1.PointToClient(new Point(e.X, e.Y));
This translates screen position coordinates into control one, more info here.