MDi background image make animated sub form appear slow - c#

I have a panel and a timer which is helping me to open a form in an animated way.
The panel contains a form.
code is
private void timer1_Tick(object sender, EventArgs e)
{
if (panel1.Width < 1004)
{
panel1.Left -= 50;
panel1.Width += 50;
}
else
{
timer1.Enabled = false;
}
}
Code is working perfectly when I click a button to enable the timer with no background image in the MDI Form.
But whenever I put a background image its become slow. Takes longer time than before. Every Timer interval can be understood. Form appears by shaking.
Is there is any solution? What can be done to avoid this.

Related

Usercontrol with transparent backcolor when animating, it is not visible

I have created a usercontrol which has transparent background. I use this usercontrol on windows form and do some animation stuff on control when a button is clicked.
myctrl is usercontrol created in windows form. Below is code :
private void button1_Click(object sender, EventArgs e)
{
Point p1 = new Point(myctrl.Location.X, myctrl.Location.Y);
for (int i = 0; i < 100; i++)
{
myctrl.Location = new Point(p1.X + i, p1.Y + i);
myctrl.Update();
pictureBox1.Update();
i++;
Thread.Sleep(100);
}
}
Problem : Once loop starts usercontrol is not visible and when loop ends control is visible.
Also, I'm placing the control on picturebox which is placed on winforms.
Perhaps you have to use Refresh instead of Update.
Or you may completely change how you do it once you read about AnimateWindow.
Edit
I must be blind to not notice a clear ussue with your animation. The point is, you doing it at once (with naive Thread.Sleep). And while you doing it, repainting is blocked (even if you call Refresh you will not see result). So yes, for a duration of animation there will be nothing visible.
What you have to do is to split animation (by using Timer more likely) into frames and display only one frame at once.

WinForm button background image updates only every other run

I want a blinking LED -- alternately a lighted, then a dark image in a PictureBox -- to appear during a run (that I click a button to start). The image lights when the run starts, and goes dark when it the run finishes. That always works.
This code:
this.timer.SynchronizingObject = this;
this.timer.Interval = 250;
this.timer.Elapsed += (s, ea) =>
{
this.ledLit = !this.ledLit;
ShowInLog(this.ledLit ? "/" : "\\");
this.picMarking.BackgroundImage = this.ledLit ? this.imageStopped : this.imageRunning;
this.picMarking.Refresh();
};
works great to show a blinking LED image during the run...every other run.
On every even-numbered invocation, the display of alternating slashes shows that the timer is working, but the background image does not update (except perhaps a rare flicker).
Why? How do I make it to work on every invocation?
Here's some quick code I put together for your "blinking" effect:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Blinker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 250;
timer1.Tick += timer1_Tick;
timer1.Start();
}
void timer1_Tick(object sender, EventArgs e)
{
Console.WriteLine("Tick");
pictureBox1.BackColor = (pictureBox1.BackColor == System.Drawing.Color.Red) ? System.Drawing.Color.Black : System.Drawing.Color.Red;
}
}
}
I've used event handlers for the Tick event on the timer to trigger the blinking. You can adjust the image like I have with the BackColor property. It's a quick and dirty approach, but it gets the blinking effect achieved.
I found the answer to my problem here: Custom event handler is repeating itself...
My problem is that I subscribed to the tick event each time, rather than only once.

WP7, C#, Increase slider value by holding a button

I have to increase the slider control value while holding a button.
As long as I am holding the button the slider has to keep increasing.
For example, the scenario is typical with volume control.
I have a slider for volume and a button Increase for volume.
Now as long as I keep holding the Increase button, the volume (marker) in the slider should keep increasing continuously till I release the button.
What I have achieved is changing the value of the slider on individual click events on the button.
Kindly give your suggestions on how I can achieve this.
According to the book there should be RepeatButton. In this case it will perfectly suit your needs. Try to avoid Thread sleeps. Freezes are not pretty good thing. It's one of first candidates for refactoring.
public bool Ok = false;
public void Do()
{
while (Ok)
{
this.Text += ".";
System.Threading.Thread.Sleep(100);
Application.DoEvents();
//I added dots to the form text , You do your own mission
}
}
private void btnLouder_MouseDown(object sender, MouseEventArgs e)
{
Ok = true;
Do();
}
private void btnLouder_MouseUp(object sender, MouseEventArgs e)
{
Ok = false;
}
Add a TrackBar control to the form and then
Replace the code in While block with this one, it does exactly what you want :
if(trackBar1.Value < trackBar1.Maximum)
trackBar1.Value += 1;

How to fade in/out a panel with content within using c#

I have a Panel as a container this panel has a picture on it as a background, within the container panel, I have another panel where I gonna put some information in labels, that information will change in time, what I want is a transition when a new info is about to show, fade out the information panel with the old info and then fade in the same panel with the new info. At the time of the fade out the information panel I will be able to see the backgroud image of the container panel. Both panels have BorderStyle=FixedSingle, also the info panel has a backcolor color.
Now my question is: is there any way to fade in/out the information panel and the whole content within too?
I was searching in the web, and I found an approach to this effect working with the panel's backcolor but it doesn't work at all, and besides, the content still there, since they just try to fade the backcolor property:
Timer tm = new Timer();
private void Form1_Shown(object sender, EventArgs e)
{
tm.Interval = 100;
tm.Tick += new EventHandler(timer1_Tick);
tm.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
int aa = 0;
panel2.BackColor = Color.FromArgb(aa, 255, 0, 0);
aa += 10;
if (aa > 255)
tm.Enabled = false;
}
Any help will be appreciated.
I don't believe you can set the opacity of individual controls. The form itself has an opacity, but I don't think you want to fade out the whole control.
You can create custom controls that support opacity...here's an example:
http://www.slimee.com/2009/02/net-transparent-forms-and-controls-with.html
I believe this implementation would apply to child controls within the panel (because it is working on the rectangular area that the control takes up). If I'm wrong, you'd have to handle all of the child control's as part of your over-ridden behavior.
As others have said, getting this to look 'smooth' might be a lot of work. Hopefully someone will have a better answer.
As suggested in other answers, you can not (easily without your own controls) fade in/out a panel.
You can fade the form in or out at start up OR have a modal dialog form that fades in or out.
Fade In
private void FadeIn_Tick(object sender, EventArgs e)
{
this.Opacity += .08;
if (this.Opacity >= 1)
{
FadeIn.Stop();
}
}
Fade Out
private void FadeOut_Tick(object sender, EventArgs e)
{
this.Opacity -= .08; //Decrease opacity
if (this.Opacity <= 0) //While it is not 0
{
FadeOut.Stop(); //Stop!
this.Close(); //Close the form
}
}

C# Fade-in and fade-out transparency when form is being moved

I have the code to make the form partially transparent when is being moved, but I want to know if it's possible to add fade-in and fade-out effects when I start moving and stop moving the form.
EDIT
The code I am using to add transparency to the form is:
bool canMove = false;
private void Form1_Load(object sender, EventArgs e)
{
canMove = true;
}
private void Form1_Move(object sender, EventArgs e)
{
if (canMove)
{
this.Opacity = 0.7;
}
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
this.Opacity = 1;
}
You should use a Timer control, set the opacity in timer's tick event. until the form stops moving (define a variable like isMoving and set it to true/false based on form's status).
You can find an example of setting opacity in timer's tick event in my article about a fading label. Use Google translator to read it.
Hope this helps.
You could take a Timer control, you could then start the timer when form starts moving and set the transparency of the form to some value, and on each tick of the Timer, make the transparency to decrease and on some value make it to increase. If you want to have the fadein fadeout effect when form stops moving, you can do the same when the form has moved.

Categories