When dragging I want to copy the panels, not moving them - c#

I want to drag panels from one tableLayoutPanel to another. I also want the panels to be copied, not moved; that is, I want them to be copied (from tableLayoutPanel1 to tableLayoutPanel2), leaving the item in tableLayoutPanel1.
Can I do this? If you can give me an idea, it will be great. Thank you
public Form1()
{
InitializeComponent();
panel1.AllowDrop = true;
panel2.AllowDrop = true;
panel3.AllowDrop = true;
panel1.DragEnter += panel_DragEnter;
panel2.DragEnter += panel_DragEnter;
panel1.DragDrop += panel_DragDrop;
panel2.DragDrop += panel_DragDrop;
}
private void panel3_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop(panel3, DragDropEffects.Copy);
}
private void panel3_MouseMove(object sender, MouseEventArgs e)
{
DoDragDrop(panel3, DragDropEffects.Copy);
}
private void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void panel_DragDrop(object sender, DragEventArgs e)
{
((Panel)e. Data . GetData(typeof(Panel))).Parent = (Panel)sender;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}

You need to create a new panel in the MouseDown handler, set its properties like those of the original, add it to the form and use DoDragDrop with this new panel.

Related

Hide Cursor on being moveless for certain amount of time and show it again on move

I want to hide the cursor after a certain time (if the mouse is not moving), and I want to show the cursor when I move the mouse in a picturebox. I just can't get it to work... This is what I have tried:
// this Never seem to hide the cursor
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Show();
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Hide();
tim.Stop();
}
-
// works but in this case I want cursor.ico to be a resource
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Current = Cursors.Default;
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Current = new Cursor("cursor.ico");
tim.Stop();
}
-
// Properties.Resources.cursor gives an error even though I added it to my resources
// cannot convert from 'System.Drawing.Icon' to 'System.IntPtr'
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Current = Cursors.Default;
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Current = new Cursor(Properties.Resources.cursor);
tim.Stop();
}
You need to have a timer and handle its Tick event. In the Tick event, check if the last movement of the mouse was before the certain time, then hide the cursor using Cursor.Hide(). Also handle MouseMove of the PictureBox and show the cursor using Cursor.Show() method.
Note: Don't forget to enable the timer and set Interval of timer to a short value, for example 300 and change duration value in the following code, for a shorter/longer inactive time:
DateTime? lastMovement;
bool hidden = false;
int duration = 2;
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
lastMovement = DateTime.Now;
if (hidden)
{
Cursor.Show();
hidden = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!lastMovement.HasValue)
return;
TimeSpan elaped = DateTime.Now - lastMovement.Value;
if (elaped >= TimeSpan.FromSeconds(duration) && !hidden)
{
Cursor.Hide();
hidden = true;
}
}

Click event on button that is draggable?

So, I made a button that is draggable with these events:
bool isMouseClicked;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMouseClicked = true;
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseClicked == true)
//You then set your location of your control. See below:
button1.Location = new Point(MousePosition.X, MousePosition.Y);
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
isMouseClicked = false;
}
Now, I want to insert something to the "Click" event. When I try to drag the button, the action from the "Click" event is performed. I only want the button to move when I drag it and to perform the action from the "Click" event when I click it. How can I do this?
I would suggest your bool should be named isMouseDown. Now, the Click event fires when a MouseDown and then MouseUp event occurs on the same control. So add another bool justDragged, set it to false on MouseDown, set it to true on MouseMove and isMouseDown and test it in the Click event.
BTW, this information is on SO, just a bit scattered around.
bool isMouseDown = false;
bool justDragged = false;
private void button1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
isMouseDown = true;
justDragged = false;
}
}
private void button1_MouseMove(object sender, MouseEventArgs e) {
if (isMouseDown) {
//You then set your location of your control. See below:
button1.Location = new Point(MousePosition.X, MousePosition.Y);
justDragged = true;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e) {
isMouseDown = false;
}
private void button1_Click(object sender, MouseEventArgs e) {
if (!justDragged) { // then we clicked
// do click handling
}
}

Tab Control blocking mouse scroll event C#

I have a tab control that contains multiple tab controls. All of the tab controls were made using the winforms designer. The embedded tab controls each have Chart objects assigned them. These were created after the program was ran, and each chart was given three events:
chart.MouseWheel += new MouseEventHandler((sender, e) => this.Chart_MouseWheel(sender, e, chart, raw, condensed, bounds));
chart.MouseHover += new EventHandler(Chart_Hover);
chart.MouseClick += new MouseEventHandler((sender, e) => this.Chart_Click(sender, e, chart));
For easy debugging, I added a simple Console.WriteLine(); to each method to see which methods were actually being fired.
private void Chart_MouseWheel(object sender, MouseEventArgs e, Chart chart, DataTable raw, DataCondenser condensed, List<double> bounds)
{
Console.WriteLine("a");
}
private void Chart_Hover(object sender, EventArgs e)
{
Console.WriteLine("b");
}
private void Chart_Click(object sender, MouseEventArgs e, Chart chart)
{
Console.WriteLine("c");
}
After hovering, clicking, and scrolling a lot, I can only get b and c to be outputted. For some reason the scroll event will not be picked up. I have a feeling this has to do with being inside a tab control.
Any ideas why this is happening?
EDIT:
Tried a small-scale version of this and the same thing is happening.
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Chart test = new Chart();
test.Width = 500;
test.Height = 500;
test.MouseWheel += new MouseEventHandler(Chart_Scroll);
test.MouseHover += new EventHandler(Chart_Hover); //mousehover event for the tooltip to activate
test.MouseClick += new MouseEventHandler(Chart_Click);
tabPage3.Controls.Add(test);
}
private void Chart_Scroll(object sender, MouseEventArgs e)
{
Console.WriteLine("a");
}
private void Chart_Hover(object sender, EventArgs e)
{
Console.WriteLine("b");
}
private void Chart_Click(object sender, MouseEventArgs e)
{
Console.WriteLine("c");
}
The same issue occurred. tabPage3 was a tabpage of a tabcontrol inside of a tab control.
EDIT 2:
so if I give the chart this event handler:
test.MouseEnter += new EventHandler(mouseEnter);
with the method:
private void mouseEnter(object sender, EventArgs e)
{
this.Focus();
}
it still doesn't work. However, if I use this:
private void mouseEnter(object sender, EventArgs e)
{
if (sender is Chart)
{
Chart temp = (Chart) sender;
temp.Focus();
}
}
It will work, even if it's embedded in other controls.
You could wire up the MouseEnter() event and give the Chart focus with a one-liner like this:
test.MouseEnter += (s, evt) => { ((Control)s).Focus(); };

Drag and dropping a button (Button won't follow)

I'm having a bit of trouble making drag and drop a button in a panel and panel to recognize it and display Message with buttons name
So far I managed the part of dragging and dropping and recognizing however I'm missing the visual style of dragging, when I press with mouse it will just sit on the same place, it won't follow cursor. How do I make it follow the mouse?
public Form1()
{
InitializeComponent();
panel1.AllowDrop = true;
panel1.DragEnter += panel_DragEnter;
panel1.DragDrop += panel_DragDrop;
button1.MouseDown += button1_MouseDown;
}
private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
button1.DoDragDrop(button1.Text, DragDropEffects.Copy | DragDropEffects.Move);
button1.Location= new Point(e.X, e.Y);
}
private void panel_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void panel_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
MessageBox.Show(e.Data.GetData(DataFormats.Text).ToString());
}
You'll have to keep in mind that the DoDragDrop method doesn't return the Position you dropped the object. The DragDrop event handles that.
To move the control while you're dragging you use the DragOver event of the panel. In the implementation you need to compensate for the fact that the X and Y coordinates in the EventArgs are Screen based while you need Clientbased coordinates to correctly position the control. The PointToClient is instrumental in that:
private void panel_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Button).FullName))
{
var draggedButton = (Button)e.Data.GetData(typeof(Button).FullName);
var screenpos = new Point(e.X, e.Y);
var clientPos = panel1.PointToClient(screenpos);
// calc offset
draggedButton.Location = new Point(
clientPos.X + panel1.Left,
clientPos.Y + panel1.Top);
}
}
Notice that your Data now contains the actual Button, instead of only the text. This makes that you can dragdrop multiple buttons, not only button1.
Your DragDrop event should now look like this:
private void panel_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Button).FullName))
{
var draggedButton = (Button)e.Data.GetData(typeof(Button).FullName);
MessageBox.Show(draggedButton.Text);
var screenpos = new Point(e.X, e.Y);
var clientPos = panel1.PointToClient(screenpos);
draggedButton.Location = new Point(
clientPos.X + panel1.Left,
clientPos.Y + panel1.Top);
}
}
And DragEnter only slightly changed so it could handle the Button control instead of the text:
private void panel_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
if (e.Data.GetDataPresent(typeof(Button).FullName)) // button
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
And finally to get it all started and wired up the constructor code and the MouseDown implementation of the button:
public Form1()
{
InitializeComponent();
panel1.AllowDrop = true;
panel1.DragEnter += panel_DragEnter;
panel1.DragDrop += panel_DragDrop;
panel1.DragOver += panel_DragOver;
button1.MouseDown += button1_MouseDown;
}
private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
button1.DoDragDrop(button1, DragDropEffects.Copy | DragDropEffects.Move);
}

How to Drag, Drop and Resize Label on a Panel at runtime? C#, winForms

I have this code for dragging on a Panel but its not doing the thing.
I have to choose if I will just Drag&drop or Resize.
I think theres something wrong with my code here in form load.
Anyway I have 5 labels here and a panel named as label1, label2, label3, label4, label5 on panel1.
private void form_Load(object sender, EventArgs e)
{
//for drag and drop
//this.panel1.AllowDrop = true; // or Allow drop in the panel.
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);
//end of drag and drop
}
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;
lblResizeAmtWord.Visible = false;
if (c != null)
{
c.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
//this.panel1.Controls.Add(c); //disable if already on the panel
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
I used the MouseDown, Up and Move Event of the Control I want to move. Let's say my control name is ctrlToMove.
private Point _Offset = Point.Empty;
private void ctrlToMove_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_Offset = new Point(e.X, e.Y);
}
}
private void ctrlToMove_MouseUp(object sender, MouseEventArgs e)
{
_Offset = Point.Empty;
}
private void ctrlToMove_MouseMove(object sender, MouseEventArgs e)
{
if (_Offset != Point.Empty)
{
Point newlocation = ctrlToMove.Location;
newlocation.X += e.X - _Offset.X;
newlocation.Y += e.Y - _Offset.Y;
ctrlToMove.Location = newlocation;
}
}

Categories

Resources