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;
Related
I'm trying to put text in a specific cell using drag and drop screenshot of my screen
There is a simplified solution provided here, please check -
https://www.codeproject.com/Questions/101532/How-to-apply-drag-drop-cell-content-in-datagridvie
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//allow drop
this.dataGridView1.AllowDrop = true;
//data grid view sample data
DataTable dtb = new DataTable();
dtb.Columns.AddRange(new DataColumn[] { new DataColumn(), new DataColumn(), new DataColumn() });
for (int i = 0; i < 30;)
{
dtb.Rows.Add((++i).ToString(), (++i).ToString(), (++i).ToString());
}
dataGridView1.DataSource = dtb;
}
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
dataGridView1.DoDragDrop(dataGridView1[e.ColumnIndex,e.RowIndex].FormattedValue, DragDropEffects.Copy);
}
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
string cellvalue=e.Data.GetData(typeof(string)) as string;
Point cursorLocation=this.PointToClient(new Point(e.X,e.Y));
System.Windows.Forms.DataGridView.HitTestInfo hittest= dataGridView1.HitTest(cursorLocation.X,cursorLocation.Y);
if (hittest.ColumnIndex != -1
&& hittest.RowIndex != -1)
dataGridView1[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
}
I'm a beginner in C# Windows Forms. I tried to google this but not sure I understand how this is possible. I want to create a Listbox under run time, and succed making one like this:
private void button3_Click(object sender, EventArgs e)
{
ListBox lb = new ListBox();
lb.AllowDrop = true;
lb.FormattingEnabled = true;
lb.Size = new System.Drawing.Size(200, 100);
lb.Location = new System.Drawing.Point(100, 250);
this.Controls.Add(lb);
}
But I also need conditions in a function for my listbox, I want to add code in designer to add these too to the listbox. I want to add a function like this for example:
lb.DragEnter += new System.Windows.Forms.DragEventHandler(this.lb_DragEnter);
and
private void lb_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
I hope i explain my problem clear!
welcome to stack.
I might misunderstand what you're trying to do, but couldn't you just add the event in your button3_Click method?
private void button3_Click(object sender, EventArgs e)
{
ListBox lb = new ListBox();
lb.AllowDrop = true;
lb.FormattingEnabled = true;
lb.Size = new System.Drawing.Size(200, 100);
lb.Location = new System.Drawing.Point(100, 250);
// Your event
lb.DragEnter += new System.Windows.Forms.DragEventHandler(this.lb_DragEnter);
this.Controls.Add(lb);
}
private void lb_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
I have a C# project in which I want the user to be able to drag and drop text (i.e. to move text) within a RichTextBox in a WinForm.
I have found many examples showing how to do drop something onto a RichTextBox but I didn't succeed to have them work when the RichTextBox is both the drag source and drop target.
How should I do this ?
Below is my non-working attempt sofar.
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.richTextBox1.AllowDrop = true;
this.richTextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.richTextBox1_DragEnter);
this.richTextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.richTextBox1_DragDrop);
}
private void richTextBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Rtf))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
private void richTextBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
int i;
String s;
i = richTextBox1.SelectionStart;
s = richTextBox1.Text.Substring(i);
richTextBox1.Text = richTextBox1.Text.Substring(0, i);
richTextBox1.Text = richTextBox1.Text +
e.Data.GetData(DataFormats.Text).ToString();
richTextBox1.Text = richTextBox1.Text + s;
}
}
Well, I found myself a solution to this problem :
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
richTextBox1.EnableAutoDragDrop = true;
}
}
And nothing more.
My first attempt was based on MSDN documentation:
http://msdn.microsoft.com/en-us/library/aa984395(v=vs.71).aspx, but it seems broken.
Drag and drop text within RXBox (RichTextBox):
Run HookOn_EventHandlers_etc() during Initialize(). Dragging mouse will fire the DragEnter and DragDrop events. In RXBox_DragDrop(), set DragDropEffects back to None.
private void HookOnEventHandlers_etc()
{
RXBox.DragEnter += RXBox_DragEnter;
RXBox.DragDrop += RXBox_DragDrop;
RXBox.AllowDrop = true;
RXBox.EnableAutoDragDrop = true;
}
private void RXBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void RXBox_DragDrop(object sender, DragEventArgs e)
{
RXBox.SelectedText = e.Data.GetData(DataFormats.Text).ToString();
e.Effect = DragDropEffects.None; // with this the paste won't be doubled
}
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();
}