Initially i was having a picture box which can be moved on the form by user from one place to another.
I have handled the events for the picture box and it was moving perfectly.
But now user wants to display a text below the picture. So I thought to create a custom control dynamically and add that picture box and a label control inside the user control.
I also set the dock properties of controls to TOP and Bottom. Now my user control is completely covered with the sub controls.
After that i want to handle the mouse events for the user control. But unfortunately that is not working for me.
As per my understanding, now i cannot access the user control instead i am having access to sub controls in user control, so the mouse events for user control are not working.
Correct me if am wrong, and provide any solution.
well, the mouse event like MouseDown and MouseUp occurs only when the mouse is doing something on the specific control. the best offer i can give you is to catch each mouse event in the controls and call a method on the userControl
public UserControl1()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(this.UserControl1_MouseDown);
this.comboBox1.MouseDown += new MouseEventHandler(this.comboBox1_MouseDown);
}
private void UserControl1_MouseClick(object sender, MouseEventArgs e)
{
UCMouseDown();
}
private void UserControl1_MouseDown(object sender, MouseEventArgs e)
{
UCMouseDown();
}
private void comboBox1_MouseDown(object sender, MouseEventArgs e)
{
UCMouseDown();
}
private void UCMouseDown()
{
// Your code
}
Related
I am trying to make a click counter for everything on my form.
My form consists of textboxes, buttons, pictureboxes and labels.
What my problem is that my picutrebox is covering the whole form because I want it as a background picture. So when I have the private void Form1_Click(object sender, EventArgs e) event on it doesn't register the clicks that are on the picture box. And when I have private void pictureBox2_MouseClick(object sender, MouseEventArgs e) event the code it doesn't register any of the clicks I do on the buttons or text boxes. Is there an event for a click on absolutely everything on the form?
There are various ways you can handle this.
One way is to send the picture box back so your buttons come on top like this:
pictureBox.SendToBack();
Or you can attach an event handler to all of your controls like this:
foreach (Control control in Controls)
{
control.Click += Control_Click;
}
I have been looking around for a while for some code that tells what control the mouse has clicked. I have a Form with over 50 controls and I don't want to click each one to make a mouse clicked on. How would I do this?
You can use the Tag property of each control. So set it to something meaningful and on Click event do something like this:
(sender As Control).Tag
EDIT: Also you may do this:
foreach (Control item in this.Controls) //this IS YOUR CURRENT FORM
{
if ((sender as Control).Equals(item))
{
//Do what you want
}
}
Approach One: Individualized Handling
The mouse click event will actually be received by the control on which the mouse is clicked, so the only thing you need to do is handle the MouseClick event for that control. That makes sense if you want mouse clicks to do something different for each of your controls.
This is just the basic event-handling strategy that you should already be familiar with. The event handler method can be wired up either using the designer or manually via code, and looks like this:
private void myControl_MouseClick(object sender, MouseEventArgs e)
{
// do something when the myControl control is clicked
}
Approach Two: Consolidated Handling
If you want the same behavior on multiple controls, you would wire up a single event handler method to handle the MouseClick event for multiple controls. Then, from inside of the event handler method, you would use the sender parameter to identify the control that was clicked. For example, you could cast sender to a Control object and test the value of the Name property.
If you wanted all controls of a certain type to behave a certain way, and all controls of another type to behave a different way, you could have two event handler methods and wire the controls up accordingly by type.
The easiest way to do this is to wire up the event handler methods through code. The designer would work, but it would be overly tedious to use it for each of many controls. For example, in your form's constructor, you could loop over all of the controls and hook up your event handlers:
public MyForm()
{
this.InitializeComponent();
// Wire up your event handlers.
foreach (Control ctrl in this.Controls)
{
if (ctrl is Label)
{
// Wire all Label controls up to do one thing.
ctrl.MouseClick += new MouseEventHandler(LabelControls_MouseClick);
}
else if (ctrl is Button)
{
// Wire up all Button controls to do another thing.
ctrl.MouseClick += new MouseEventHandler(ButtonControls_MouseClick);
}
else
{
// And wire up the rest of the controls to do a third thing.
ctrl.MouseClick += new MouseEventHandler(OtherControls_MouseClick);
}
}
}
private void LabelControls_MouseClick(object sender, MouseEventArgs e)
{
// do something when a Label control is clicked
}
private void ButtonControls_MouseClick(object sender, MouseEventArgs e)
{
// do something when a Button control is clicked
}
private void OtherControls_MouseClick(object sender, MouseEventArgs e)
{
// do something when a control other than a Label or Button is clicked
}
Approach Three: Global Handling
If you've made all of these controls transparent (that is, transparent to mouse events, not visually transparent) so that mouse click events are handled at a higher level (i.e., by your form), you can use the Control.GetChildAtPoint method to determine the control that was clicked on. You just specify the coordinates of the location at which the mouse was clicked, and the method will return the child control (if any) that is located at that point.
private void myForm_MouseClick(object sender, MouseEventArgs e)
{
Control ctrl = Control.GetChildAtPoint(e.Location);
if (ctrl != null)
{
// do something with the clicked control
}
else
{
// if ctrl is null, then the parent form itself was clicked,
// rather than one of its child controls
}
}
I don't really recommend this approach, though, because it violates good object-oriented design. You have to write code to determine which control is which based on its unique properties, instead of just letting the runtime determine and handle that automatically.
I developping one win-form application which having one custom control with one label and text box, and placed the custom control in one panel with docksytle as fill,
there is mouse click event for panel and custom control both, but when i click only custom control mouse click event is firing not the panel click event,
so anyone please let me know how to call the panel mouse click event.
Are you sure that you really need to invoke click of parent control? In general it would be, in my opinion, a code smell if you will do something like that - especially when it requires some strange constructions.
If you need to react in a same way when clicking on panel and on any child control inside the panel, it should be enough just to call the same method from two event handlers (that is from event handler of parent panel and event handler of child control. If you need, for example, mouse pointer location inside parent panel, you can easily calculate the position of mouse pointer using, for example, PointToScreen() and PointToClient() methods.
This is not a general solution, but maybe it's what you're looking for:
private void CustomControl_MouseClick(object sender, MouseEventArgs e)
{
panel_MouseClick(sender, e);
}
private void panel_MouseClick(object sender, MouseEventArgs e)
{
}
Create Click Event for each control in panel and invoke the parent :
private void This_Click(object sender, EventArgs e)
{
this.InvokeOnClick(this, null);
}
How do I drag a item out of a Winforms-listview control onto another control (picture of trash can)?
UPDATE1:
I think the basic flow is:
for the ItemDrag event on the listview have a DoDragDrop
Then have a DragEnter event on the picturebox that captures that drag?
UPDATE2:
The basic flow (based on answers):
add 'ItemDrag' event to the listview.
add a 'DoDragDrop' inside the 'ItemDrag'
add 'DragEnter' event to the picturebox.
add a 'GetDataPresent' check inside the 'DragEnter' to check on the data type
add a 'DragDrop' event to the picturebox
add a 'GetDataPresent' check inside the 'DragEnter' to check on the data type
Implement an event handler for the list view's ItemDrag event:
private void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
DoDragDrop(e.Item, DragDropEffects.Move);
}
And write the event handlers for the trash can:
private void trashCan_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(typeof(ListViewItem))) {
e.Effect = DragDropEffects.Move;
}
// others...
}
private void trashCan_DragDrop(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(typeof(ListViewItem))) {
var item = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
item.ListView.Items.Remove(item);
}
// others...
}
You'll have to force the AllowDrop property for the PictureBox, it isn't available in the Properties window:
public Form1() {
InitializeComponent();
trashCan.AllowDrop = true;
}
Look into DragEnter, DragLeave, and DragDrop. Also see example, Implementing Drag and Drop in ListView Controls
EDIT This applies only if you want shell integrated drag-and-drop. If you are not integrating with the shell, and only dragging and dropping between things in your own app, then this answer does not apply. My apologies for the confusion.
You need to support drag-n-drop in your app or control. This involves some COM interop.
It seems a little complicated at first, but once you get the basic skeleton up, it's not that hard to implement. Also there's a nice guide right here, that tells you how:
http://blogs.msdn.com/adamroot/pages/shell-style-drag-and-drop-in-net-wpf-and-winforms.aspx
I have a Windows Forms application in C# .NET. It contains a user-drawn control that also handles the keyboard focus. If a part of the control has the focus, a focus highlight border is painted around it. When the form that contains the control is deactivated, the focus border must disappear from the control, obviously. But the control doesn't even get a notification about it. It only receives the "Leave" event when another control is focused, not another window. How can the control know about that?
When the Form+Control are loaded, the Control can subscribe to the Activate and DeActivated events of the Form.
If it is a UserControl, you have the Control.Load event to do this. For a CustomControl I would have to look it up.
Anyway, be sure to implement Dispose in your Control to unsubscribe to the events.
Just gave it a try:
private void UserControl1_FormActivate(object sender, EventArgs e)
{
label1.Text = "Acitve";
}
private void UserControl1_FormDeActivate(object sender, EventArgs e)
{
label1.Text = "InAcitve";
}
private void UserControl1_Load(object sender, EventArgs e)
{
this.ParentForm.Activated += UserControl1_FormActivate;
this.ParentForm.Deactivate += UserControl1_FormDeActivate;
}