eventhandler for a list of pictureboxes - c#

I have a list of pictureBoxes that created at run-time.
How can I define same eventhandlers (same mousemove event, for example) for each picturebox in the list?
I use C# in Visual Studio .net 4
Thanks for every help

http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox_events.aspx all events supported by PictureBox can be found here
For e.g
pictureBox[0].MouseMove += new System.Windows.Forms.MouseEventHandler(pictureBox_MouseMove);
protected void pictureBox_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
//...
}

What about simple
PictureBox pb = new PictureBox();
pb.MouseMove += new EventHandler(MyHandler_Click);
And your handler:
private void MyHandler_Click(object sender, System.Windows.Forms.MouseEventArgs e)
{
//your code
}

Related

how to detect drag begins in wpf

I know from this question how to handle a drag&Drop
https://stackoverflow.com/a/17872857/982161
but I can not detect when the Drag event begins so I can prepare some resources...
if I print those events the Drop is coming first and after that the Drag..
how can this be cleanly handled
My Code is pretty simple
private void Label_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var lbl = (Label)sender;
DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Move);
Console.WriteLine("Drag...");
}
private void Label_Drop(object sender, DragEventArgs e)
{
Console.WriteLine("Drop...");
}
private void Label_DragEnter(object sender, DragEventArgs e)
{
Console.WriteLine("Label_DragEnter...");
}
private void Label_DragLeave(object sender, DragEventArgs e)
{
Console.WriteLine("Label_DragLeave...");
}
Long story short: If you want to prepare resources before you drop the label, write that code before calling the DragDrop method or in the OnPreviewMouseDown event.
Long Story:
Using Snoop I was able to look into the events that are triggering when dragging the label.
It appears that the only events triggering are the PreviewMouseDown and MouseDown.
So we should only implement those events.
private void Lbl_OnMouseDown(object sender, MouseButtonEventArgs e)
{
var lbl = (Label)sender;
DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Move);
Console.WriteLine("Drag...");
}
private void UIElement_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Label_PreviewMouseDown...");
}
This will result in first printing "Label_PreviewMouseDown..." when starting to drag the label and "Drag..." when the label is done being dragged.
However, this isn't the complete truth.
Let's modify our code a little. Let's add DateTime.Now.Second to test when the messages are actually triggering. I will then drag the label for a few seconds, then drop it to see the order of printing to console.
private void Lbl_OnMouseDown(object sender, MouseButtonEventArgs e)
{
var lbl = (Label)sender;
Console.WriteLine("Label_OnMouseDown_BeforeDragging..." + DateTime.Now.Second);
DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Move);
Console.WriteLine("Label_OnMouseDown_AfterDragging..." + DateTime.Now.Second);
}
private void UIElement_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Label_PreviewMouseDown..." + DateTime.Now.Second);
}
Now let's try dragging again.
Turns out that OnMouseDown happens before you are done dragging. The DoDragDrop method pauses the code there until you drop the label, then you are able to continue and print to the console.
So therefore: If you want to prepare resources before you drop the label, write that code before calling the DragDrop method or in the OnPreviewMouseDown event.
Hope this helps.
If you need a drag event that is not provided by the control then you may consider adding one to your own code. For example
// Event fired immediately before DragDrop
public DragEventHandler DragBegin { get; set; }
private void Label_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var lbl = (Label)sender;
// Create args object and fire event if not null
var args = new DragEventArgs(new DataObject(lbl.Content), DragDropKeyStates.None, DragDropEffects.None, lbl, e.GetPoint(lbl));
DragBegin?.Invoke(sender, args);
DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Move);
Console.WriteLine("Drag...");
}
You could then bind to that event from anywhere you have a reference to that code behind, such as
MyUserControl.DragBegin += (sender, args) => /* some behavior */;

C# winforms. Drag events

I need to run code when I dismiss(end) dragging click. How this event is called?
Here is main example, please find the below screenshot for more information:
I made that I can drag the car on other picture boxes like below this:
Repeat again - I need to know what EVENT is then you dismiss to drag on picture box?
There is no event for when the drag is released on a control, but you don't really need one. This is what I did to simulate what (I think) you're looking for. I used code courtesy of this stackoverflow answer
private Point? _mouseLocation;
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.MouseDown += this.pictureBox1_MouseDown;
this.pictureBox1.MouseUp += this.pictureBox1_MouseUp;
this.pictureBox1.MouseMove += this.pictureBox1_MouseMove;
}
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if ( this._mouseLocation.HasValue)
{
this.pictureBox1.Left = e.X + this.pictureBox1.Left - this._mouseLocation.Value.X;
this.pictureBox1.Top = e.Y + this.pictureBox1.Top - this._mouseLocation.Value.Y;
}
}
void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
this._mouseLocation = null;
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//Check if you've left-clicked if you want
this._mouseLocation = e.Location;
}
Setting the mouse location to null with this._mouseLocation = null; is your "drag released" code.
I guess you talking about DragDrop. You can find example here: How to: Enable Drag-and-Drop Operations with the Windows Forms RichTextBox Control

custom tooltip in c# .net win forms

I am looking to simulate a custom tooltip the like of you see in websites using c# .NET 4.5 windows forms.This tooltip will basically show status of some Tasks like how many tasks are pending,tasks in process, completed etc.To do this i am using a borderless win form.This winform will have some texts, images etc.I want it to reveal itself on button's mouseHover event and disappear on MouseLeave event.My problem is that on Mousehover event numerous instances of that tooltip form is getting generated and on MouseLeave they are not getting closed.My code is
private void B_MouseHover(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Close();
}
My code is not working, hence please tell me how to do this the correct way.Thanks
You're generating a new instance of the form class every time you get a hover event, and every time you get a leave event. If you want to continue to use this approach I would recommend you use a variable on your main form object to store the reference to your tooltip form. Secondly, you need to not generate a new instance whenever the event handler is called, but only when necessary. I would create your instance the first time your Hover event is called for a particular control, and then dispose of it when your Leave handler is called -- this is under the assumption that the tooltip dialog's constructor loads up different information for each control being hovered over. Like so:
frmSecQStatToolTipDlg f_tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
f_tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(f_tooltip != null)
{
f_tooltip.Close();
f_tooltip = null;
}
}
You should keep a global field for this form, and should not dispose or close it. Just hide it on some events and show again.
Sample Code:
frmSecQStatToolTipDlg tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg != null)
{
tooltip.Hide();
}
}
With this logic you'll not have to create tooltip instance again and again and it will not take time to popup if you frequently do this activity.
Declare your tooltip once as readonly and use it without asking anytime if it is null or not.
If you need to Dispose it, implement the IDisposable pattern:
https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
private readonly frmSecQStatToolTipDlg _tooltip = new frmSecQStatToolTipDlg() ;
private void B_MouseHover(object sender, EventArgs e)
{
_tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
_tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
_tooltip.Hide();
}

Changing padding on hover to PictureBox dynamically

A PictureBox is added dynamically to the form:
PictureBox image = new PictureBox();
//setting image properties...
image.MouseHover += ImageHover; //also tried MouseEnter
image.MouseLeave += ImageLeave;
Controls.Add(image);
And the event methods:
private void ImageHover (object sender, EventArgs e)
{
((PictureBox)sender).Padding = new Padding(7);
}
private void ImageLeave (object sender, EventArgs e)
{
((PictureBox)sender).Padding = new Padding(3);
}
Now the event doesn't always trigger. It does, but in a weird way (like when I ALT+TAB back to the form and when the form images load).
I thought of putting the events in a Thread, but I have no idea how to call delegate instances (called methods) as Threads.
How can I fix this?

How do I make a button show the listbox when clicked?

I am making a form where if a button is clicked it will go to my list box and run the functions I have in there though I am a bit confused on how to make it realise when the button has been clicked and for the listbox to work. Here is my code >_>
private void button1_Click(object sender, EventArgs e)
{
}
public void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
Ping.PlayConsole();
}
You just need:
private void button1_Click(object sender, EventArgs e)
{
Ping.PlayConsole();
}
It's ok to call the same function under different handlers.
Try this :
private void button1_Click(object sender, EventArgs e)
{
ListBox_SelectedIndexChanged(sender,e);
}
Good Luck !!
private void button1_Click(object sender, EventArgs e)
{
ListBox.Focus();
Ping.PlayConsole();
}
Both event handlers share the same signature void (object , EventArgs), so they are call-compatible.
If you are connecting the event visually using the form designer:
Go to the Property Inspector's Events pane and instead of double-clicking it to create an event handler stub for button1.Click, click the dropdown box icon that appears on the right side. Visual Studio will show all the event handlers present in the form that have a compatible signature, you should be able to choose ListBox_SelectIndexChanged for the button1.Click handler. They will be sharing the same handler.
If you are connecting the handler by code then this should work too:
ListBox1.SelectIndexChanged += new System.EventHandler(ListBox_SelectedIndexChanged);
button1.Click += new System.EventHandler(ListBox_SelectedIndexChanged);

Categories

Resources