Mousehover too much delay when pictures change (winforms) - c#

I'm facing a problem in windowsforms. When I change the backgroundimage of a menubutton the delay is way too long, it takes around 1/2 seconds to change it. The reason why it's happening is not because of my computerspeed, I'm sure of that. Here's my code, I'm looking forward to someone that could help me out.
private void button1_MouseHover(object sender, EventArgs e)
{
button1.BackgroundImage = Properties.Resources.Tomb_of_the_Ancients_Portal;
label1.Visible = true;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.BackgroundImage = Properties.Resources.Toxic_Sewers_Portal;
label1.Visible = false;
}

You can call .Refresh() on the button so it can redraw its graphics.

Related

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.

I want to make a picturebox disappear after a certain time has passed without using a control

Hello I am kind of new to C#... I have a picturebox which is my control. It's meant to be rapidly clicked on. I want to make it so when it is clicked a certain amount of times a second picturebox which has a .gif in it becomes visible. And when a certain amount of time has past without clicking the first picturebox the second disappears. Is there a way to do this? Maybe with timers. Some sample code will help me out A LOT! Thanks to all in advance! :)
Try This:
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=60000;//one minute
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();
private void pictureBox1_Click(object sender, EventArgs e)
{
timer1.Stop();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
pictureBox2.Visible = false;
}

How do I use a PictureBox as a Button

I'd like to create a roulette game, and I've made a PictureBox with an image of a roulette table.
I've tried to set Visibility = False in the property and in code:
pictureBox1.Visible = !pictureBox1.Visible;
But when I click on the hidden PictureBox, nothing happens.
Do you have any idea how to make it work?
I want to make it visible from being invisible, by clicking on it.
Once you make the visibility of an Item false, it can no longer be clicked. You can handle the click event on the form or container level, check that the mouse coordinates are contained in the bounds of the PictureBox and use that to make it visible again.
i.e.
//Common eventhandler assigned to all of your PictureBox.Click Events
private void pictureBox_Click(object sender, EventArgs e)
{
((PictureBox)sender).Visible = false;
}
private void Form1_Click(object sender, EventArgs e)
{
foreach (var item in this.Controls) // or whatever your container control is
{
if(item is PictureBox)
{
PictureBox pb = (PictureBox)item;
if (pb.Bounds.Contains(PointToClient( MousePosition)))
{
pb.Visible = true;
}
}
}
}
Problem with your logic is that once the PictureBox is made invisible, you can no longer click it anymore, since it's no longer on the form, and while you can for sure click the empty space it left, you're still not clicking it.
A possibility would be to, instead of making visible/invisible, to put/remove its background picture, so it appears to have disappeared but in fact it's still there, still able to receive clicks and respond to events.
If you want to make the pictureBox1 not visible, you would write pictureBox1.Visible = false;
Edit: What you are doing should work. I created a winform and put a picture box in it. I double clicked the picture box to make Click event handler and typed pictureBox1.Visible = !pictureBox1.Visible;. When I debug the program and click on the picture box, it turns invisible. I can't think of why it isn't working for you.
Edit: I understand what you want now. To make it visible on the click of it when it is invisible. Unfortunately I don't know how to do that.
A maybe-silly but simple way might be to put one picture box on top of the other. pictureBox1 would be your picture, pictureBox2 would have no picture. Then you could do something like this:
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = false;
pictureBox2.Visible = true;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
pictureBox2.Visible = false;
}
You could put each of your PictureBox controls in a Panel control. Then handle the Click event of both the Panel and the PictureBox.
private void panel1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}

Expand collpase splitcontainer in win form c#

i am working with split container. my split container has two panel and horizontal orientation. in first panel there are some textboxes and one button. when i click on button then a code run to collapse Panel1 of split container. code is like
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
}
my problem is when collapse occur then my button and all the textboxes getting invisible. so i next time not being able to make those control visible. so i want trick like button will not be invisible as a result i can click on that button again to make panel1 visible. if possible guide me how to fix or place my button on splitter rather on panel. so guide me how can i do it.
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
button1.Parent = splitContainer1.Panel1Collapsed ? splitContainer1.Panel2 : splitContainer1.Panel1;
}
Related to my previous comment on your entire posting. this is a small solution with a ToolBarButton. It will only be enabled if the SplitContainer.Panel1 is collapsed.
Code:
private void Form1_Load(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = true;
toolStripButton1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1.Hide();
toolStripButton1.Enabled = true;
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (splitContainer1.Panel1Collapsed)
{
toolStripButton1.Enabled = false;
splitContainer1.Panel1.Show();
}
}

Gradually or smoothly change object size (Checkboxlist)?

C# question
Here is what I am trying to do. When I click a button, I want the checkboxlist to smoothly change from say (200,10) to (200,100) in size. I am successful at getting to size to change instantaneously, but I want it to look smooth.
Here is the code I wrote:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (checkedListBox1.Height < 100)
{
checkedListBox1.Size = new Size(checkedListBox1.Size.Width, checkedListBox1.Size.Height + 1);
}
else
{
timer1.Enabled = false;
}
}
I have used this coding to move objects smoothly, but never to change sizes.
So when you run this code, the box just flickers and it seems like its trying to change size, but it doesn't, and the loop never ends.
Thanks!
You need to set IntegralHeight to false to that the box can be a height that is not a multiple of the item height.
For the flickering, you should probably double-buffer the form which contains this control.

Categories

Resources