Changing padding on hover to PictureBox dynamically - c#

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?

Related

How can I send other object types from a function to another function using EventHandler

I would like to make an app that is using INI file to store new FlowLayoutPanels, what I cant get to work on is that I want to make an EventHandler when I DoubleClik a PictureBox that is inside that new FlowLayoutPanel created to delete that very own FlowLayoutPanel.Here is some code:
private void Function(object sender, EventArgs e)
{
FlowLayoutPanel flP = new FlowLayoutPanel();
PictureBox picB = new PictureBox();
Label laB = new Label();
flP.Controls.Add(picB);
flP.Controls.Add(laB);
flowLayoutPanel1.Controls.Add(flP);
picB.DoubleClick += picB_DoubleClick;
}
private void picB_DoubleClick(object sender, EventArgs e)
{
FlowLayoutPanel flp = (FlowLayoutPanel)sender;
flp.Dispose();
}
You are receiving that error because the object sender parameter of the click event is actually the picturebox object. You are trying to act the picturebox as a flow layout panel. You can't do that. A flowlayoutpanel is not a picturebox object and does not inherit from picturebox.
What your probably trying to do can be accomplished by casting sender as a picturebox: var pb = (picturebox) sender; and then getting the parent of the picturebox var flp = pb.parent as flowlayoutpanel; and then disposing flp. flp.dispose();
I'm typing this answering on my iPhone without access to VS, I'm unsure the consequences of disposing the parent object from an event of a child object.

adding an action for click event on picturebox in c#

I get a pictureboxe and try to add it an action to change background Image upon click event happen, but there is no action. So, this is the code:
pieces bishopBB = new pieces();
public Form1()
{
// object of picturebox
bishopBB.Location = new Point(300, 455);
bishopBB.Parent = this;
bishopBB.Click += new System.EventHandler(pictureboxes_Click)
InitializeComponent();
}
private void pictureboxes_Click(object sender, EventArges e)
{
backgroundImage = Properties.Resources.black;
}
Looking at the name and other indicators I would assume (and hope) that pictureboxes_Click is a common click handler for many PictureBoxes.
So to access the one that has been clicked you need to cast sender to PictureBox and then set the BackgroundImage:
private void pictureboxes_Click(object sender, EventArges e)
{
((PictureBox)sender).BackgroundImage = Properties.Resources.black;
}
I'm a little amazed though that your spelling of backgroundImage compiles. The correct spelling BackgroundImage refers to the current class, usually the Form and it should also show, unless your Form has a black background already..
To change the current image of the PictureBox control, you need to refer to the control and reference the BackgroundImage property (not 'backgroundImage'):
private void pictureboxes_Click(object sender, EventArges e)
{
this.pictureboxes.BackgroundImage = Properties.Resources.black;
}
To change the background image of the form:
private void pictureboxes_Click(object sender, EventArgs e)
{
this.BackgroundImage = Properties.Resources.black;
}
To do this you can use the on_click property of the picturebox. You can then use the like picbox.image = whatever the image location is

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();
}

I want to use BackgroundWorker to speed up my graphical appearances

I am writing a Kiosk-type program for a media center PC. I have some pictureBoxes as my buttons, and would like a label or another pictureBox (acting as a label) to be visible/not visible and be triggered by a MouseEnter and MouseLeave event.
When the MouseLeave event happens(triggering Visible=false), a white box appears in place of the pictureBox or label that was previously there for a split second until the background image fills back in.
I have read up a little bit on using the BackgroundWorker to "pre-load" my pictureBox or labels. I am not sure that i am doing it right. I am very new to programming; i dabble here and there, and do not have any formal training in c#.
I am not looking for anyone to write the code for me, but i also am not objected to that either. A simple example of how to use it should suffice.
I have a background image (1920x1080) on my form. I think this image is actually having a harder time refreshing after the label or pictureBox's visibility is set to false.
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
this.pictureBox9.Visible = true;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
this.pictureBox9.Visible = false;
}
I am attempting to use the backgroundWorker DoWork event, but really have no idea what i am doing.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.pictureBox9.Visible = true;
this.pictureBox9.Visible = false;
}
My question is, "How do i use backgroundworker to effectively reduce the lag caused by changing visibilty of my pictureBoxes or lables?
The BackgroundWorker works like this:
BackgroundWorker workerOne = new BackgroundWorker();
BackgroundWorker workerTwo = new BackgroundWorker();
private void MyForm_Load(object sender, EventArgs e)
{
workerOne.DoWork += workerOne_DoWork;
workerTwo.DoWork += workerTwo_DoWork;
}
private void ThingOne_Click(object sender, EventArgs e)
{
workerOne.RunWorkerAsync();
}
private void ThingOne_Click(object sender, EventArgs e)
{
workerTwo.RunWorkerAsync();
}
void workerOne_DoWork(object sender, DoWorkEventArgs e)
{
//This will run as async and not interupt main thread
}
void workerTwo_DoWork(object sender, DoWorkEventArgs e)
{
//This will run as async and not interupt main thread
}
I've included two in the example. Basically the RunWorkerAsync breaks it off in its own thread.
I do not recommend using BackgroundWorker for this because the fact of changing the Visibility of a Visual Element requires the Main Thread and not a Worker Thread. What I recommend in any case is to handle the Opacity so you don't have to load the image again.

eventhandler for a list of pictureboxes

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
}

Categories

Resources