Drag and drop files into my listbox - c#

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!

Related

Invoking google search button

My application has one button one text box and a webbrowser control which is going to navigate at google.com at form load event.
If I am setting the text attributes to the google textbox in webBrowser1_DocumentCompleted method after clicking the button I am able to invoke google search button.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.google.co.in/?gfe_rd=cr&ei=u6PkV4X9LovZ8Aec7bI4&gws_rd=ssl");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textElement = webBrowser1.Document.All.GetElementsByName("q")[0];
textElement.SetAttribute("value", "facebook");
}
private void btnLogin_Click(object sender, EventArgs e)
{
ele = webBrowser1.Document.All.GetElementsByName("btnK")[0];
ele.InvokeMember("click");
}
If I am setting the text attributes to the google textbox in btnLogin_Click method after clicking the button i am not able to invoke google search button.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.google.co.in/?gfe_rd=cr&ei=u6PkV4X9LovZ8Aec7bI4&gws_rd=ssl");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textElement = webBrowser1.Document.All.GetElementsByName("q")[0];
}
private void btnLogin_Click(object sender, EventArgs e)
{
textElement.SetAttribute("value", "facebook");
ele = webBrowser1.Document.All.GetElementsByName("btnK")[0];
ele.InvokeMember("click");
}
Where I am going wrong please suggest me.

My RichTextBox's cut/copy/paste doesn't cut, copy, or paste

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();
}

Display dropped image on Form

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;

Web browser control not working to get text

I'm trying to get source code of a webpage and save it to richtextbox and that web browser control navigates to new URL. But I'm getting a blank rich text box. Here is my code:
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
webBrowser2.Navigate("http://www.gmail.com");
}
private void timer1_Tick(object sender, EventArgs e)
{
if(webBrowser2.ReadyState == WebBrowserReadyState.Complete)
{
timer1.Enabled = false;
richTextBox1.Text = webBrowser2.DocumentText;
webBrowser2.Navigate("new URL");
}
}
I see two alternatives to what you're doing.
1) Using DocumentCompleted:
private void Form_Load(object sender, EventArgs e) {
webBrowser.Navigate("www.google.com");
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
richTextBox.Text = webBrowser.DocumentText;
}
2) Using the WebClient:
private void Form_Load(object sender, EventArgs e) {
using (System.Net.WebClient wc = new System.Net.WebClient()) {
richTextBox.Text = wc.DownloadString("http://www.google.com");
}
}

how to Drag&Drop in the same ListView?

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);
}

Categories

Resources