C# - Drag item out of listview into a trash can? - c#

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

Related

Method for Focus Change

Is there a method which initiates on focus change and can be overridden?
My goal is for the program to fetch closest data automatically from database to input fields whenever user changes his focus/presses enter or tab when on corresponding field. I'm still looking for a way to do this when user selects an item by mouse.
I'm aware that this could be implemented on mouse click but I refuse to believe that there is not a general method for focus change.
What about something like this:
foreach(Control ctrl in this.Controls)
{
ctrl.Enter += new EventHandler(Focus_Changed); // Your method to fire
}
Iterate through all controls and add a enter-event. Bind this handler to your method.
Edit:
Just in case you are wondering why "Enter" and not "LostFocus" or something like that: From my knowledge not every control got focus-events. As I've seen so far "Enter" is presented for all. Maybe there are exceptions. Should be checked out...
You could use Control.Enter event and Control.Leave event for that purpose.
See on MSDN Control.Enter and
Control.Leave.
textBox1.Enter += textBox1_Enter;
textBox1.Leave += textBox1_Leave;
private void textBox1_Enter(object sender, System.EventArgs e)
{
// the control got focus
}
private void textBox1_Leave(object sender, System.EventArgs e)
{
// the control lost focus
}

Which control supports Label with Close Button for windows forms

Is it possible to get a Text with Close button option for a windows Forms Application.Please let me know if we have any Dev Express control is available for the below functionality.
If you need this on across multiple interface. I would recommend you to create yourself an UserControl. (Or component if you prefer) Put a label and a pictureBox in the user control. Then implement the two ClickEvent needed.
public partial class UCTextWithImage : UserControl
{
public event EventHandler TextClick;
public event EventHandler ImgClick;
public UCTextWithImage()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if (ImgClick != null)
ImgClick(sender, e);
}
private void label1_Click(object sender, EventArgs e)
{
if (TextClick != null)
TextClick(sender, e);
}
}
Once done, build you project, so the new userControl become available in form designer toolbox. Then drag and drop this UserControl into your interface. Finally bind and implement the two click event.
If you need this for only a few area, then the ButtonEdit is the closest to what you need.
https://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraEditorsButtonEdittopic
In the properties, you have a button array. Remove the default one and add a new one of type glyph. Set the glyph you want. You can then use the ButtonClick event for the ImageClick and Click event for the LabelClick. However, in LabelClick, you need to check if underlying control isn't a button.
Finally, you need to change the appearance so the ButtonEdit so it become exactly like a label instead of Textbox.
To do so :
1- BackGroundColor must be transparent
2- BorderStyle set to None
Also, with devexpress, in order to have appearance set be taken into consideration, you need to remove the LookNFeel that overwrite everything...
So, it is possible but if you really want to, but userControl solution is easier.

Handling UserControl events dynamically

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
}

Call parent control click event c#

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 to call an event handler from one control to the another control?

In Visual C# Form Application, When I Click on the button I want to add to the other controls(like listboxes,labels,textboxes) in same form.
How do I do this?
I have no idea what "to come to the other controls" might mean. But the event handlers in your Form derived class is the switchboard. Implement the button's Click event and have it do whatever you want done with any other controls. A trivial example:
private void button1_Click(object sender, EventArgs e) {
label1.Text = "You clicked the button!";
}
In the form designer, add an event handler to the button's Click event.
The form designer will give you a new method like this; add your code into this method:
private void button_Click(object sender, EventArgs e)
{
// Write some code that uses list boxes, labels, text boxes etc.
}
You question is somewhat unclear, but if you simply want to access other controls on the form, just go ahead and do so:
private void YourButton_Click(object sender, EventArgs e)
{
string someValue = yourTextBox.Text;
// do something with the value
}
If you want to add one event handler to many controls, you can do it.
Just go to properties of control you wish to subscribe, find appropriate event from list (ex: onClick) and choise your existed handler.
But this method will be sutable if events compotable.
Describe your task more detail.

Categories