Drag And Drop on Customized Control C# - c#

I created a control (called Table) made up by two pictureBoxes and two Labels.
I'm trying to drag and drop it from a panel to another, but it doesn't work.
This is my code:
void TableExampleMouseDown(object sender, MouseEventArgs e)
{
tableExample.DoDragDrop(tableExample, DragDropEffects.Copy);
}
void Panel2DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
void Panel2DragDrop(object sender, DragEventArgs e)
{
panel2.Controls.Add((Table) e.Data.GetData(e.Data.GetFormats()[0]));
}
Obviously I've set AllowDrop to true in panel2. Already when I click on Table object (which is in panel1), the mouse cursor doesn't change. It looks like the MouseDown event doesn't fire...
Thank you!
This is the part of the constructor code in which I subscribe Handlers:
this.tableExample.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TableExampleMouseDown);
this.label2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Label2MouseDown);
this.panel1.DragDrop += new System.Windows.Forms.DragEventHandler(this.Panel1DragDrop);
this.panel1.DragEnter += new System.Windows.Forms.DragEventHandler(this.Panel1DragEnter);

You seem to have forgot to subscribe to the MouseDown event. Simply writing an event hanlder isn't enough.
Put this in the Form_Load event handler or in the form's constructor:
tableExample.MouseDown += new MouseEventHandler(TableExampleMouseDown);
For more information refer to the documentation: How to: Subscribe to and Unsubscribe from Events - Microsoft Docs.
EDIT:
It could also be that you press one of the child controls of your custom control. Child controls have their own MouseDown events.
To make the child controls also raise the parent control's MouseDown event put this in the constructor of your custom control:
MouseEventHandler mouseDownHandler = (object msender, MouseEventArgs me) => {
this.OnMouseDown(me);
};
foreach(Control c in this.Controls) {
c.MouseDown += mouseDownHandler;
}
EDIT 2:
Based on the new code you added to the question you seem to have forgotten to subscribe to the events for panel2:
this.panel2.DragDrop += new System.Windows.Forms.DragEventHandler(this.Panel2DragDrop);
this.panel2.DragEnter += new System.Windows.Forms.DragEventHandler(this.Panel2DragEnter);

Related

user control and raising events from controls placed in user control

So I would like to know what is wrong with the following code, especially from a theoretical point of view.
I have a user control in which I've added a text box.
When I click in the text box I would like the Mouse clicked event raised in the user control.
To my mind, the solution should be:
Create an event handler for the mouse click event in the text box.
in this event handler, raise the mouse click event for the user control.
so this is what i have:
private void txtLog_MouseClick(object sender, MouseEventArgs e)
{
this.OnMouseClick(e);
}
i have tried it and it doesn't work, why is this?
P.S. I would really like to know why this is wrong! A correct solution is great, but I'm really trying to understand where I'm going wrong here. Thank :-)
Well, you could just click on your textbox in design mode and in the property window in events tab add the click event. or if you want to do it in runtime you can do it like this:
textbox.Click += Txt_Click;
private static void Txt_Click(object sender, EventArgs e)
{
// do your thing
}
or even shorter:
textbox.Click += (s,e) =>
{
//do your thing
};
you should do these three steps
declare an MouseClick delegation method for textbox
assign method to textbox
add this delegation to the this (form) OnMouseClick event [on user control constructor]
Step1:
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
}
Step2:
this.textBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseClick);
Step3:
public myUserControl()
{
InitializeComponent();
this.MouseClick += new MouseEventHandler(textBox1_MouseClick);
}

How to stop parent control taking touch event when it contains a nested button

so I have my control which I would like to expand on touch and show my button which is nested within the data template of the control. I would then like the button to be pressable but would also like the control to collapse if touched anywhere apart from the button
Initialise Control
MyControl mc = new MyControl();
mc.TouchDown += mc_TouchDown;
mc.TouchUp += mc_TouchUp;
Event handlers
void mc_TouchDown(object sender, TouchEventArgs e)
{
var mc = (MyControl)sender;
mc.CaptureTouch(e.TouchDevice);
}
void mc_TouchUp(object sender, TouchEventArgs e)
{
var mc = (MyControl)sender;
if (mc != null && e.TouchDevice.Captured == mc)
{
//Expand and show button / Collapse and hide button
mc.ReleaseTouchCapture(e.TouchDevice);
}
}
Now the button is in the data template of MyControl and is hooked up to a command and when i touch the button to run the command MyControl touch events are being fired and the button tap gets ignored.
How do I make it so that the touch events on MyControl work but when the button is shown and touched the button touch event takes priority over the MyControl event?
So I was using Command for my button but I changed this to use the TouchDown event instead and had
private void addToDiary_TouchDown(object sender, TouchEventArgs e)
{
//do my thing
e.Handled = true;
}
and this meant the button was executed and the event didn't bubble up to MyControl
Try to set e.Handled = true for TouchEventArgs to prevent a bubbling of event.

MouseEnter & MouseLeave objectname

I want to add MouseOver and MouseLeave events to dynamical created panels in a flowLayoutPanel.
I added all panels in a list named "panels" and they are accessible with "panels[index]".
Now I want to dynamical add a MouseOver and MouseLeave event to each panel.
I thought it could be possible to get the panelname the Mouse is over and use just one method for each event and identify the panel the mouse is over with its panelname (panel.Name) but I found nothing in "sender".
Is there a way to do this?
My code:
//Method
private void PanelsMouseEnter(object sender, EventArgs e)
{
var panel = sender as Control;
foreach (Control control in this.fLpKoerper.Controls)
{
if (control.Name == panel.Name)
{
foreach (Panel panels in panelsKoerper)
{
if (panels.Name == panel.Name)
panels.BackColor = Color.DarkGray;
}
}
}
}
//Event
panelsKoerper[y].MouseEnter += PanelsMouseEnter;
var panel = sender as Control;
var thePanelName = panel.Name;
I believe you can generate one mouseover event for a control, copy that event method name and then paste it into another controls mouseover event box and that should work
So you would have this event
private void label1_MouseHover(object sender, EventArgs e)
{
//Code...
}
and then you could put 'label1_MouseHover' in any controls mouseover event

Custom Component On Click Event

I've built a custom component that basically has a picture box and label in it. In the parent form, I want to be able to detect when its been clicked on. The standard .click event doesn't seem to be working, but I've never used events before so am unsure if I'm using them correctly. Heres the code I'm using (in the parent) to try and make it recognise the click:
Item aItem = new Item();
aItem.Icon = ItemImage;
aItem.Title = Title;
aItem.Click += new EventHandler(ItemClicked);
aItem.Filename = File;
and heres the method its calling:
public void ItemClicked(Object sender, EventArgs e)
{
MessageBox.Show("Item Clicked!");
}
This code never fires. Do I need to put anything into the component or am I just doing this wrong?
Cheers
Right I finally worked it out. Tejs response just confused me more so here's what I did.
In my UserControl I had the following event:
public event EventHandler Clicked;
Then I had an event for when the image was clicked (still in the UserControl) and I just called the Clicked event:
private void imgItem_Click(object sender, EventArgs e)
{
Clicked(this, e);
}
Then in my parent form, when I created the object, I had the following:
Item aItem = new Item();
aItem.Clicked += new EventHandler(ItemClicked);
void ItemClicked(object sender, EventArgs e)
{
MessageBox.Show("Clicked!");
}
You would do this by exposing an event':
Your custom component:
// A custom delegate like MyItemClickedHandler, or you could make a Func<> or Action<>
public event MyItemClickedHandler ItemClickedEvent;
public void ItemClicked(object sender, EventArgs e)
{
if(ItemClickedEvent != null)
ItemClickedEvent(); // Your delegate could pass parameters if needed
}
Then your parent form simply observes the event:
myCustomControl.ItemClickedEvent += new MyItemClickedHandler(SomeMethod);
Then, whenever the event is raised on your custom control, the parent is notified because it subscribed the event.

MouseWheel event doesn't fire when using any control with scrolbars (in C# Windows Forms)

MouseWheel event doesn't fire
when I' am using any control (ListBox, Panel, TextBox) with scrollbars.
To reproduce problem:
public class Form1 : Form
{
private readonly Button button1;
private readonly TextBox textBox1;
private void button1_MouseWheel(object sender, MouseEventArgs e)
{
ToString(); // doesn't fire when uncomment lines below
}
public Form1()
{
button1 = new Button();
textBox1 = new TextBox();
SuspendLayout();
button1.Location = new System.Drawing.Point(80, 105);
button1.Size = new System.Drawing.Size(75, 23);
button1.MouseWheel += button1_MouseWheel;
button1.Click += button1_Click;
textBox1.Location = new System.Drawing.Point(338, 105);
//textBox1.Multiline = true; // uncomment this
//textBox1.ScrollBars = ScrollBars.Vertical; // uncomment this
textBox1.Size = new System.Drawing.Size(100, 92);
ClientSize = new System.Drawing.Size(604, 257);
Controls.Add(textBox1);
Controls.Add(button1);
ResumeLayout(false);
PerformLayout();
}
// Clicking the button sets Focus, but even I do it explicit Focus() or Select()
// still doesn't work
private void button1_Click(object sender, System.EventArgs e)
{
button1.Focus();
button1.Select();
}
}
I was having the same problem, and what worked for me was to add a handler for the event MouseEnter in the control, that one is triggered with or without focus.
private void chart1_MouseEnter(object sender, EventArgs e)
{
chart1.Focus();
}
After that I could get the mouseWheel events with no problems.
You normally need to make sure the control you want to handle the MouseWheel event is active.
For example try calling button1.Select() in the Form Load (or Shown) event and then using the scroll wheel.
eg:
private void Form1_Load(object sender, EventArgs e)
{
button1.MouseWheel += new MouseEventHandler(button1_MouseWheel);
button1.Select();
}
I found solution, gility is default "Mouse Configuration". Lenovo USB Optical Wheel Mouse default configuration is:
Control Panel/Mouse/Wheel/Whell->Enable Universal Scrolling;
I changed to:
Control Panel/Mouse/Wheel/Whell->Use Microsoft Office 97 Scrolling Emulation Only
Now in .net code MouseWheel working with Focused Control.
But questions are:
how can I fix it in .net code?
how can I detect this situation in .net code?
Any ideas ?
I tried your example, and, whether the lines were commented or not, the MouseWheel event only fires if the button is focused. This behavior is by design. (the MouseWheel event, like keyboard events, goes to the focused control)

Categories

Resources