I have a small file explorer application [WINFORMS], and i use ListView Control to explore items.
So the listView display the files and the folders from the current address [From local Computer].
i need to enable drag&drop functionality to let a File/Folder easy to move/copy to another Folder in the same Address.
each item has some preperties:
(item.Text/item.Name) has the file/folder Name.
item.ToolTipText has the file/folder Path.
item.SubItems[ 1 ].Text for a file it will respresent the file size like "13.45 MB" and for a folder, it's going to be string.Empty [However, there's several way to know whether its a file or a folder].
I've seen many tutorials about how to use drag&drop in listview, but it was like from Windows File Explorer to ListView, or from a ListView to another one, but in my case i need to know how to drag&drop in the same ListView.
I've Enabled AllowDrop property of the ListView. and also Activated the following functions:
private void listView1_DragDrop(object sender, DragEventArgs e)
{
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
}
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
}
UPDATE:
i tried to use this :
private void listView1_DragDrop(object sender, DragEventArgs e)
{
ListViewItem item = (ListViewItem)e.Data.GetData(typeof(ListViewItem)); //this should be the target item (FOLDER)
StringBuilder s = new StringBuilder();
foreach (ListViewItem i in listView1.SelectedItems)
{
s.AppendLine(i.Text);
}
MessageBox.Show("DRAGGED ITEM : " + s.ToString() + "TARGET ITEM : " + item.Text);
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
var item = listView1.GetItemAt(e.X, e.Y);
if (item != null)
{
listView1.DoDragDrop(item, DragDropEffects.Copy);
}
}
at listView1_MouseDown i get the item which mouse points onto, but it gives me the item before i drop the dragged item, so if i am dragging a folder named "SWImg" to the folder "ODDFiles" , the messageBox shows "SWImg - SWImg"
then i replaced listView1_MouseDown with listView1_ItemDrag :
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
if (e.Item != null)
{
listView1.DoDragDrop(e.Item, DragDropEffects.Copy);
}
}
same result :S.
I've figured it out.
private void listView1_DragDrop(object sender, DragEventArgs e)
{
if (listView1.SelectedItems.Count == 0) { return; }
Point p = listView1.PointToClient(MousePosition);
ListViewItem item = listView1.GetItemAt(p.X, p.Y);
if (item == null) { return; }
List<ListViewItem> collection = new List<ListViewItem>();
foreach (ListViewItem i in listView1.SelectedItems)
{
collection.Add((ListViewItem)i.Clone());
}
if ((e.Effect & DragDropEffects.Move) == DragDropEffects.Move)
{
Thread thMove = new Thread(unused => PasteFromMove(item.ToolTipText, collection));
thMove.Start();
}
else
{
Thread thCopy = new Thread(unused => PasteFromCopy(item.ToolTipText, collection));
thCopy.Start();
}
}
private void listView1_DragOver(object sender, DragEventArgs e)
{
if ((e.KeyState & 8) == 8)
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.Move;
}
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
listView1.DoDragDrop(e.Item,DragDropEffects.All);
}
Related
I have a CheckedListBox with items from a database.
When I check an item in the CheckedListBox and after that I close the form and open the form again, the item is not checked any more, i.e. the "check" has not been saved.
How can I accomplish that if I check an item and then close the form and open it again, that the item is still checked?
I tried this:
void deliveries_FormClosing(object sender, FormClosingEventArgs e)
{
for (int i = 0; i < deliveries.ClbOrdersCheckDelivery.Items.Count; i++)
{
if (deliveries.ClbOrdersCheckDelivery.GetItemChecked(i) == true)
{
Properties.Settings.Default.CheckedItems = deliveries.ClbOrdersCheckDelivery.GetItemChecked(i);
}
}
}
]\
You need to write
Properties.Settings.Default.Save();
To save the settings.
Write it after the for loop.
EDIT
I tried the following code to save all checked items to settings file. It works. Please check.
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.CheckedItems = string.Empty;
foreach (var item in checkedListBox1.CheckedItems)
{
Properties.Settings.Default.CheckedItems += item + "," ;
}
Properties.Settings.Default.Save();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(Properties.Settings.Default.CheckedItems);
}
private void Form1_Load(object sender, EventArgs e)
{
var checkedItems = Properties.Settings.Default.CheckedItems.ToString().Split(',');
foreach (var item in checkedItems)
{
var index=checkedListBox1.FindString(item);
if(index>=0)
{
checkedListBox1.SetItemChecked(index, true);
}
}
}
i try to add the option to my application to drag file into my Listbox instead of navigate into the file folder and this is what i have try:
private void Form1_Load(object sender, EventArgs e)
{
listBoxFiles.AllowDrop = true;
listBoxFiles.DragDrop += listBoxFiles_DragDrop;
listBoxFiles.DragEnter += listBoxFiles_DragEnter;
}
private void listBoxFiles_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void listBoxFiles_DragDrop(object sender, DragEventArgs e)
{
listBoxFiles.Items.Add(e.Data.ToString());
}
but instead of the full file path e.Data.ToString() return System.Windows.Forms.DataObject
This code I found here:
private void Form1_Load(object sender, EventArgs e)
{
listBoxFiles.AllowDrop = true;
listBoxFiles.DragDrop += listBoxFiles_DragDrop;
listBoxFiles.DragEnter += listBoxFiles_DragEnter;
}
private void listBoxFiles_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
private void listBoxFiles_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
listBoxFiles.Items.Add(file);
}
I tried using #AsfK's answer in WPF, had to delete
listBoxFiles.DragDrop += listBoxFiles_DragDrop;
from public MainWindow() else I got the dragged files duplicated.
Thanks!
Here's my code:
void CutAction(object sender, EventArgs e)
{
richTextBox2.Cut();
}
void CopyAction(object sender, EventArgs e)
{
Clipboard.SetData(DataFormats.Rtf, richTextBox2.SelectedRtf);
Clipboard.Clear();
}
void PasteAction(object sender, EventArgs e)
{
if (Clipboard.ContainsText(TextDataFormat.Rtf))
{
richTextBox2.SelectedRtf
= Clipboard.GetData(DataFormats.Rtf).ToString();
}
}
private void richTextBox2_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{ //click event
//MessageBox.Show("you got it!");
ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
MenuItem menuItem = new MenuItem("Cut");
menuItem.Click += new EventHandler(CutAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Copy");
menuItem.Click += new EventHandler(CopyAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Paste");
menuItem.Click += new EventHandler(PasteAction);
contextMenu.MenuItems.Add(menuItem);
richTextBox2.ContextMenu = contextMenu;
}
}
I have 2 problems:
After marking the text in richTextbox2 I need to simulate a right click on the mouse to see the cut paste copy menu.
When I click on Copy I can't afterwards paste it anywhere because there is nothing to paste. I didn't test yet the cut and paste options but after doing copy it's not working.
Remove the Clipboard.Clear();
void CopyAction(object sender, EventArgs e) {
Clipboard.SetData(DataFormats.Rtf, richTextBox2.SelectedRtf);
}
You can also use the Copy() method of a RichTextBox:
void CopyAction(object sender, EventArgs e) {
richTextBox2.Copy();
}
For paste:
void PasteAction(object sender, EventArgs e) {
if (Clipboard.ContainsText(TextDataFormat.Rtf)) {
SendKeys.Send("^v");
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
richTextBox1.Copy();
}
private void btnPaste_Click(object sender, EventArgs e)
{
richTextBox2.Paste();
}
private void btnCut_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
richTextBox1.Cut();
}
Note:The windows forms has built in methods to copy the text from richTextBox to Clipboard.
like Copy, Paste, Cut (you have to select the text first which you want to copy or cut. Here in my example i had given select all text).
Here in this example basically it will copy the content to the clipboard, still there are overloading methods please see the definitions of the methods.
Try to add toolstripbar, add 3 toolstripbuttons. This is the code for copy, cut and paste
private void toolStripButton1_Click(object sender, EventArgs e)
{
SendKeys.Send("^x");
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
SendKeys.Send("^v");
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
SendKeys.Send("^c");
}
The code works directly with the clipboard.
thanks for your answer mr Doron Muzar
private void richTextBox2_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{ //click event
//MessageBox.Show("you got it!");
ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
MenuItem menuItem = new MenuItem("Cut");
menuItem.Click += new EventHandler(CutAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Copy");
menuItem.Click += new EventHandler(CopyAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Paste");
menuItem.Click += new EventHandler(PasteAction);
contextMenu.MenuItems.Add(menuItem);
richTextBox2.ContextMenu = contextMenu;
}
}
void CutAction(object sender, EventArgs e)
{
richTextBox1.Cut();
}
void CopyAction(object sender, EventArgs e)
{
richTextBox1.Copy();
}
void PasteAction(object sender, EventArgs e)
{`
richTextBox1.Paste();
}
I have a C# app and i want it that when an Image is dropped on the form, A picturebox in the form displays the Image. I have tried this
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
Graphics p = pictureBox1.CreateGraphics();
p.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap), new Point(0, 10));
}
But it doesn't work.
Pls what did i do wrong?
I think you are dragging from file.
Simple code for this will be such:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.AllowDrop = true;
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] filex = (string[])e.Data.GetData(DataFormats.FileDrop);
if (filex.Length > 0)
{
pictureBox1.ImageLocation = filex[0];
}
}
}
i truly hope this is not the solution but you didn't gave much information so i'll start here and we'll go as you give us more information.
when you toke this sample did you bind it to the proper events?
as in:
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
also, on initializing, did you do:
AllowDrop = true;
I am trying to clear the contents of a listbox when a new tab is seleced. Here is what I got, but nothing happens.
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabControl1.TabPages["entryTab"])
{
readBox.Items.Clear();
reminderBox.Items.Clear();
}
}
Try something like this in your form load
tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_SelectedIndexChanged);
// Try this set null to DataSource
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabControl1.TabPages["entryTab"])
{
readBox.DataSource = null;
reminderBox.DataSource = null;
}
}